Ansible Ad-hoc Commands

Ad-hoc Commands

Ad-hoc commands are the most superficial statements used in Ansible, which ate mainly used whenever you want to operate a group of servers. These commands are fast and give a good response when a specific server target. They use /usr/bin/ansible command-line tool for the automation of a particular task on a single or multiple hosts (s). They are not reusable but are useful for a specific period of time. These commands show us the actual strength of Ansible.

Ad hoc commands have multiple use case, which is as follows:

  1. File management
  2. Server rebooting
  3. Package management
  4. Client management
  5. Service management
  6. Fact gathering

These commands are utilized when you want to save your time as some tasks are performed so rarely. You will not write a whole playbook for such type of task; rather than you will write a single line code.

ansible [pattern] -m [module] -a “[module options]”

Explanation of use cases for ad hoc commands

These commands or tasks have multiple capabilities as they can reboot a server, manage clients, and do many more things. They become busy until they complete their current task and do not move forward if the current task is not completed.

  1. File management

For transferring a large number of files to multiple machines parallelly, ad hoc commands tackle with the power of ansible and SCP (secure copy protocol).

  1. For spreading a file all on server, you can use this command:
ansible xyz -m copy -a “src =  /etc/yum.conf dest = /tmp/yum.conf”

you can use such command for transferring files in Atlanta group, for example:

ansible atlanta -m ansible.builtin.copy -a “src=/etc/hosts dest=/tmp/hosts”
  •  Ansible's built-in file module provides features like ownership changing and permissions on files. Two examples are given below:
 ansible webservers -m ansible.builtin.file -a “dest=/srv/foo/a.txt mode=600”
 ansible webservers -m ansible.builtin.file -a “dest=/srv/foo/a.txt mode=600  owner=mdehaan group=mdehaan” 
  • We can create new directories by using the command given below:
ansible abc -m file -a “dest = /path/user1/new mode = 755 owner = user1 group = user1 state = directory”
  • Deleting all directory and files
ansible abc -m file -a “dest = /path/user1/new state = absent”
  • Server rebooting

Using ad hoc commands, you can use command modules to reboot or restart each web server. For example, you can reboot ten servers in Atlanta, but you have to do it quick as Ansible can perform this before Atlanta. For achieving such a task, all servers must be listed inside a group or inventory in Atlanta; for each machine in that group, you must have authenticated SSH keys. For example, a command is given that is used in Atlanta for rebooting:

ansible atlanta -a “/sbin/reboot”

Ansible can, by default, handle five processes at an instance. For rebooting ten servers parallelly, we apply the following command:

ansible atlanta -a “/sbin/reboot”  -f 10

/usr/bin/ansible will be executed from your system by default; if you want to connect as a different user, apply the following command:

  ansible atlanta -a “/sbin/reboot”  -f 10 -u username

Rebooting takes time, but if you are doing this with ten servers, it will take a lot of time, creating problems or inconvenience for clients. That's why its security must be enhanced as hackers can create a problem for everyone. So, this security can be achieved by connecting with the server using a username, and with the help of the become keyword, we can run the root user command as shown below.

ansible atlanta -a “/sbin/reboot” -f 10 -u username –become [ --ask-become-pass]

After using –ask-become-pass or -k it will ask you for the password before moving to the next command.

We have used default commands; now it's time to use some commands from built-in modules that we can do by passing -m for the module. For example:

ansible raleigh -m ansible.builtin.shell -a ‘echo $TERM’

As you can see in the above given example, we have used the command ‘echo $TERM’ in single commas, which means that the local shell will first retain the variable, and then the shell will pass the variable to Ansible. Whereas by using a double comma in the command “echo $TERM” means the system will immediately evaluate the variable on the box.

  • Package management

You can perform different operations like installing, removing, and updating packages using yum, a package management module. To check whether a module is installed or not without updating, we can apply the following command:

ansible webservers -m ansible.builtin.yum -a “name=acme state=present’

For checking a particular version of a package is installed or not, we apply the following command:

ansible webservers -m ansible.builtin.yum -a “name=acme – 1.5 state=present”

To check if the installed package is the latest one or not:

 ansible webservers -m ansible.builtin.yum -a “name=acme state=latest”

To check if the package is not installed:

ansible webservers -m ansible.builtin.yum -a “name=acme state=absent”

If you found no module for your package manager, you can create it or install it using the command module.

  • Client management

By using ad hoc commands, you can create, remove user accounts from your machine. The commands are as follows:

 ansible all -m ansible.builtin.user -a “name=foo password=<crypted password here>”
 ansible all -m ansible.builtin.user -a "name=foo state=absent" 

5. Service management

By providing the required commands, we can ensure that a service is started on all webservers:

Some of them are given below:

To check if the service is running on each web server or not:

ansible webservers -m ansible.builtin.service -a “name=httpd state=started”

To restart all the services on all webservers:

ansible webservers -m ansible.builtin.service -a “name=httpd state=restarted”

To stop a particular service:

ansible webservers -m ansible.builtin.service -a “name=httpd state=stopped”

6.Fact gathering

Facts show the overview of the system. These are used to see the information about your system and to execute the commands on condition. For checking all facts:

            ansible localhost -m ansible.builtin.setup

You can abstract this output for more clarity.

Now that you have learned the basic commands used in Ansible, you are ready for Ansible playbooks, which frequently use all of these commands.