HomeAutomation Tools

Part #1 : Getting Started with Ansible Ad-Hoc Commands

Part #1 : Getting Started with Ansible Ad-Hoc Commands
Like Tweet Pin it Share Share Email
Personal Development Category (English)728x90

This guide will help you to start your journey of Automation with Ansible. Here I am going to discuss Ansible Ad-Hoc Commands. In the previous articles, I talked about Ansible Inventories and how to configure with deferent environments like Local Infra with variable hosts and posts and also Public cloud managing with Dynamic inventory for AWS EC2.

In this article, I am going to talk about some useful commands which will help you to get started with ansible Ad-Hoc Commands to get hands-on practice with Ansible.Ansible Ad-Hoc Commands

Ansible Ad-Hoc command is something that you might type in to do something really quick, but don’t want to save for later.

This is a good place to start to understand the basics of what Ansible can do prior to learning the playbooks language – ad-hoc commands can also be used to do quick things that you might not necessarily want to write a full playbook for.

Source info: http://docs.ansible.com/ansible/latest/intro_adhoc.html

Suggestable Read:

1. Introduction and Installation & Configuration of Ansible 2.3 on CentOS/Redhat Linux 7

2. Configuring Ansible Dynamic Inventory for AWS EC2

3. Creating an Ansible static inventory with variables

Let’s start with knowing the correct syntax to run the Ansible Ad-Hoc Commands.

Some useful tips to run Ansible Ad-Hoc commands.

call a shell command:

-a “$command”

call with sudo rights:

–sudo

test if machine responses:

-m ping

call an arbitrary module:

-m $module -a “$argument”

gather specific facts:

-m setup -a “filter=*distri*”

Before executing the Ad-Hoc commands it is very important to know about Ansible Patterns that might help you to know where you’re going to run the Ansible-Hoc commands.

Patterns: You can use Ansible patterns to select the specific set of hosts/groups.

You can use the ansible help command to understand the Ad-Hoc command syntax and more.

Ansible Ad-Hoc Commands

(or)

 

This command will list out all the hosts which are configured in Static Inventory (/ete/ansible/hosts) file

 

(or)

You can also use wildcard character (*) to collect the same information

 

If you want to check the hosts underline on your one of the groups

 

This command will give you the output which is matching hostname with “vagrant”

 

 

Ansible Ad-Hoc Commands:

Suggestable Read: Check the list of all module

Run your first Ansible command on your Ansible server command line interface.

The below command will check the “uptime” in the group called “all”.

 

Explanation about the above command:

ansible: Ansible is the command to execute the Ad-Hoc Commands on the Shell environment.

all : This is a Group/hostname that you want to execute the commands.

-i : INVENTORY, –inventory-file=INVENTORY. specify inventory host path (default=/etc/ansible/hosts) or comma separated host list.

-m : Module name – That you want to execute on the remote servers. (default=command)

command : The Command module will execute the Commands on Remote Servers. It won’t support to do extra tasks like filtering the strings from command output. Check the Manual page ( [[email protected] ~]$ ansible-doc command ) for more info.

-a : MODULE_ARGS, –args=MODULE_ARGS-  module arguments.

“Uptime”: This is a Unix Command to get the server uptime details.

 

Some more Quick hands on Ansible Modules and Adhoc Commands.

Check reachability of all the target systems

 

Check detailed information about the target machines

 

Execute ad-hoc commands on the target machines

-s means command will be executed with sudo permissions on remote servers.

List hosts on which command will be executed

 

Copy file from Ansible server to target machines using the copy module

 

Let’s look what Ansible is doing in the background

– vvv is the verbose

Ansible Ad-Hoc Commands

 

Conclusion:  I hope, you’re enjoying with reading this article. This is concluding here. I will come up with some more basic Ad-Hoc commands that can help you to reach next level to write the Ansible playbooks. If it helps you or if you want to suggest something on this please do comment on below. I will accept your feedback. Happy Automation with Ansible.

Follow our Youtube Channel for more about Ansible Tutorials :

Check out the below Video for More about Ansible Basic and Ad-Hoc Commands.

Comments (1)

  • Good day! This post could not be written any better!
    Reading through this post reminds me of my old room mate!
    He always kept talking about this. I will forward this article to him.
    Pretty sure he will have a good read. Many thanks for sharing!

    Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.