So CI(build) part
are done by using Jenkins but CD(deploy) part are done by using Ansible
Create 3 REHL ec2 server (Jenkins_Server ,Tomcat_Server ,
Ansible_Server)
Install the ansible on Ansible server
Now make the password less communication between Tomcat_Server and
Ansible_Server for this create user let say ansadmin in both with this user both server
communicate without any password
Now logging into ansible server and create any user say ansadmin with
this user we are going to run playbook
[root@ip-172-31-82-207 ~]# useradd ansadmin
[root@ip-172-31-82-207 ~]# passwd ansadmin (set the password for this user say
raj123456 to use in Jenkins)
Now add below client ip(tomcat server ip) in host file of master
server(ansible server) to run ansible playbook on client.
[ansadmin@ip-172-31-85-7 ~]$ sudo vi /etc/ansible/hosts
[webservers]
172.31.85.242
Now to check ansible is working run below command
[ansadmin@ip-172-31-85-7 ~]$ ansible all -m ping
172.31.89.37 | SUCCESS => {
"changed": false,
"ping":
"pong"
}
create a copyfile.yml on Ansible server under /opt/playbooks
[ansadmin@ip-172-31-85-7 ~]$ cd /opt
[ansadmin@ip-172-31-85-7 opt]$ sudo mkdir playbooks
[ansadmin@ip-172-31-85-7 opt]$ cd playbooks/
[ansadmin@ip-172-31-85-7 playbooks]$ sudo vi copyfile.yml
-
hosts: all
tasks:
- name: Ansible copy file to remote server
copy:
src: /opt/playbooks/webapp/target/webapp.war
dest: /opt/app/apache-tomcat-8.5.37/webapps
[ansadmin@ip-172-31-85-7 playbooks]$ cat copyfile.yml
Now go to Jenkins and install plugging publish over ssh
after installation do the configuration in Jenkins for this plugging
SSH server name:- you can give any name as per your choice say
here ansible
Hostname:- here you give private ip address of ansible server.
Username:- you give the username whatever you created on ansible
server to run playbook like in ourcase ansadmin
Then click on advance button and click on Use
password authentication, or use a different key then give the password of user
you have created in ansible server for ansadmin like in our case raj123456
Passphrase / Password:-
raj123456
Now logging into ansible server and in the below file change the
yellow part.
[root@ip-172-31-86-168 ~]# vi /etc/ssh/sshd_config
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication
yes
#PermitEmptyPasswords no
#PasswordAuthentication no
[root@ip-172-31-86-168 ~]# service sshd restart
Now in the Jenkins click on bottom Test configuration if you get
Success then our setup are fine.
Now add the below line So that it will
work through ansible playbooks.
- Source fiels: webapp/target/*.war
- Remote directory: //opt//playbooks
ansible-playbook /opt/playbooks/copywarfile.yml
Run the below command in ansible server
[root@ip-172-31-30-43 playbooks]# chown -R
ansadmin:ansadmin /opt/playbooks/
Do
the below two things in tomcat server if you get the error
[root@ip-172-31-42-77 ~]# mv app /opt/ -----move tomcat from app folder to opt
folder
[ansadmin@ip-172-31-42-77 opt]$ chmod 755
app/apache-tomcat-8.5.37/webapps
----give other permission to webapps folder to execute
After that restart the tomcat
[root@ip-172-31-42-77 bin]# ./shutdown.sh
[root@ip-172-31-42-77 bin]# ./startup.sh
No comments:
Post a Comment