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

Microsoft Office 2013 Features and Looks

Now Office 2013 launched recently, now its not available for users may be its available for User aft...

Read More >

How to Sync On-premise AD with Windows Azure AD using Azure AD Connect tool

 Azure AD is a service that provides identity and access management capabilities in the cloud. ...

Read More >

Fortira Exclusive CxO Roundtable at C-SOaP- SUMMIT 2013

img{ display:block; } #outlook a{ padding:0; } body{ width:100% !important; } body{ ma...

Read More >

New Tools in NAV 2013 (the less talked about ones) - Concluding Part

This part (as I said in my previous blog), will contain some more new tools and features focusing on...

Read More >

Examples of Selenium Webdriver Scripts

Examples of Selenium Webdriver ScriptsNow its time to code and execute the selenium webdriver script...

Read More >

LDAP Integration with Sonatype Nexus

Sonatype Nexus supports the Repository Manager integration with various active directory systems for...

Read More >

Webinar - The process of version migration from older versions of Navision to NAV 2013 R2

Please watch LIVE RECORDING Video of the webinar session below which took place on 5th Jun...

Read More >

Create a Virtual Wi-Fi Hotspot for Windows 7 & 8

In this blog, I have added information on how to turn your windows 7 or 8 machine into a Wi-Fi Hotsp...

Read More >

Mobile Operating Systems And Cross Platforms

Current Popular mobile operating systems are:Android from Google Inc.(Open source)initial release :2...

Read More >

Share

Comments

zOyINVDuSFUriRT
6/2/2020 1:39:22 AM | Reply

Post a Comment

  • Recent
  • Popular
  • Tag
Tags
Monthly Archive
Subscribe
Name

Contact Us
  • *
  • *