Updated April 4, 2023
Introduction to Ansible ad-hoc Commands
Ansible ad-hoc commands are the commands that we can directly run from the terminal on one or more managed clients. It is the simplest and the quickest way to run a command on any client. It is mostly used to test the commands or commands that we do not use repeatedly. It is one-liner command that does not require writing a playbook, for example checking the connection of managed nodes with the Ansible master server, managing files, services, users etc. It shows the simplicity and power of Ansible however, we can run one module at a time.
Syntax:
$ansible [pattern] -m [module] -a “[module options]”
- pattern: where we define managed nodes or group in our inventory.
- module: where we define which module of Ansible we want to execute against the pattern.
- module options: where we pass the options that are required for that module to work and it is optional.
Example of Ansible ad-hoc Commands
Given below is the example mentioned:
Code:
$ansible ansible_client.lab.com -m ping
Output:
Explanation:
In the above command, we used ‘ping’ module to test connection between master server and client named ‘ansible_client.lab.com’.
Ansible ad-hoc Commands
Given below are some ansible ad-hoc commands that are used in day to day management of our infrastructure:
1. If we want to collect information of a specific host or group of host.
Example #1
Code:
$ansible webservers –m setup
Output:
Explanation:
In the above example, we used ‘setup’ module to get the details about the nodes in the webservers group. We get a lot of data as an output, however, if we are interested in specific fact, we can use ‘filter’ option as below.
Example #2
Code:
$ansible webservers -m setup -a "filter=ansible_nodename"
Output:
Explanation:
In the above example, we have filtered out hostname of the server using ‘ansible_nodename’.
2. We can manage packages like installing and uninstalling a package or checking the status of the package on the nodes.
Example #1:
Code:
$ansibletest_group -b -m yum -a "name=nginx state=latest"
Output:
Explanation:
In the above example, we have installed ‘nginx’ package to the client node. The ‘-b’ option is used to run the command as root as it requires sudo privilege. We use ‘state=absent’ in the ad-hoc command to uninstall any package as below.
Example #2:
Code:
$ansibletest_group -b -m yum -a "name=nginx state=absent"
Output:
Explanation:
In the above example, we can see changed is equal to true. It means ‘nginx’ package has been uninstalled successfully.
3. We can manage files using Ansible ad-hoc commands, for example, creating new files, removing existing files and modifying it etc. We use ‘file’ module to manage files.
Example:
Code:
$ansibletest_group -m file -a "path=/root/my_testfile state=touch”
Output:
Explanation:
In the above example, it creates a file named ‘my_testfile’ in the ‘/root’ folder. If we want to get the details of any file, just give the path as below.
Code:
$ ansibletest_group -m file -a "path=/root/my_testfile"
Output:
We can also change the mode, owner, and group of the file, if we want as below.
Code:
$ansibletest_group -m file -a "path=/root/my_testfile mode=0400"
Output:
Explanation:
In the above example, if we compare the mode from the previous snapshot, we see that mode has been changed from ‘0644’ to ‘0400’. We can make changes to multiple things like group and ownership of the file in the same command. And if we want to delete the file, change the state value to ‘absent’ as below.
Code:
$ansibletest_group -m file -a "path=/root/my_testfile state=absent"
Output:
4. We can also manage users such as creating new users, deleting any existing users, modifying the group, etc.
We use ‘user’ module to manage users as below.
Example #1:
Code:
$ansible webservers -b -m user -a "name=jon"
Output:
Example #2:
In the above example, it creates a new user named ‘jon’ and now we want to add this user to wheel group as below.
Code:
$ ansible webservers -b -m user -a "name=jon append=yes group=wheel"
Output:
Explanation:
In the above example, it adds user ‘jon’ to ‘wheel’ group and we have used append flag and set it to yes because if we don’t use append flag with yes, it is going to wipe out the detail of previous group of that user. And to remove the user, we use ‘state’ flag and set it to ‘absent’ as below.
Code:
$ansible webservers -b -m user -a "name=jon state=absent"
Output:
5. We can manage services as well using Ansible ad-hoc commands, for example, starting the service, stopping the service, checking the status of the service etc. We use ‘service’ module to start the service as below.
Example:
Code:
$ansible ansible_client.lab.com -m service -a "name=httpd state=started"
Output:
Explanation:
In the above example, we have started the ‘httpd’ service using the ‘service’ module on the client node. Just login to the client node to verify that the service is running.
6. We can also reboot our servers using Ansible ad-hoc command as below.
Example:
Code:
$ansible webservers -i inventory.ini -b -a "/sbin/reboot" -f 10
Output:
Explanation:
In the above example, we can see that the client node is getting rebooted as soon as we run the command however, getting an error because the host becomes unreachable when it reboots.
7. We can execute shell command on the client nodes using the ‘shell’ module. We need to pay attention to shell quoting rules to avoid any error.
Example:
Code:
$ansible webservers -m shell -a ‘df -h’
Output:
Explanation:
In the above example, we have run the ‘df -h’ command to check the disk utilization on the client node. We can run any shell command remotely that we run on our system.
Conclusion
Ansible ad-hoc Commands are very useful when we have to run any command quickly from the terminal without writing any playbook. We can send the output to a file using redirection and can pipe the output to use by another command like less, more, grep, etc. It is very easy and simple to use however, it is not useful while writing complex tasks.
Recommended Articles
This is a guide to Ansible ad-hoc Commands. Here we discuss the introduction to Ansible ad-hoc Commands, example for better understanding. You may also have a look at the following articles to learn more –