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

How to do Installation of K8S using kubeadm?

Till now I have given blogs on k8s objects, services, namespaces, ingress etc. but where to execute ...

Read More >

Webinar -The curious case of native versus the cross platform development of mobile apps

The curious case of native versus the cross platform development of mobile appsBusiness is often cau...

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 >

THE RAZOR VIEW ENGINE IN MVC

View Engine is responsible for rendering the view into html form to the browser.ASP.NET MVC includes...

Read More >

Microsoft Azure : Running Windows Virtual Machine

How easy it is to create an Azure VM in just a few minutes. We'll use Windows Server 2012 r2 Dat...

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 >

Using XMPP and Openfire server in ASP.NET C# - Part -1

Extensible Messaging and Presence Protocol (XMPP)The Extensible Messaging and Presence Protocol (XMP...

Read More >

Using XMPP and Openfire server in ASP.NET C# - Part -2

Roster in XMPPRoster are the contacts of  user (contact list). Roster Handlers in AgsXmppL...

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 >

What's New In C# 6.0?

Microsoft has released the preview version of Visual Studio 2015 and .NET 4.6 for developers to try ...

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
  • *
  • *