Pages

Monday, February 22, 2016

Ansible Roles- An advanced Example

Share it Please
In the previous article we have seen how we can use the Ansible roles in running the playbook on remote machine. In this article we will see a more advanced example of using Ansible roles. In this article we will see how we can configure a bacic Tomcat server on the remote machine using Roles.

1) Lets Create a Directory Structure as,

[root@vx111a roles]# tree tomcat/
tomcat /
── files
── handlers
── meta
── tasks
── templates
└── vars

2) Lets start creating the variable files first tomcat/vars
[root@vx111a vault]# cat main.yml
version: 8.0.32
http_port: 8084

3) Now lets create the tasks file. We will write 3 tasks files
Configure_java.yml – configure Java for Tomcat Server
Configure_tomcat.yml – Configure Tomcat
Update_path.yml – set JAVA_HOME and add java location to PATH in .bashrc file
Main.yml – this contains all the above files using include

Configure_java.yml
---
 - name: Install Java
   copy: src=jdk-8u65-linux-x64.tar.gz dest=/tmp group=root owner=root mode=0755   

 - name: Unpack JAVA
   unarchive: src=/tmp/jdk-8u65-linux-x64.tar.gz dest=/usr/local/ copy=no

This file is the one that we use for configuring java before tomcat is configured. The Java version 8u65 is downloaded and copied to the same /files location. We will copy this using the COPY module to the remote /tmp location. In the second task we will unarchive the copied java to /usr/local. At this point we have not set any JAVA_HOME or added any thing to the PATH variable.

configure_tomcat.yml
---
- name: Remove Older Tomcat Directories
  file: path=/usr/local/tomcat state=absent
     
- stat: path=/usr/local/
  register: tc

- name: Download Tomcat
  get_url: url=http://a.mbbsindia.com/tomcat/tomcat-8/v8.0.32/bin/apache-tomcat-{{ version }}.tar.gz dest=/tmp mode=0755

- name: Unpack Tomcat
  unarchive: src=/tmp/apache-tomcat-{{ version }}.tar.gz dest=/usr/local/ copy=no
  when: tc.isdir is undefined

- name: Change Tomcat Directory Name
  command: mv /usr/local/apache-tomcat-{{ version }} /usr/local/tomcat

- name: Upload server.xml with modified Changes
  template: src=server.xml dest=/usr/local/tomcat/conf
  notify: restart tomcat
          
- name: Upload tomcat-users.xml with users added
  template: src=tomcat-users.xml  dest=/usr/local/tomcat/conf
  notify: restart tomcat

- name: Install Tomcat init.d Script
  copy: src=tomcat-initscript.sh dest=/etc/init.d/tomcat mode=0755  

- name: Start Tomcat
  service: name=tomcat state=started enabled=yes

- name: Wait for Tomcat to Start on Port 8084
  wait_for: host=172.16.202.96 port={{ http_port }}

This file is used to configure Tomcat once the java installation is done. The file contains the following tasks.
1) Check for tomcat folder and remove them.
2) make sure the location /usr/local is available
3) Download tomcat from Internet
4) Un-archive the tomcat and move to /usr/local/apache-tomcat-version
5) Rename the download apache-tomcat-version to tomcat
6) Upload the server.xml file with port changes that exist in the files location to the remote machine tomcat location.
7) Upload the tomcat-users.xml file with users added
8) Upload the tomcat-initscript.sh to the remote machine /etc/init.d location. This file is used to start and stop tomcat as a service.
9) Wait for the Tomcat to start on Port 8084

The version details are extracted from the variable file that we already configured.

Update_path.yml
---
- name: Set Java HOME
  lineinfile: dest=~/.bashrc regexp='^JAVA_HOME'>
                 line="export JAVA_HOME=/usr/local/jdk1.8.0_65/"

- name: Set JAVA PATH
  lineinfile: dest=~/.bashrc regexp='^JAVA_HOME'>
                 line="export PATH=$M2:$PATH:/usr/local/jdk1.8.0_65/bin"

- name: Source Bashrc
  action: shell source ~/.bashrc

This file is used to update the .bashrc file with the necessary Environment variables. The lineinfile module is used to update the .bashrc with necessary variable information. At last we will be using the source command to update the .bashrc file

Main.yml
---
- include: update_path.yml
- include: configure_java.yml
- include: configure_tomcat.yml

This file is the important file for the tomcat role to run. Ansible-playbook uses the main.yml file for running the tasks defined. We included all the other three files in sequence for running the tasks.

All these files are created under /tomcat/tasks location in the tomcat folder.

4) Now lets create the handlers file for restarting the tomcat service. The main.yml is created under the /tomcat /handlers folder as

Main.yml
---
- name: Restart Tomcat
  Service: name=tomcat state=restarted

6. The next step is creating the template files. As we already know that we used server.xml and tomcat-user.xml for modifying the port and adding users to the tomcat. These 2 files are copied under the /tomcat/templates location.
7. The next step is to copy the files into the /tomcat/files location which needs to be copied to the remote location. The files are the java.tar.gz file and the tomcat-initscript.sh location.

8. The file step is to create the actual playbook file for executing the tomcat role. The main.yml looks as
---
- hosts: cent
  roles:
     - tomcat

Once all the steps are done we can see the directory structure as,
[root@vx111a work]# tree tags/
tags/
── ansible.cfg
── hosts
── tomcat
│  
── files
│   │  
── jdk-8u65-linux-x64.tar.gz
│   │   └── tomcat-initscript.sh
│  
── handlers
│   │   └── main.yml
│  
── meta
│  
── tasks
│   │  
── configure_java.yml
│   │  
── configure_tomcat.yml
│   │  
── main.yml
│   │   └── update_path.yml
│  
── templates
│   │  
── server.xml
│   │   └── tomcat-users.xml
│   └── vars
│       └── main.yml
└──
main.yml

You can check the directory structure as above. Let’s run the role which will run the installation, configuration of Java and tomcat.


By this I hope you got a understaning of Roles in Ansbile.I will add more examples using roles. More to come. Happy learnig J

6 comments :

  1. Hi what is that $M2 in updating the path variable.

    ReplyDelete
  2. Unable to run .bashrc file using above ansible command.

    ReplyDelete
  3. Please provide the sample file contents as well for the /files folder. Like init.d tomcat script , server.xml and tomcat-users.xml

    ReplyDelete
  4. Hi jagadeesh,

    what script needs to be written in tomcat-initscript.sh
    i am not able to know what to add in this
    please help me.

    ReplyDelete
    Replies
    1. Mahesh, Iam sorry for late response. is the Issue solved ?

      Delete
  5. Use this Github Repo for all the code - https://github.com/jagadish12/ansible-roles.git

    and its free to use the code :-), Hope this will help

    ReplyDelete