Part 6 – Ansible Modules

Modules are packages that can perform a set of operations on remote hosts based on the arguments given. Without knowing much about modules, we have already used a “ping” module to test our ansible lab configuration. Without any further do, let’s understand Ansible modules in a bit detail.

Most of the modules are packaged with Ansible and they are updated with every release and custom modules can also be built and uploaded for community user to use, however custom modules are not supported by Ansible. There are some hundreds of modules that come with Ansible and mentioning all of them here practically impossible. To view the list of modules that are available use the below command.

                                 # ansible-doc -l

2017-04-04 15_04_49-vagrant@ansi_ ~_my_ansible

To view about a particular module just give the name of the module at the end. This disaplay the mandatory fields that needs to be mentioned while using the module. Below is the example of the yum module which is more used. Take a close look at this and you will know how to use it.

2017-04-04 15_08_25-vagrant@ansi_ ~_my_ansible

Some of the common modules used in day to day configuration management are

  • copy        – copies file from local machine to remote machine
  • service   – manages services like start, stop and enable
  • yum        – package manager for linux machines like RHEL, CentOS
  • apt          – package manager for debian based linux machines
  • setup      – gathers facts about the machines
  • docker   – docker container life cycle management

Let’s use some modules to get some better understanding. Let’s say i want to use my vagrant machine named web to have apache installed. You may have to modify your inventory file accordingly to reflect the names in your infrastructure.

$ ansible web -i my_inventory -m yum -a "name=httpd state=present" --sudo

2017-04-04 15_33_52-vagrant@ansi_ ~_my_ansible

How about starting the service using ansible? We will just use the service module.

$ ansible web -i my_inventory -m service -a "name=httpd enabled=yes state=started" --sudo

2017-04-04 15_39_26-vagrant@ansi_ ~_my_ansible

Similarly, to copy a file to all the systems, we use the copy module. The copy module also validates by the checksum and also can be used for backups.

$ ansible group1 -i my_inventory -m copy -a "src=/home/vagrant/my_ansible/file2copy dest=/home/vagrant/" --sudo

2017-04-04 15_49_43-vagrant@ansi_ ~_my_ansible

The setup module is a special type of module that gathers facts about the machines and these information can be used for writing plays. Setup modules pulls loads of information about the machines and can be viewed by

$ ansible web -i my_inventory -m setup --sudo

To view sepecific information, say amount of free space on the machines, use

$ ansible group1 -i my_inventory -m setup -a "filter=ansible_memfree_mb" --sudo

2017-04-04 16_04_06-vagrant@ansi_ ~_my_ansible

Hope this was informative. Thanks!

 Part 1 – Introduction to Ansible

Part 2 – Ansible Lab Setup

Part 3 – Understanding our First Ansible Command

Part 4 – More about Inventory File

Part 5 – Ansible Config

Part 6 – Ansible Modules

Part 7 – Ansible Tasks, Plays, PlayBooks

Part 8 – Ansible Play Recap, vars, Notify, Handlers

Part 9 – Ansible Roles

Advertisements

#ansible-doc, #copy, #modules, #service, #yum