10 Easy-to-Use Modules in Ansible
Learn about the most common modules in Ansible playbooks, what they do, and how to use them.
Join the DZone community and get the full member experience.Join For Free
Ansible is all about using modules in its Playbook. This article talks about my top ten most used modules.
Before reading this blog, I would like to explain some terminologies used below.
- test-servers: the group of my hosts which I have written in my inventory file.
- - m XYZ: XYZ is the module name.
- -u ec2-user: defines username that is ec2-user.
- SUCCESS: means that my module has done its task on destination nodes.
- Changed: If it is true, something has changed on the destination nodes. If it is false, something has not changed on the destination nodes.
Let's start with the modules:
Ping is used when we want to check whether the connection with our hosts defined in the inventory file is established or not.
ansible test-servers -m ping -u ec2-user
ping changes to pong if an SSH connection is established.
The setup module is used when we want to see the information of all the hosts, their configuration, and detailed information.
ansible test-servers -m setup -u ec2-user
This is a snapshot of the configuration of my machine running on AWS.
The copy module is often used in writing playbooks when we want to copy a file from a remote server to destination nodes.
For example, suppose we want to copy a file from a remote server to all destination machines.
ansible test-servers -m copy -a 'src=/home/knoldus/Personal/blogs/blog3.txt dest=/tmp' -u ec2-user
We use the Yum module to install a service.
ansible test-servers -m yum -a 'name=httpd state=present' -become -u ec2-user
Apache2 will be installed on our machines.
The key point to note here is that we have to use
-become, which is new in version 2.6; before, we had to use
When we want to run UNIX commands then we use shell module
ansible test-servers -m shell -a 'ls -la' -u ec2-user
This will display all the files present in our machine with their permissions.
When we want to ensure the state of a service that is service is running we use the service module.
ansible test-servers -m service -a 'name=httpd state=started' -become -u ec2-user
Apache2 is up on my machine.
To print a msg on hosts we use Debug module.
ansible test-servers -m debug -a 'msg=Hello' -u ec2-user
Hello, a message is printed on my machine.
The Template module is used to copy a configuration file from the local system to the host server. It is the same as the copy module, but it dynamically binds group variables defined by us.
Here, I have vars in my source machine.
When we want to include another playbook in our playbook, then we use the Include module.
To add a particular user to our module we can use User module. Here, we have added a user named Sachin to our module.
I hope this blog was useful. For more Ansible modules, we can refer to the documentation.
Opinions expressed by DZone contributors are their own.