Understanding Ad-hoc commands in Ansible

Understanding Ad-hoc commands in Ansible

·

4 min read

Table of contents

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-01

Certainly, let's provide explanations for the commands:

  1. Ansible ad hoc ping command to ping 3 servers:

     ansible -i /path/to/inventory/file server1:server2:server3 -m ping
    

    This Ansible ad hoc command uses the -i option to specify the inventory file path containing the servers to ping. The list of servers to ping includes server1, server2, and server3. The -m ping option indicates the use of the ping module for the ping operation.

  2. Ansible ad hoc command to check uptime:

     ansible -i /path/to/inventory/file all -m command -a uptime
    

    This command employs the command module (-m command) to execute the uptime command on all servers specified in the inventory file. The -a uptime option provides the arguments for the command module.

  3. Ansible ad hoc command to check free memory:

     ansible -i /path/to/inventory/file all -a "free -m"
    

    The -a "free -m" option specifies the arguments for the command to be executed on remote hosts. In this case, the free -m command is used to display memory usage on each host.

  4. Ansible ad hoc command to get physical memory allocated:

     ansible all -m shell -a "cat /proc/meminfo|head -2"
    

    This command uses the shell module to execute the cat /proc/meminfo|head -2 command, displaying the first two lines of the /proc/meminfo file on each host.

  5. Check disk space on all hosts:

     ansible -i inventory_file all -m shell -a 'df -h'
    

    The df command is used to show disk space usage on each host in the inventory file. The -m shell option specifies the execution using the shell.

  6. List all running processes on a specific host:

     ansible -i inventory_file specific_host -m command -a 'ps aux'
    

    This command utilizes the ps command to list all running processes on the specific host specified in the inventory file. The -m command option executes the command using the command module.

  7. Run a shell command with sudo on all hosts:

     ansible -i inventory_file all -b -m shell -a 'sudo-command'
    

    Using the -b option to become the sudo user, this command runs the specified shell command with sudo on all hosts in the inventory file. Replace 'command' with the desired command.

    For example:

     ansible -i inventory_file all -b -m shell -a 'sudo apt-get install docker.io -y'
     ansible -i inventory_file all -b -m shell -a 'sudo git --version'
     ansible -i inventory_file all -b -m shell -a 'sudo python --version'
    

  1. Copy a file to all hosts:

     ansible -i inventory_file all -m copy -a 'src=/local/path/to/file dest=/remote/path/to/file mode=0644'
    

    The copy module is used to copy a file from the local machine to all hosts in the inventory file. The -a 'src=/local/path/to/file dest=/remote/path/to/file mode=0644' option specifies source and destination paths along with the desired file permissions.

    For example:

     ansible -i inventory_file all -m copy -a 'src=/home/ubuntu/demo.txt dest=/remote/path/to/demo.txt mode=0644'
     sudo ls /remote/path/to
    

  2. Create a directory with 755 permissions:

     ansible all -m file -a "path=/home/ubuntu/ansible state=directory mode=0755" -b
    

    Using the file module, this command creates a directory at the specified path (/home/ubuntu/ansible) with the directory state and permissions (mode=0755). The -b option runs the command as the superuser (sudo).

Create a file with 755 permissions:

ansible all -m file -a "path=/path/to/file state=touch mode=0755" -b

Utilizing the file module, this command creates a file at the specified path (/path/to/file) with the touch state and permissions (mode=0755). The -b option runs the command as the superuser (sudo).

Check the file creation status using:

ansible all -b -m shell -a 'sudo ls -l /home/ubuntu/test.txt'


Thankyou for reading until here. See you in the next one.