Some time we
need to automate the packages installation on
multiple server, Here we are about to create Ansible Playbook to install Apache Tomcat server
We believe, You have already installed Ansible if not you need to click on given link to install Ansible on ubuntu server.
Step 1: Configure Ansible
By default ansible host configuration file is /etc/ansible/hosts Here you need to add the tomcat server details like this, Use the following step for the same, In my case i have 1 apache tomcat server.
Group Name - tomcat
IP Address - Your IP Address
Username - example
Password - XXXXXXXXXX
You need to add your server in ansible host file, Syntax is given below, You need to open the /etc/ansible/hosts file with nano or vim text editor.
©sudo vim /etc/ansible/hosts
and add the web server entry like this.
[tomcat]
localhost ansible_connection=ssh ansible_ssh_user=ubuntu ansible_ssh_pass=xxxxxxx
Change the credentials as per your environment, Save and exit from the vim text editor and verify the host machine connectivity with ansible by using the given command.
©©ansible all -m ping
You should get output like this.
Here our host configuration is completed and now we can go for next step to write play book.
Step 2: Creating Apache Playbook
You need to create a tomcat-playbook.yml and you change it according to your requirement.
©©sudo vim tomcat-playbook.yml
Paste the given code.
- hosts: tomcat
become: yes
tasks:
- name: update
apt: update_cache=yes
ignore_errors: yes
- name: Installating JDK.
apt: name=default-jdk state=latest
- name: Adding Group and user for Tomcat.
shell: groupadd tomcat && useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
- name: Installating curl.
apt: name=curl state=latest
- name: Downloading Apache Tomcat tar.
shell: wget http://us.mirrors.quenda.co/apache/tomcat/tomcat-8/v8.5.56/bin/apache-tomcat-8.5.56.tar.gz
args:
chdir: /tmp
- name: Creating Apache Tomcat home directory.
command: mkdir /opt/tomcat
- name: Extracting Apache Tomcat.
shell: tar -xzvf /tmp/apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
- name: Updating permission.
command: "{{ item }}"
with_items:
- chown -R tomcat:tomcat /opt/tomcat
- chmod -R g+r /opt/tomcat/conf
- chmod g+x /opt/tomcat/conf
- name: Creating service for Apache tomcat.
file:
path: /etc/systemd/system/tomcat.service
state: touch
mode: u+rwx,g-rwx,o-x
- name: download foo.conf
get_url:
url: https://raw.githubusercontent.com/aftab70/Apache_Tomcat/master/tomcat_services
dest: /etc/systemd/system/tomcat.service
- name: Deamon reload.
command: systemctl daemon-reload
- name: Starting Apache Tomcat service.
service: name=tomcat state=started
Now we are ready to use the tomcat's PlayBook, To execute the PlayBook you need to use the given command.
©©ansible-playbook tomcat-playbook.yml
No comments:
Post a Comment