Day 56: Understanding Ad-hoc commands in Ansible

Day 56: 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 in handy when you want to perform a quick task.

Task-01

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

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

This command uses the ansible command with the following options:

  • -i /path/to/inventory/file: specifies the path to the inventory file containing the servers we want to ping. /etc/ansible/hosts is by default path of inventory file. there is no need to write path in ansible ad hoc command. If your inventory file is at different location then you need to write the path of inventory file in ad hoc command.

  • server1:server2:server3: specifies the list of servers to ping, separated by colons.

  • -m ping: specifies that we want to use the ping module to ping the servers.

    1. Write an ansible ad hoc command to check uptime

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

      This command uses the ansible command with the following options:

      • -m command: specifies that we want to use the command module to execute the uptime command on the remote servers.

      • -a uptime: specifies the arguments to pass to the command module, which in this case is simply the uptime command.

  1. ansible ad hoc command to check the free memory or memory usage of hosts

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

    -a "free -m": the -a option specifies the arguments to pass to the command to be executed on the remote hosts. In this case, the free-m command is executed to show the memory usage on each host.

  2. To check the disk space on all hosts in an inventory file

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

    This command uses the df command to show the disk space usage on each host in the inventory file.

    The -m shell option is used to execute the command on the remote hosts using the shell.

  3. To run a shell command with sudo on all hosts in an inventory file

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

    This command uses the -b option to become the sudo user before executing the command with the shell module. Replace the command with the command you want to run as sudo.

    1. Use the sudo command 'sudo apt-get install docker.io -y', so by using this ad hoc command you can install Jenkins, nginx, apache, nodejs, etc. on your 3 or more servers.

    2. Use the sudo command 'sudo git --version' which shows all 3 servers git version.

    3. Use the sudo command 'sudo python --version' which shows all 3 server python versions.

    4. Use the sudo command 'sudo docker --version' which shows all 3 server python versions.

  1. To check the status of a specific service on all hosts in an inventory file

     ansible -i inventory_file all -m service -a 'name=docker state=started'
    

    This command uses the service module to check the status of the apache2 service on all hosts in the inventory file. The -a 'name=docker state=started' option specifies the name of the service to check and the desired state, which is started in this case.

  2. You can check the status of a specific service on single hosts or server1

     ansible -i inventory_file server1 -m service -a 'name=apache2 state=started'
    

  3. To copy a file to all hosts in an inventory file

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

    This command uses the copy module 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 the source and destination paths for the file, as well as the desired file permissions.

    first create a simple text file at any location, here create a text file at /home/ubuntu location with the name demo.txt

    Check file is successfully copied to all the servers using the 'sudo ls command'

  4. Create a file with 755 permissions using ansible ad hoc commands

     ansible all -m file -a "path=/path/to/file state=touch mode=0755"
    
    • -a "path=/path/to/file state=touch mode=0755": This is the argument to the -m option. It tells Ansible to create a file at the path /path-to-file with the touch state (i.e., to create the file if it doesn't exist). The mode argument sets the file permissions to 0755, which means that the owner has read, write, and execute permissions, and everyone else has read and execute permissions.

    • -b: This tells Ansible to run the command as the superuser (i.e., with sudo).

Check file is created with given permissions using the sudo ls -l command

Happy Learning!