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
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.
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
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
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
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
Hope this was informative. Thanks!