Understanding Ad-hoc commands in Ansible

Understanding Ad-hoc commands in Ansible

Ansible ad hoc commands are one-liners designed to achieve a very specific task they are like quick snippets and your compact swiss army knife when you want to do a quick task across multiple machines.

To put simply, Ansible ad hoc commands are one-liner Linux shell commands and playbooks are like a shell script, a collective of many commands with logic.

Ansible ad hoc commands come handy when you want to perform a quick task.

1) Write an ansible ad hoc ping command to ping 3 servers from the inventory file

  1. If you're trying to use an Ansible ad hoc command to ping three servers from host file, you can use the ansible command along with the ping module. Here's how you can achieve that:
#Your /etc/ansible/host File should Look Like This
server1 ansible_host=54.166.149.111 ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-key.pem
server2 ansible_host=54.224.108.181 ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-key.pem
server3 ansible_host=54.81.68.115  ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-key.pem

Now Try ping to all these servers using following command

ansible server1:server2:server3 -m ping

  1. If you're trying to use an Ansible ad hoc command to ping three servers from inventory.ini, First create inventory.ini file same as host file
touch /home/ubuntu/keys/inventory.ini
vim /home/ubuntu/keys/inventory.ini

Write below content in the /ubuntu/home/keys/inventory.ini file

# inventory.ini
#Your /ubuntu/home/keys/inventory.ini file should Look Like This
server1 ansible_host=54.166.149.111 ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-key.pem
server2 ansible_host=54.224.108.181 ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-key.pem
server3 ansible_host=54.81.68.115  ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-key.pem

Now, we will set the permission as 600 to this file

sudo chmod 600 /home/ubuntu/keys/inventory.ini

Use below Ansible ad hoc command to ping the servers:

ansible server1:server2:server3 -i inventory.ini -m ping

Here's a breakdown of the command:

  • server1:server2:server3: Specifies the hosts you want to target from the inventory. You can also use host patterns or group names if you've defined any in your inventory.

  • -i inventory.ini: Specifies the inventory file.

  • -m ping: Uses the ping module.

You should see output for each host, indicating whether the ping was successful or not. If the hosts are reachable and Ansible is properly configured on them, you should see a SUCCESS status and a "pong" response.

2) Write an ansible ad hoc command to check uptime

To check the uptime of hosts using an Ansible ad hoc command, you can use the command module to execute the uptime command. Assuming you have an inventory file named inventory.ini:

You can use the following Ansible ad hoc command:

ansible all -i inventory.ini -m command -a "uptime"

Breakdown:

  • all: This targets all hosts in the inventory. If you want to target specific hosts or a group, replace all with the respective hostnames or group names.

  • -i inventory.ini: Specifies the inventory file.

  • -m command: Uses the command module.

  • -a "uptime": Specifies the command you want to run (in this case, uptime).

Upon execution, you should see the uptime for each of the servers listed in your inventory.

3)Gather facts (system and network info, etc.) about hosts:

ansible all -i inventory.ini -m setup

4)Check disk space on all hosts:

ansible all -i inventory.ini -m command -a "df -h"

5) Update all servers

ansible all -i inventory.ini -m shell  -a 'sudo apt update -y'

6) Install Docker

ansible all -i inventory.ini -m shell  -a 'sudo apt install docker.io -y'
ansible all -i inventory.ini -m shell  -a 'sudo docker --version'

7)Install apache and see service status

ansible all -i inventory.ini -m shell  -a 'sudo apt install apache2 -y'
ansible all -i inventory.ini -m service -a 'name=apache2 state=started'

8)Copy a file to all hosts in an inventory file

ansible all -i inventory.ini -m copy -a 'src=/home/ubuntu/keys/myfile.txt dest=/home/ubuntu/ mode=0644'
ansible all -i inventory.ini -m shell  -a 'sudo ls /home/ubuntu/'

Remember to replace inventory.ini with the path to your own inventory file, and adjust host patterns (all in the examples) as needed.

Always make sure to test commands on a safe environment before applying them to production servers, especially commands that might change system state or configuration.

Thanks for reading! ๐Ÿ˜ƒ๐Ÿ™

ย