DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. 10 Easy-to-Use Modules in Ansible

10 Easy-to-Use Modules in Ansible

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

Sachin Slathia user avatar by
Sachin Slathia
·
Oct. 31, 18 · Tutorial
Like (4)
Save
Tweet
Share
30.80K Views

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.

  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.

Ansible (software) Host (Unix) Machine Template Debug (command) Inventory (library) remote

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Spring Boot, Quarkus, or Micronaut?
  • Real-Time Analytics for IoT
  • Front-End Troubleshooting Using OpenTelemetry

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: