Wednesday, February 3, 2016

Ansible Modules


Until now we have seen playbooks, variables and inventories we will now move to the modules. Ansible provides more than 200 modules under the top level modules like system, Network, Datbase, Files and many others.

Ansible comes with modules (also called as module library) that can be executed directly on remote hosts or through playbooks. Ansible allows users to write their own modules. These helps in performing various operations from starting services, installing packages to executing commands.

In this article we will see a couple of most used modules.

Command – Command module allows us to execute commands on the remote machine. A simple play book for the Command module is,

[root@vx111a inventory]# cat command.yml
---
- hosts: dev
  tasks:
   - name: Check for Command ls
     command: /bin/echo Hello

Upon running we can see the below output,

[root@vx111a inventory]# ansible-playbook command.yml

PLAY [dev] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.202.96]

TASK: [Check for Command ls] **************************************************
changed: [172.16.202.96]

PLAY RECAP ********************************************************************
172.16.202.96              : ok=2    changed=1    unreachable=0    failed=0  

Raw – This module is used when there is more need than Command module or if the command module does not support the operation. This module makes a SSH to the remote machine and run the command. For the Ansible to work we need to have Python available but for this module we don’t need a Python to be available. A simple playbook looks as,

[root@vx111a inventory]# cat raw.yml
---
- hosts: dev
  tasks:
   - name: Install VIM
     raw: yum -y install vim-common

[root@vx111a inventory]# ansible-playbook raw.yml

PLAY [dev] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.202.96]

TASK: [Install VIM] ***********************************************************
ok: [172.16.202.96]

PLAY RECAP ********************************************************************
172.16.202.96              : ok=2    changed=0    unreachable=0    failed=0  

Script – The third module is the Script module which helps in executing scripts on the more machine. This module will copy the script to the remote machine and will execute. A simple playbook looks as

Write a list_dir.sh script file with below contents as,
ls -l /etc | egrep '^d' | wc –l

Now write the playbook as,
[root@vx111a inventory]# cat script.yml
---
- hosts: dev
  tasks:
   - name: Count Number of Dir
     script: list_dir.sh /etc
     register: out

   - debug: var="{{ out.stdout }}" 

The output when we execute that will be,

[root@vx111a inventory]# ansible-playbook script.yml

PLAY [dev] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.202.96]

TASK: [Count Number of Dir] ***************************************************
changed: [172.16.202.96]

TASK: [debug var="{{ out.stdout }}"] ******************************************
ok: [172.16.202.96] => {
    "var": {
        "153\n": "153\n"
    }
}

PLAY RECAP ********************************************************************
172.16.202.96              : ok=3    changed=1    unreachable=0    failed=0  

Shell – The Shell module also allows you to execute commands on the remote machine. The major difference between the Command and the Shell modules are that the Shell module uses a shell (/bin/bash) to run commands. We can get all the shell variables and use other shell features. A simple play book includes,

[root@vx111a inventory]# cat shell.yml
---
- hosts: dev
  tasks:
   - name: create File
     shell: /bin/touch /tmp/hai
     register: out

   - debug: var="{{ out.stdout }}"


[root@vx111a inventory]# ansible-playbook shell.yml

PLAY [dev] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [172.16.202.96]

TASK: [create File] ***********************************************************
changed: [172.16.202.96]

TASK: [debug var="{{ out.stdout }}"] ******************************************
ok: [172.16.202.96] => {
    "var": {
        "": ""
    }
}

PLAY RECAP ********************************************************************
172.16.202.96              : ok=3    changed=1    unreachable=0    failed=0  

File – There are certain modules available in Ansible that helps in managing files, directories etc. One such module is File. The file module allows you to change the attributes of a file. It can touch a file, create or delete recursive directories, and create or delete symlinks.

A simple playbook includes setting a file /tmp/hai with 644 permissions and creating a directory in tmr in /tmp

---
- hosts: dev
  tasks:
   - name: File Permissions
     file: dest=/tmp/hai mode=0644

   - name: Create Directory
     file: dest=/tmp/tmr state=directory

Copy – Another module in managing file is Copy module. A Copy module allows in copying file to the remote location. A Simple example yml file is

[root@vx111a inventory]# cat copy.yml
---
- hosts: dev
  tasks:
   - name: Copy File
     copy: src=./shell.yml dest=/tmp

Copies a file shell.yml to the /tmp location on remote machine.

Template – Template module is a very important module provided by Ansible. Templating is nothing but creating file on the machine. Ansible uses Jinja2 templating language for creating templates based on the Django Framework.

Create a test file as
[root@vx111a inventory]# cat test
This is a test file in {{ ansible_hostname }}

Now lets copy the test file to the remote system using the below playbook as,

[root@vx111a inventory]# cat template.yml
---
- hosts: dev
  tasks:
   - name: Create a Test Template
     template: src=test dest=/tmp/testFile mode=0644

Once we execute the playbook we can see that the file is not just moved but the variable “Ansible_hostname” is replaced with the Hostname variable.

Another option provided by template is the validate parameter that allows you to run a command to validate the file before copying it. This is like a hook that Ansible provides to make sure files that might break the service are not written. 

A simple playbook for copying a conf file to the HTTPD location and validating it before copying that look as,

[root@vx111a inventory]# cat template1.yml
---
- hosts: dev
  tasks:
   - name: Create a Virtaul Host and Validate
     template: src=test.conf dest=/etc/httpd/conf.d/test.conf validate="httpd -t %s"

Upon executing the Playbiook it throws an error as Validation failed.

That’s all for now. We will be seeing more modules along with examples in the next articles.

No comments :

Post a Comment