Introduction to Ansible modules

Ansible has an extensible and modular architecture in functionalities which are organized by modules. You can use modules directly with playbooks or through ad-hoc commands.

Ansible modules are small pieces of code that perform one function (copying a file, or starting or stopping a daemon, for instance). Ansible comes packaged with about 1,000 modules for all sorts of use cases. You can also extend it with your own modules and roles. Check out the modules list: https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html.

For example, the ping module (http://docs.ansible.com/ansible/ping_module.html) is a test module that connects to the remote host, verifies a usable Python installation, and returns the output pong if the connection with the host is successful.

Using the Ansible command-line tool, we can use the ping module over the two remote nodes. We can use the -m flag to specify the Ansible module we need, and the -all flag for all the hosts/groups in the inventory.

The simplest way to use Ansible is to execute ad-hoc commands. The format of using ad-hoc commands is as follows:

$ ansible <host group> -i <inventory file> -m <module> [-a <argument 1>, ... <argument N>]

For example, if you want to check whether all hosts in your inventory are active, you can use the ping module without using arguments. To verify that all machines available in out inventory are active, we can perform a ping. The -m parameter indicates the Ansible module we are using:

$ ansible all -m ping

We can now use the command-line option to test a specific host:

$ ansible -i hosts 192.168.1.160 -m ping
192.168.1.160 | SUCCESS >> {
"changed": false,
"ping": "pong"
}

If you have no connection with the host, it returns the following error message:

192.168.1.160| UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh.",
"unreachable": true
}

The previous command reads that we will use the host file as the inventory file, and execute the ping module on the 192.168.1.160 host.

We can use Ansible's shell module (http://docs.ansible.com/ansible/shell_module.html) to test a specific group defined in the inventory file:

$ ansible -m shell -a "hostname" test-servers
192.168.1.161 | SUCCESS | rc=0 >>
ansible-node1

For example, if we want to execute a command in all our nodes, we can do the following:

$ ansible all -a "/etc/init.d/apache2 start"

In this way, we have managed to start the Apache service of all the nodes we have previously configured.

Ansible has many modules for all common system administration tasks, such as file management, user administration, and package management. The following command extracts the internal and external IP addresses of all network hosts:

$ ansible all -i hosts -m shell -a '/sbin/ifconfig | grep inet.*Bcast'"
192.168.1.161 | SUCCESS | rc=0 >>
inet addr:10.0.1.10 Bcast:10.0.1.255 Mask:255.255.255.0
inet addr:192.168.1.161 Bcast:192.168.1.255 Mask:255.255.255.0
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset