Ansible Ad-Hoc Commands and Playbooks - Blogs

Ansible Ad-Hoc Commands and Playbooks

An ansible ad-hoc command is a one-line command that lets you perform basic tasks efficiently without writing playbooks. An Ansible ad-hoc command uses ansible command-line tool to automate a single task on one or more managed nodes. Ad-hoc commands are quick and easy, but they are not reusable. Ad-hoc commands demonstrate the simplicity and power of Ansible. 

Ansible ad hoc commands are written for a very specific task. Some of the ad hoc tasks include ping and validating whether the server is alive and responds, copying files between hosts, rebooting servers, installing packages, getting remote hosts' disk space, knowing 1 to 200 remote servers uptime and shutting down multiple remote hosts on a single command, etc.

In this tutorial, we are going to discuss various tasks of Ansible Ad-Hoc commands and their use cases.


Why learn first about the ad-hoc commands?

Ad-hoc commands are great for those tasks that you rarely repeat. For example, if you want to turn off all the machines in your lab for a vacation, you might be able to execute a quick one-liner in Ansible without a playbook.


Ansible Ad-Hoc Commands:

            The syntax of ad-hoc commands to run is as follows and see some of the ad-hoc commands as well.

$ ansible [pattern] -m [module] -a "[module options]"


$ ansible-doc l: This will display all available modules in ansible.

$ ansible-doc yum: This will show more information about the module 'yum' along with examples.

$ ansible all -m ping: This will ping all the servers that we listed in the inventory file (i.e. /etc/ansible/hosts).

$ ansible all -m ping -o: The same output of the above command will be shown in single line.


$ ansible all -m shell -a ‘uptime’: This will show all the machines uptime. Here 'm' is node, and 'a' is statement. Here ‘m’ means module and ‘a’ means argument.


$ ansible all -a ‘uptime’:  The same output of the above command will be shown.


$ ansible all -m shell -a ‘date’: It shows the date of all machines.

$ ansible all -m shell -a ‘cat /etc/*release’: It displays the details of all the machines released in Redhat.

$ ansible all -m shell -a ‘service sshd status’: This will check the status of the 'sshd' service on all machines.

$ ansible dbservers -a ‘df -h’: This will check the disk space utilization of all nodes in the group of dbservers.

$ ansible webservers -a ‘free -m’: This will display the total amount of free space available along with the amount of memory used and swap memory in the system of all nodes in the group of webservers.

$ ansible all -m yum -a "name=httpd state=present": This will install the Apache web server on all machines listed in the inventory file.


$ ansible all -m yum -a "name=httpd state=present": This will install the Apache web server on all machines listed in the inventory file.

I discussed a few ansible commands in this article but there are plenty of ad hoc commands available in the ansible tool, but the thing is that we have to pick the command according to our use. I hope this will be useful to you. If we face any access issue, then we need to provide sudo access to ansible admin user in all client machines / systems. To do this, we have to literally edit the /etc/sudoers file and add the user of the ansible admin in the /etc/sudoers file, as shown below.

# vim /etc/sudoers

admin_user ALL=NOPASSWD: ALL

Ansible Playbooks:

Playbooks are nothing but files consisting of your written code, and they are written in YAML format, which defines the tasks and executes them through the Ansible. Playbooks may include one or more plays. Plays defines a set of activities or tasks to be run on hosts of inventory file.

Let's see as follows one sample playbook for installing an apache webserver in Linux machine.

Filename: sample.yaml


- name: install Apache webserver

  hosts: webservers


       - name: install httpd


          name: httpd 

          State: latest


                We need to strictly follow the syntax suggested by ansible, otherwise it will throw the error. Let's see in the following, some of the suggestions made by ansible.

  • Playbooks always start in a YAML file with three dashes.
  • Items that start with a single dash shall be treated as list items.
  • Whitespaces matters in ansible, so the items are always defined by indentation.


To Run the playbook, we need to execute the command shown as follows.

$ ansible-playbook

E.g.: $ ansible-playbook sample.yaml


                In this document, I took you through one of the sample playbooks, as well as some of the ansible ad-hoc commands. I can say at the end that there's a lot more commands to learn but these are enough to get you started. You can pick up the rest of the commands as you explore more about the ansible configuration management tool.


Entity Framework Code First

Code First development with Entity Framework New approaches to modeling for Entity Framework 4.1. Co...

Read More >

Bulk Export Import Test Cases to Azure DevOps Test Plans using CSV file

As a user of Azure DevOps Test Plans or TFS Test Management for manual testing, from a long time eve...

Read More >

How to enable Lync Addin and create Lync Meetings using the Outlook 2013

 How to enable Lync Addin and create Lync Meetings using the Outlook 20131.   &n...

Read More >

Sonar Integration with TFS for Java Projects

 Sonar Integration with JAVA.1.Download sonar-3.7 and sonar-runner-2.3*** unzip folders and cop...

Read More >

Building a RESTful API using python and Django

What you Need? Python 2.6+ Django 1.5+ django-tastypie  Sqllite3Django is Free and Open Source ...

Read More >

Partnership Program

We understand that the world of technology is changing rapidly from day to day and businesses have m...

Read More >

LDAP Integration with Jenkins

In this document, we are going to see the integration process of LDAP and Jenkins. The default insta...

Read More >

Create a Basic Bot with the Bot Builder SDK for.NET

A bot is a software that is designed to automate the tasks you would usually do on your own, like bo...

Read More >

VSTS Copy Dashboard Tool

The Copy Dashboard tool replicates/copies the existing dashboard from one team to either single or m...

Read More >


Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive

Contact Us
  • *
  • *