Day 42: Ad-hoc commands in Ansible

Hi, I am an AWS-certified cloud engineer and I write about my progress and learnings of DevOps.
Hi everyone!
In the last article, we saw the basics of Ansible, and its architecture, and connected our Master server to the worker server.
Today we will explore more about the working of Ansible and its ad-hoc commands. Ad hoc in Latin means something done for an exact and particular purpose. As the word suggests, ansible ad hoc commands are written for a very specific task.
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 in handy when you want to perform a quick task.
Write an ansible ad hoc ping command to ping 3 servers from the inventory file
To ping all the servers run the below command:
ansible -m ping all -i /etc/ansible/hosts

Write an ansible ad hoc command to check uptime
To check the uptime run the below command
ansible all -a uptime

Check the free memory or memory usage of hosts using Ansible ad hoc command
To check the memory usage run below
ansible all -a "free -m" -i inventory

Create a new user in the worker node from Master
Creating a new user remotely
ansible all -m user -a "name=mark password=<your password>"

Check free memory in the worker node
ansible all -a "free -m" -i inventory

Create a new directory in worker node
ansible all -m file -a "path=/home/ansible/test-directory state=directory mode=0755" -b

Create a new file in worker node
ansible all -m file -a "path=/home/ansible/test-file state=touch mode=0755"


Check free space on worker node
ansible all -a "df -h"

Thanks for reading ;)





