Ansible Ad-Hoc Commands and Playbooks - Blogs
X
01Jun

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.

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

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.

Related

Understanding how to create and Install Windows Services in C#.Net

Windows Service in C#:This article is about how to create a Windows Service in C# .net using Visual ...

Read More >

Microsoft Azure : Mobile Services - iOS with JavaScript

We will add cloud-based backend service to an iOS app using Azure Mobile Services.We will create bot...

Read More >

Implementing Google account authentication in ASP.NET MVC

Implementation of Google OAuth in MVC application: Step 1: Create a Google OAuth application &n...

Read More >

Selenium Locators

Selenium webdriver uses locators to find the web elements on web page. The following are the list of...

Read More >

How to add Overlay in Windows phone 8

Windows Phone is windows mobile operating system from Microsoft.In my project, there was a requireme...

Read More >

SSRS(Sql Server Reporting Services) Reports

What is SSRS reports?SSRS stands for SQL Server Reporting services a server based report generation ...

Read More >

How to check network reachability in iOS

What is network reachability? Now, you are all set to program your app on the ios device? Here is on...

Read More >

GitHub - Administration

To understand GitHub, you must first have an understanding of Git. Git is an open-source version con...

Read More >

Autofill RecVariable Name in DynamicsNAV 2015

I discovered something quite interesting couple of week back and I thought it was worth sharing.When...

Read More >

Share

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

Sign Up

  • Recent
  • Popular
  • Tag
Tags
Monthly Archive
Subscribe
Name

Text/HTML
Contact Us
  • *
  • *