Integrate Ansible with Jenkins
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
All credits to the Author and Creator of this youtube video .
https://www.youtube.com/watch?v=xQ_yKp8SdDk
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
I have a Jenkins LTS controller version 2.289.2. And attached to that controller , we have an Agent label of Linux on this agent I have installed the Ansible CLI . Also when I install the controller I installed the Installer suggested plugins. So we installed no extra plugins to integrate with ansible.
Also today we have a sample repository that we are going to be using.
https://github.com/darinpope/jenkins-example-ansible
You can see we have two Jenkins file and a Playbook and an Inventory file. Before we jump dive into what the playbook is going to do. Lets go and create a Job to integrate with Jenkins file_1 just to make sure everything is working correctly with Ansible.
Jenkins -- New Item --
And we are going to select Pipeline
And we are on the main branch
And finally we are going to test with Jenkins file-1
Lets take a quick look at Jenkins file one just to see what we are doing.
We are connecting to an Linux Agent for that is what we have. And we are checking the versions of Ansible, Ansible-Playbook, and Ansible Galaxy
Now go back to the controller and press Build Now.
As you can see once it starts up we are connected to our Agent 1
Now lets go to our Jenkinsfile-2 now and understand what we are going to be doing in today's video.
What you will see here is that you are running an Ansible-Galaxy for our requirements yaml. Because we have specifics in our playbook that has a dependency on our requirements Yaml.
We will look at what is requirement.yml in just a moment
And a playbook that is running a playbook using inventory mariadb.hosts . The playbook is going to install mariadb for us on a different instance . So within our inventory we will be third server .
We have a controller and agent and now we have a third server where we are going to be installing mariadb.
Then we pass on the private key and the playbook is going to install maria.
Ansible private Key is also an environment variable printed down here at line 10.
We are pulling in a secret file which will be mariadb-private-key file
due to these lines "firewalld" we are bring in the collections that is required to run this which is in requirements.yml file as below.
This is used in order to access firewalld.
We talked about using a secret file by referencing in our playbook --private-key
If you checkout your ansible documentation you will see that private-key is a reference to a file. But we cannot guarantee that file will exist in our agent. But we can provide it via a secret file credential type.
Go to Manage Jenkins -- Manage credentials -- Add Credentials - we are going to change the type.
Kind -- to secret file
We will then browse to the file that I have. which is actually named "mariadb-private-key".
Now lets go back to our job and change our reference from Jenkinsfile1 to Jenkinsfile2 .
Click on the Job -- ansible -- configure on the left and -- script path to >Jenkinsfile2















Comments
Post a Comment