Ansible ad hoc commands with Examples

In this article we are going to cover Ansible ad hoc commands with examples.

Prerequisites

  • One Ansible master node
  • One or more client nodes
  • SSH connection should be enabled between Ansible master node and the remote client servers.

Why we use Ansible ad hoc Commands ?

An ad hoc commands can be use to do simple quick task to perform some operation at that command line itself. And they are not reusable.

For e.g, if you wants to restart your infrastructure servers or get OS information on small and quick task you can use ad hoc commands.

Syntax:

ansible <group_name> -m <module> -a <arguments>

Test the connection

Before start demonstrating the ad hoc commands example let us first test our connection with Ansible client nodes.

To check my inventory file run the below command:

sudo nano /etc/ansible/hosts

So run the below ad hoc command in ansible server:

ansible all -m ping

Output:

65.0.89.82 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
15.206.172.222 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

If you want to run any specific group then run the below command:

Syntax:

ansible <group name> -m ping

Example:

ansible devopshint -m ping

Output:

15.206.172.222 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

File Operations:

In this we will perform some file operation on remote host

To check there is any files or directories

ansible devopshint -a “ls”

Output:

65.0.89.82 | CHANGED | rc=0 >>

15.206.172.222 | CHANGED | rc=0 >>

Sample

Create files:

ansible all -a  “touch file11”

Output:

65.0.89.82 | CHANGED | rc=0 >>

15.206.172.222 | CHANGED | rc=0 >>

Output in client node1:

ansible@ip-172-31-47-246:~$ ls

file11  sample

Output in client node2:

ansible@ip-172-31-41-9:~$ ls

file11

Copy files across the remote nodes:

ansible devopshint -m copy -a "src=./sample dest=/tmp/sample"

Output in client node:

ansible@ip-172-31-47-246:/tmp$ ls -l sample
-rw-rw-r-- 1 ansible ansible 0 Mar  3 10:07 sample

System Information:

In this system information we are going to check operating system, IP addresses and more.

Run the below command to check all about your system

ansible devopshint -m setup

Find the operating system run the command:

ansible devopshint -m setup -a 'filter=ansible_os_family'

To check memory details to your client node run the below command:

ansible devopshint -m setup -a 'filter=ansible_memory_mb'

Output:

15.206.172.222 | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 735,
                "used": 233
            },
            "real": {
                "free": 297,
                "total": 968,
                "used": 671
            },
            "swap": {
                "cached": 0,
                "free": 0,
                "total": 0,
                "used": 0
            }
        },
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false
}

Find high CPU consuming processes:

To know about the highest cpu consuming processes you can use top command.

ansible devopshint -m shell -a "top -c -b | head -15"

Find high Memory consuming processes

The following command will list down the processes with higher memory consumption.

ansible devopshint -m shell -a "ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%mem | head"

Conclusion:

In this article we have covered Ansible Ad hoc commands with examples.

Related Articles:

Reference:

Ansible ad hoc commands official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

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

Share via
Copy link
Powered by Social Snap