Day 56: Understanding Ad-hoc commands in Ansible  (Jan 21, 2024)

Day 56: Understanding Ad-hoc commands in Ansible (Jan 21, 2024)

🙏 Introduction:

In this blog, we'll explore Ansible Ad-hoc commands.

🔶What is 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.

🎯Task: 1

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

  • Create one master node server and three server nodes.

  • Edit the host file
[servers]
host_1 ansible_host=54.184.85.99
host_2 ansible_host=54.200.206.23
host_3 ansible_host=34.215.56.203

[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-key.pem

  • To verify the inventory of hosts
ansible-inventory --list

  • ad hoc command to ping 3 servers from inventory file
ansible host_1:host_2:host_3 -m ping

  1. Write an ansible ad hoc command to check uptime

ansible -a "uptime" servers

For more Ansible ad-hoc commands we can refer to this link