Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

10 Easy-to-Use Modules in Ansible

DZone's Guide to

10 Easy-to-Use Modules in Ansible

Learn about the most common modules in Ansible playbooks, what they do, and how to use them.

· DevOps Zone ·
Free Resource

Learn how integrating security into DevOps to deliver "DevSecOps" requires changing mindsets, processes and technology.

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.

  1. test-servers: 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: defines 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 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 Module

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.

Setup Module

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.

Copy Module

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

Yum Module

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 -s.

Shell Module*

When we want to run UNIX commands then we use shell module

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

https://gist.github.com/slathia15/be3f84fa101ab39fb0d1969b8a99fe5d

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

Service Module

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

https://gist.github.com/slathia15/339cc8f6784bdec5037481f7dc225bbb

Apache2 is up on my machine.

Debug Module

To print a msg on hosts we use Debug module.

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

https://gist.github.com/slathia15/d408ac54c5cc1cddbf07d6b14abcaa3b

Hello, a message is printed on my machine.

Template Module

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.

Include Module

When we want to include another playbook in our playbook, then we use the Include module.

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.

I hope this blog was useful. For more Ansible modules, we can refer to the documentation.

Learn how enterprises are using tools to automate security in their DevOps toolchain with these DevSecOps Reference Architectures.

Topics:
devops ,ansible ,tutorial ,automation ,configuration management ,provisioning

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}