In this project we are creating Jenkins CI CD of git > github > jenkins > maven to build and send .war file > to ansible server and deploy .war file with playbook > to tomcat 8 server.
Prerequisites
- Ansible server
- Jenkins with Maven Server
- Tomcat Server
- Install Publish Over SSH plugin
- Enable connection between Ansible and Jenkins
Create SSH key and copy to Tomcat server
- Login to tomcat user and create
ansadmuser, set password and add to sudo
sudo useradd ansadm
sudo passwd ansadm
sudo echo "ansadm ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers- Now login to
Ansibleserver generateSSH keyand copy totomcatserver
ssh-keygen
ssh-copy-id ansadm@<tomcat-server_IP>Add tomcat server details to /etc/ansible/hosts (if you are using other hosts file update server info there)
echo "<tomcat-server_IP>" >> /etc/ansible/hostsCreate Playbooks
- Create dicrectory
playbookson/optmkdir /opt/playbooks
- Create a
copywarfile.ymlplaybook file under/opt/playbooksdirectory on Ansible server# copywarfile.yml --- - hosts: all become: true tasks: - name: copy jar/war onto tomcat servers copy: src: /opt/playbooks/webapp/target/webapp.war dest: /usr/tomcat8/webapps
Login to Jenkins Dashboard >> Manage Jenkins > Manage Plugins > Available > Publish over SSH
Enable connection between Ansible and Jenkins
Login to Jenkins Dashboard >> Manage Jenkins > Configure System > Publish Over SSH > SSH Servers
- SSH Servers:
- Name :
ansible_server - Hostname:
<ServerIP> - username:
ansadm - password:
*******
- Name :
Test the connection by clicking Test Connection and save
Jenkins CI CD new item Setup
Create Jenkins job, Fill the following details,
Login to Jenksing Dashboard > click New Item
- Enter item name:
sample devops project 2 - Source Code Management:
- Repository:
https://github.com/maheshkn400/hello-world.git- Branches to build :*/master - Build:
- Root POM:
pom.xml - Goals and options :
clean install package
- Root POM:
- Add post-build steps
- Send files or execute commands over SSH
- SSH Server :
ansible_server - Source fiels:
webapp/target/*.war - Remote directory:
//opt//playbooks
- SSH Server :
- Send files or execute commands over SSH
- Add post-build steps
- Send files or execute commands over SSH
- SSH Server :
ansible_server - Exec command:
ansible-playbook /opt/playbooks/copywarfile.yml
- SSH Server :
- Send files or execute commands over SSH
Run the job and you should be able to seen build has been deployed on Tomcat server.
I'm Happy To Get Suggestions 😄
