Ansible is a powerful automation tool that allows you to manage and configure multiple systems efficiently. While playbooks offer a robust way to automate tasks, sometimes you need to perform quick, one-off tasks across a set of machines. This is where Ansible ad hoc commands come into play.
Ansible ad hoc commands are simple one-liners that allow you to perform tasks across multiple servers without writing a playbook. They are useful for quick tasks, troubleshooting, or running commands across a set of hosts.
In this guide, we’ll explore how to use Ansible ad hoc commands with practical examples.
Table of Contents
Basic Syntax
The basic syntax of using ad hoc commands in Ansible is as follows:
# ansible host-pattern -m module -a "module-arguments" [options]
Explanation:
- host-pattern: Specifies the target hosts.
- -m module: Specifies the module to use.
- -a “module-arguments”: Specifies the module arguments.
Common Modules Used in Ad Hoc Commands
Here are some commonly used modules in ad hoc commands:
- ping: Checks the connectivity to the hosts.
- command: Runs commands on the remote hosts.
- shell: Runs shell commands on the remote hosts.
- copy: Copies files to the remote hosts.
- user: Manages users on the remote hosts.
- yum, apt: Manages packages on the remote hosts.
- service: Manages services on the remote hosts.
- setup: Gather facts about the remote hosts.
Examples 1: Checking System Uptime
You can quickly check the system uptime on all your hosts using the command module:
# ansible all -m command -a "uptime"
Output.
Example 2: Managing Users
To add a new user to all your hosts, use the user module:
# ansible all -m user -a "name=johndoe state=present"
Output:
host1 | SUCCESS = {
"changed": true,
"name": "johndoe"
}
host2 | SUCCESS = {
"changed": true,
"name": "johndoe"
}
To delete a user, use the state=absent option:
# ansible all -m user -a "name=johndoe state=absent"
Output.
host1 | SUCCESS = {
"changed": true,
"name": "johndoe"
}
host2 | SUCCESS = {
"changed": true,
"name": "johndoe"
}
Example 3: Managing Packages
To install a package on all your hosts using the yum module (for RHEL-based systems):
# ansible all -m yum -a "name=httpd state=present"
For Debian-based systems, use the apt module:
# ansible all -m apt -a "name=nginx state=present"
Example 4: Copying Files
To copy a file from your local machine to all your hosts:
# ansible all -m copy -a "src=/tmp/apache.conf dest=/etc/apache2/"
This command copies an apache.conf file from your local machine to /etc/apache2/ directory on all remote servers.
Example 5: Managing Services
To start a service on all your hosts:
# ansible all -m service -a "name=httpd state=started"
This command starts an Apache service on all remote hosts.
To stop an Apache service, use the state=stopped option.
# ansible all -m service -a "name=httpd state=stopped"
Example 6: Gathering Facts
To gather and display facts about your hosts, use the setup module.
# ansible all -m setup
This command will output detailed information about each host, including hardware, network, and OS details.
host1 | SUCCESS = {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.1.10"
],
"ansible_all_ipv6_addresses": [],
"ansible_architecture": "x86_64",
"ansible_bios_date": "04/01/2020",
...
},
"changed": false
}
host2 | SUCCESS = {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.1.11"
],
"ansible_all_ipv6_addresses": [],
"ansible_architecture": "x86_64",
"ansible_bios_date": "04/01/2020",
...
},
"changed": false
}
To gather only the IP address facts, you can filter the output using the setup module:
# ansible all -m setup -a 'filter=ansible_all_ipv4_addresses'
Output.
host1 | SUCCESS = {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.1.10"
]
},
"changed": false
}
host2 | SUCCESS = {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.1.11"
]
},
"changed": false
}
Example 7: Running Complex Shell Commands
Suppose you want to find and delete log files older than 7 days. You can use the shell module to execute this command:
# ansible all -m shell -a 'find /var/log -type f -name "*.log" -mtime +7 -exec rm -f {} \;'
Output.
host1 | SUCCESS | rc=0
host2 | SUCCESS | rc=0
Example 8: Checking Disk Space Usage
To check disk space usage on all hosts, use the shell module with “df -h” command.
# ansible all -m shell -a 'df -h'
Output.
host1 | SUCCESS | rc=0
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 20G 28G 42% /
...
host2 | SUCCESS | rc=0
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 15G 33G 31% /
Example 9: Creating Directories with Specific Permissions
To create a directory with specific permissions, use the file module.
# ansible all -m file -a "path=/data/test state=directory mode=0755"
This command creates a directory at /data/test on all targeted hosts, setting its permissions to 0755.
Conclusion
Ansible ad hoc commands are a powerful way to quickly perform tasks across multiple hosts without writing a playbook. They are particularly useful for troubleshooting, system administration, and performing quick tasks. By understanding the basic syntax and commonly used modules, you can leverage the full potential of Ansible ad hoc commands in your daily operations.
FAQs
1. Can I run an ad-hoc command as a specific user?
Yes, you can run ad-hoc commands as a specific user by adding the -u username option. You can also use --become to execute the command with elevated privileges.
2. How can I execute an ad-hoc command on a specific host?
You can target a specific host by specifying its name in the ad-hoc command, like ansible hostname -m module.