Jenkins-Ansible Integration
----------------------------------------
1 - Jenkins and Ansible Master
1 - Ansible Node
# sudo su -
# apt-get update
# apt-get install openjdk-11-jdk -y
# apt-get install tomcat9 -y
# apt-get install git-core -y
# apt-get install ant -y
# apt-get install maven -y
#vi /etc/tomcat9/server.xml <Connector port="9090"
Google ---> jenkins download
# curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
# apt-get update
# apt-get install fontconfig openjdk-11-jre
# apt-get install jenkins -y
Jenkins --> select "Manage Jenkins" --> "Global tool configuration"
# apt-get update
# apt-get install software-properties-common -y
# apt-add-reposotiry ppa:ansibe/ansible
# apt-get update
# apt-get install ansible -y
# ansible -version
[node] 172.31.36.172
# ansible all -m ping
172.31.36.172 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" }
node# passwd root
Enter new passwd : root
node# vi /etc/ssh/sshd_config
[press 'i' for insert mode]
PermitRootLogin yes
PasswordAuthentication yes
[press 'Esc']
[:wq ---> write and quit]
node# service ssh restart (for Ubuntu nodes)
[OR]
node# systemctl restart sshd (for Redhat nodes)
node# hostname -i (to get an Private IP-Address)
[OR] # ifconfig
[OR] # ip a
node# curl ifconfig.me (to get an Public IP-Address)
[OR] curl ifconfig.in
node# hostname -f (to get a Host Name / FQDN)
mstr# ssh <node-IP>
Password less Authentication (SSH-Keys)
-------------------------------------------------------------
mstr# ls -a (to list hidden files)
mstr# cd .ssh
mstr# ssh-keygen
mstr# ls
id_rsa (private key)
id_rsa.pub (public key)
mstr# ssh-copy-id <node IP Address>
mstr# ssh <node IP>
Jenkins ---> Manage Jenkins ---> "Plugins" ---> Available -->
search for "Ansible Plugin" --> install without restart
Jenkins ---> Manage Jenkins ---> "tools" --> select "Ansible"
Name: Ansible
Path to ansible executables directory: /usr/bin
# mkdir playbooks
# vi pb1.yml
-
hosts: all gather_facts: true become: true tasks:
-
name: to install apache apt: name=apache2 update_cache=yes state=present changed_when: true
-
name: to deploy file copy: src: /opt/index.html dest: /var/www/html/index.html changed_when: true
-
name: to start service service: name=apache2 state=started changed_when: true
-
Jenkins ---> New Item --> "Free style project"
Build steps --> select "Invoke Ansible Playbook" -->
Ansible installation: Ansible
Playbook path: /root/playbooks/pb1.yml
Inventory: File or host list --> /etc/ansible/hosts
Host subset: web
Credentials: xxxxx
Started by user admin Running as SYSTEM Building on the built-in node in workspace /var/lib/jenkins/workspace/JENKINS-ANSIBLE-PROJECT [JENKINS-ANSIBLE-PROJECT] $ /usr/bin/ansible-playbook /opt/pb1.yml -i /etc/ansible/hosts -l web -f 5 --private-key /tmp/ssh15539247242160740393.key -u root
PLAY [all] *********************************************************************
TASK [Gathering Facts] ********************************************************* ok: [172.31.7.40]
TASK [to install apache] ******************************************************* changed: [172.31.7.40]
TASK [to deploy file] ********************************************************** changed: [172.31.7.40]
TASK [to start service] ******************************************************** changed: [172.31.7.40]
PLAY RECAP ********************************************************************* 172.31.7.40 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Finished: SUCCESS