Friday, June 10, 2022

Run Ansible Playbook using Jenkins Pipeline

Description: Here I have explained, How to install Ansible plugin in Jenkins, integrate it and Run Ansible playbook using Jenkins Freestyle Project.


Setup:

  1. Ansible install in same machine as Jenkins
  2. Jenkins with require plugins
  3. Client machine for install Nginx
To run the ansible job from Jenkins, we need  to install ansible plugin from Jenkins 













To login and authentication in Remote client machine, we need to setup user with key in ansible/Jenkins machine. Here in this example, I am using Ubuntu user and generate key using below command.

$ ssh-keygen -t rsa



















After running above command you will find 2 key files. One is Private key [id_rsa] and Public Key [id_rsa.pub]  After generating keys, copy public key to destination client machine's  root user  authorisation file.

Here I am going to copy key to root user because, I will use root to login using same user via ansible playbook and install Nginx in destination client server.

Public key file [id_rsa.pub] content looks like as follow:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDWokq0EqR3F6/aFJRYP2P7vsB1DOORosEgmfB9DYG5cLXKlumCoEonG/uZcvns8zBHAgCfSq/gysj9hMmml6KUWfwl7V6/6ethJrpQc3xIQeSamFiGiCJuz9s57y7O95pq/TL9saLJDi2TvmfheLvtUig98CFEQrW+Bvv0AMwHAsQCCqyGhRhdUlnijVoryXlVBpnMZ1GAbzMSeOrF/9t6pfNOujFw7PPK6a45VafcmHgIpPtnxWOzyf/WhpsZ11l+eezyBMQT14Cv5pYnMLswqi9HazDLwHkz81wu+e34r6Uo5vQB/ViIMOyMVZuKwMAokVH5+NI/M5OJlMmbyQPmlWZdtprrSiD59Nb59lFe0wk5rTYoOm6lXfPCUygKkrPvX/co2/SyOc8/zsAtcRSkbjmi6oK7QXSl8GDa/pXy/g1PH1SAkHHg9/YNQbNW1SlvmvYyMxGLLxRkQ42Hj2k396xMc2G2coXXB4ikxxErhQEqHrnv24J0/TzGykImV0M= ubuntu@ip-172-31-11-49

Copy file content to destination machine's root folder authorized_keys as follow











After copy key, need to setup user credentials in Jenkins. To Setup user open Jenkins console and navigate to Manage Jenkins --> Manage Credentials --> Add Credentials 

















Fill details like, username and private key of ubuntu user as follow










































After saving the credentials, I am going to create one playbook in ansible for setup Nginx in ansible/jenkins as follow.

Define Host in ansible hosts file as follow:

[server] 172.31.11.49 ## Ip address of client machine in which we are going to setup Nginx 


Please create playbook.yml for install nginx  in client machine

--- - hosts: server remote_user: root tasks: - name: ensure apache is at the latest version apt: name=nginx state=latest

Define Key file location and user details in ansible.cfg [Located on /etc/ansible directory]  as follow. Also I have copied both the key files in Ansible directory. 

[defaults] host_key_checking = False private_key_file = /etc/ansible/id_rsa remote_user = root remote_tmp = /tmp/

After setting up all the above ansible thing, I am going to test the connection from Ansible to client machine using below command 

# ansible -m ping server 












After showing success, I am going to create Free style project in Jenkins. To create project navigate to Dashboard --> New Item --> Give Name of project --> Select Free Style Project




















Navigate to Build step and select Add Build Step --> Select Invoke Ansible Playbook






















Fill the require details like playbook path, host file path and credentials [setup as previous with private key] and save / apply the project




















After setup  project, Open it and click on Build Now 




















Once build process completed, Open the output of the build. It looks like as follow



















Also we can check the nginx version as follow



No comments:

Post a Comment