Canarys | IT Services

Blogs

Ansible Ad-Hoc Commands and Playbooks

Date:
Author:
Share

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.

1

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

Or

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

2

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

3

$ 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

  tasks:

       – name: install httpd

         dnf: 

          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 <playbook_filename>

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.