10 Easy To Use Modules In Ansible

Reading Time: 2 minutes

Ansible is all about using modules in its playbook.

Here are my top 10 commonly used modules:-

Before reading this blog I would like to explain some terminologies used below:-

  1. test-servers:- It is the group of my hosts which I have written in my inventory file.

2. – m XYZ:- XYZ is the module name

3. -u ec2-user:- to define username that is ec2-user

4. SUCCESS:-  means that my module has done its task on destination nodes.

5. Changed:-  If it is true something is changed on destination nodes.

If it is false something is not changed on destination nodes.

Let’s start with modules:-

  1. ping module:- Used when we want to check whether the connection with our hosts defined in my inventory file is established or not.

Command :- ansible test-servers -m ping -u ec2-user

ping changes to pong which means ssh connection is established.

2. Setup module:- Used when we want to see the information of our all the hosts their configuration and detailed information.

Command :- ansible test-servers -m setup -u ec2-user

This is the snapshot of the configuration of my machine running on AWS.

3. Copy module:- Often used in writing playbooks when we want to copy a file from remote server to destination nodes.
Example:- Suppose we want to copy a file from a remote server to all destination machines.

Command :- ansible test-servers -m copy -a 'src=/home/knoldus/Personal/blogs/blog3.txt dest=/tmp' -u ec2-user

4. Yum module:- To install a service we use Yum module

Command:- ansible test-servers -m yum -a 'name=httpd state=present' -become -u ec2-user

Apache2 will be installed in our machines.

The key point to note here is that we have to use -become which is new in 2.6 version before we have to use -s.

5. Shell module:- When we want to run UNIX commands then we use shell module

Command:- ansible test-servers -m shell -a 'ls -la' -u ec2-user

This will display all the files present in our machine with their permissions.

6. Service module:- When we want to ensure the state of a service that is service is running we use the service module.

Command:- ansible test-servers -m service -a 'name=httpd state=started' -become -u ec2-user

Apache2 is up on my machine.

7.Debug module:- To print a msg on hosts we use Debug module.

Command:- ansible test-servers -m debug -a 'msg=Hello' -u ec2-user

Hello, a message is printed on my machine.

8. Template module:- It 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.

9. Include module:– When we want to include another playbook in our playbook then we use the Include module.

10. User 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.

Written by 

Sachin is a software consultant having more than 0.5 years of experience. He loves to learn trending technologies. Sachin is familiar with languages such as Java, Scala and is currently working on DevOps technologies /tools like Kubernetes, Jenkins, Ansible, Docker, Bamboo.