In this pipeline implementation, we will perform a Complete CI/CD automation using Ansible as automation engine, installation of Java,Maven and Tomcat, configuring them, building the code and then deploying it to the Tomcat server. All this is done using the Ansible Roles and playbooks.
For this pipeline implementation, we will be using vagrant as our production machine, ansible engine to run the whole automation. The automation includes
Configuring the Java, maven and tomcat server, configuring the bashrc with the Environment variables then checking out the source code from the Github Repository, building the source code using the maven tool and then creating a war file from the source code. The final step is to copy the war file to the tomcat server and then starting the server.
Once the automation is complete, all we have to do is to check the application on the http:<IP>:8084.
1. Configure the Vagrant machine
For this automation purpose, we will use a vagrant centos machine with an Ip address. The ip address is very important as we will use this ip address in ansible to automate. The Vagrant configuration looks as below,
jagadishmanchala@Jagadish-Local:/Volumes/Work$ cat Vagrantfile
Vagrant.configure(2) do |config|
config.vm.box = "CentOS2"
config.vm.box_url = "https://saleseng.s3.amazonaws.com/boxfiles/CentOS-6.3-x86_64-minimal.box"
config.vm.host_name = "dev.foohost.vm"
config.vm.network "private_network", ip: "172.16.202.96"
config.vm.provider :virtualbox do |vb|
vb.name = "foohost"
end
end
In the above configuration we are using the Centos 6 with the Ip address “172.16.202.96”. Create a file called “Vagrantfile” and copy the above content. Run the “vagrant up” command to initialize the vagrant virtual machine. Once the machine is up and running, we can then run the “vagrant ssh” command to login to the machine. We can move to the root user using the “sudo su” command.
2. Configure the Ansible engine with the vagrant virtual machine.
Configure the ssh communication between the ansible control machine and the newly created vagrant machine. This is done by creating the ssh keys on the control machine, copying them over to the vagrant box. Follow the page here to configure the vagrant machine and the communication between the control machine and the vagrant machine.
3. Check out the Ansible role code from the github repository over here. Once the code is checked out, run a tree command and the directory structure looks like below,
jagadishmanchala@Jagadish-Local:/Volumes/Work$ tree cicd/
4. Understanding the Ansible Code
Ansible role has a specific directory structure. Create a directory “cicd” and create directories files, handlers, tasks, templates and vars inside the cicd directory. Each directory has a special purpose in the ansible engine.
Files - The files directory needs to have a java tar file. download the jdk-8u144-linux-x64.tar.gz to this location. We will be copying the jdk file to the remote machine. The same file will be extracted on the remote machine and configured.
Vars - The vars directory contains the main.yml file which will contain the variables that we will use in the ansible role. The variables include
tomcat_version: 7.0.81
http_port: 8084
maven_version: 3.5.0
Templates - the templates directory includes 2 files, server.xml and tomcat-users.xml. Both files are tomcat file where server.xml contains the port that we will access the application, the port over here is 8084. So we will access the final deployed war file using “http://<ip>:8084”.
The other file tomcat-users.xml file containers the username and password that we will use to login to the tomcat server. The credentials are tomcat/tomcat.
Both these files will be copied to the remote machine to the tomcat directory.
Handlers - The handlers directory include the main.yml which contain the script to start the tomcat server. We will be running the startup.sh script using the handler code. The code contains the script “nohup /usr/local/tomcat/bin/startup.sh &”
Tasks - This is the most important directory which contains the whole bunch of scripts. Below are the scripts,
main.yml - This is the script that gets triggered when we run the ansible role. The script includes all the other scripts below,
configure_git.yml - script that configures git and checkout the source code.
configure_java.yml - script that configures java copied from the files directory to the remote location /usr/local/jdk path.
configure_maven.yml - script that download maven, extracts to a /usr/local/maven location.
configure_tomcat.yml - script that download tomcat, extracts to a /usr/local/tomcat location.
update_path.yml - This includes script that will configure the .bashrc file with necessary environment variables include JAVA_HOME, MAVEN_HOME and TOMCAT_HOME and other variables.
Finally playbook.yml, a file that sits beside the cicd directory which ansible uses to trigger this cicd role. The code includes
jagadishmanchala@Jagadish-Local:/Volumes/Work/cicd$ cat playbook.yml
---
- hosts: all
gather_facts: True
become: yes
become_user: vagrant
roles:
- { role: tomcat }
We can see that the role tomcat is defined in the roles.
Run the Playbook using the “ansible-playbook playbook.yml”. The whole source code is available here.
For this pipeline implementation, we will be using vagrant as our production machine, ansible engine to run the whole automation. The automation includes
Configuring the Java, maven and tomcat server, configuring the bashrc with the Environment variables then checking out the source code from the Github Repository, building the source code using the maven tool and then creating a war file from the source code. The final step is to copy the war file to the tomcat server and then starting the server.
Once the automation is complete, all we have to do is to check the application on the http:<IP>:8084.
1. Configure the Vagrant machine
For this automation purpose, we will use a vagrant centos machine with an Ip address. The ip address is very important as we will use this ip address in ansible to automate. The Vagrant configuration looks as below,
jagadishmanchala@Jagadish-Local:/Volumes/Work$ cat Vagrantfile
Vagrant.configure(2) do |config|
config.vm.box = "CentOS2"
config.vm.box_url = "https://saleseng.s3.amazonaws.com/boxfiles/CentOS-6.3-x86_64-minimal.box"
config.vm.host_name = "dev.foohost.vm"
config.vm.network "private_network", ip: "172.16.202.96"
config.vm.provider :virtualbox do |vb|
vb.name = "foohost"
end
end
In the above configuration we are using the Centos 6 with the Ip address “172.16.202.96”. Create a file called “Vagrantfile” and copy the above content. Run the “vagrant up” command to initialize the vagrant virtual machine. Once the machine is up and running, we can then run the “vagrant ssh” command to login to the machine. We can move to the root user using the “sudo su” command.
2. Configure the Ansible engine with the vagrant virtual machine.
Configure the ssh communication between the ansible control machine and the newly created vagrant machine. This is done by creating the ssh keys on the control machine, copying them over to the vagrant box. Follow the page here to configure the vagrant machine and the communication between the control machine and the vagrant machine.
3. Check out the Ansible role code from the github repository over here. Once the code is checked out, run a tree command and the directory structure looks like below,
jagadishmanchala@Jagadish-Local:/Volumes/Work$ tree cicd/
cicd/
├── playbook.yml
└── tomcat
├── files
│ └── text
├── handlers
│ └── main.yml
├── tasks
│ ├── configure_git.yml
│ ├── configure_java.yml
│ ├── configure_maven.yml
│ ├── configure_tomcat.yml
│ ├── main.yml
│ └── update_path.yml
├── templates
│ ├── server.xml
│ └── tomcat-users.xml
└── vars
└── main.yml
4. Understanding the Ansible Code
Ansible role has a specific directory structure. Create a directory “cicd” and create directories files, handlers, tasks, templates and vars inside the cicd directory. Each directory has a special purpose in the ansible engine.
Files - The files directory needs to have a java tar file. download the jdk-8u144-linux-x64.tar.gz to this location. We will be copying the jdk file to the remote machine. The same file will be extracted on the remote machine and configured.
tomcat_version: 7.0.81
http_port: 8084
maven_version: 3.5.0
Templates - the templates directory includes 2 files, server.xml and tomcat-users.xml. Both files are tomcat file where server.xml contains the port that we will access the application, the port over here is 8084. So we will access the final deployed war file using “http://<ip>:8084”.
The other file tomcat-users.xml file containers the username and password that we will use to login to the tomcat server. The credentials are tomcat/tomcat.
Both these files will be copied to the remote machine to the tomcat directory.
Handlers - The handlers directory include the main.yml which contain the script to start the tomcat server. We will be running the startup.sh script using the handler code. The code contains the script “nohup /usr/local/tomcat/bin/startup.sh &”
Tasks - This is the most important directory which contains the whole bunch of scripts. Below are the scripts,
main.yml - This is the script that gets triggered when we run the ansible role. The script includes all the other scripts below,
configure_git.yml - script that configures git and checkout the source code.
configure_java.yml - script that configures java copied from the files directory to the remote location /usr/local/jdk path.
configure_maven.yml - script that download maven, extracts to a /usr/local/maven location.
configure_tomcat.yml - script that download tomcat, extracts to a /usr/local/tomcat location.
update_path.yml - This includes script that will configure the .bashrc file with necessary environment variables include JAVA_HOME, MAVEN_HOME and TOMCAT_HOME and other variables.
jagadishmanchala@Jagadish-Local:/Volumes/Work/cicd$ cat playbook.yml
---
- hosts: all
gather_facts: True
become: yes
become_user: vagrant
roles:
- { role: tomcat }
We can see that the role tomcat is defined in the roles.
Run the Playbook using the “ansible-playbook playbook.yml”. The whole source code is available here.
No comments :
Post a Comment