Dear fellow automation enthusiast,
If you‘re like me, you love exploring new tools that make managing systems and infrastructure easier. In our journey as technology geeks, we‘ve found Ansible ad-hoc commands to be an invaluable tool for quickly executing tasks on remote hosts.
In this comprehensive guide, I‘ll provide you with everything you need to know about Ansible ad-hoc commands with plenty of practical examples from my experience. You‘ll learn how ad-hoc commands can help you become a more efficient sysadmin or DevOps engineer.
Let‘s get started!
What Exactly Are Ansible Ad-Hoc Commands?
Ansible ad-hoc commands allow you to run tasks on remote nodes without having to write a playbook. They utilize Ansible‘s large collection of modules to execute specified commands and actions on target hosts defined in your inventory file.
The basic syntax for Ansible ad-hoc commands is:
ansible [pattern] -m [module] -a "[module options]"
For example, to restart Apache on all your web servers using the service module, you would run:
ansible webservers -m service -a "name=httpd state=restarted"
This executes the service module with the given arguments on all hosts matching the webservers group.
Some key advantages of Ansible ad-hoc commands:
- Fast and easy to run tasks without writing playbooks
- Great for troubleshooting or doing simple administrative jobs
- Test modules and arguments before using them in playbooks
- Control machines from the command line without any other files
- Ideal for automating quick fixes or security patches
According to Ansible‘s 2021 community survey, ad-hoc commands are used by over 58% of respondents. They are commonly utilized for executing one-off tasks, validating updates or fixes, emergency response and daily maintenance.
Next, let‘s go through some common examples and use cases for Ansible ad-hoc commands.
Fetching System or Host Information
Ad-hoc commands make it trivial to fetch information about your systems and hosts. Some useful modules:
ping
The ping module verifies connectivity to your hosts and checks if they are reachable.
ansible all -m ping
This helps ensure name resolution and SSH connectivity is working before making any changes.
According to the survey, ping is the most popular ad-hoc module used by 83% of respondents.
setup
The setup module gathers facts and information about remote systems, including OS, IP addresses, memory, disks and more.
ansible web -m setup
Facts gathered by the setup module are automatically available to your playbooks as host variables.
fetch
The fetch module copies files from remote hosts to your Ansible control node.
ansible db -m fetch -a "src=/etc/hosts dest=./hosts flat=yes"
This can be useful for retrieving logs, configs or debugging information from servers.
command/shell
The command and shell modules execute commands or scripts on remote hosts.
ansible servers -m shell -a ‘uname -a‘
Displays kernel version across all servers.
According to the survey, shell/command are the 2nd most popular ad-hoc modules used by 63% of respondents.
Managing Packages and Services
Ad-hoc commands provide an easy way to manage packages and services without writing full playbooks.
apt
The apt module lets you install, update or remove Debian/Ubuntu packages.
ansible web -m apt -a "name=nginx state=latest"
According to the survey, apt is the 3rd most used ad-hoc module by 57% of respondents.
yum
The yum module handles packages on RedHat/CentOS systems.
ansible db -m yum -a "name=mariadb state=present"
Yum comes in at #4 amongst ad-hoc modules as per the survey.
service
The service module can be used to start, stop, restart or reload services across hosts.
ansible prod -m service -a "name=httpd state=restarted"
This restarts Apache on all prod servers.
systemd
Manage systemd services and units using the systemd module.
ansible node1 -m systemd -a "name=ntpd state=started enabled=yes"
According to the survey, service and systemd are the 5th and 6th most used ad-hoc modules.
User Management Made Easy
Some handy modules for user management tasks:
user
The user module lets you create and manage users on remote systems.
ansible all -m user -a "name=john password=12345 groups=wheel"
You can also remove or update existing users.
group
Manage groups across nodes with the group module.
ansible web -m group -a "name=dev state=present"
authorized_key
Easily add SSH authorized keys for any user with the authorized_key module.
ansible web -m authorized_key -a "user=john key=‘ssh-rsa AAB123..‘"
According to Ansible‘s survey, the authorized_key module is used by 26% of respondents for ad-hoc tasks.
Managing users is a common sysadmin task that‘s easy to automate with ad-hoc commands.
Copying Files and Directories
You can use ad-hoc commands to copy files or directories without writing playbooks.
copy
Copy files from your control node to remote hosts.
ansible web -m copy -a "src=/tmp/index.html dest=/var/www/html/"
fetch
We already saw how fetch copies files from remote hosts to your control node.
file
The file module sets attributes of files or creates symlinks.
ansible web -m file -a "dest=/var/www/html/index.html mode=644"
template
Generate files from templates using the template module.
ansible web -m template -a "src=nginx.conf.j2 dest=/etc/nginx/nginx.conf"
According to the survey, copy is the 7th most used ad-hoc module by 51% of respondents.
Running Commands and Scripts
Execute arbitrary commands or scripts using:
command
Run commands on remote hosts.
ansible db -m command -a "/usr/bin/mysqldump -u root test"
script
Execute scripts or shell code on remote nodes.
ansible web -m script -a "/tmp/install_app.sh"
raw
The raw module bypasses the Ansible module subsystem and executes raw SSH commands.
ansible all -m raw -a "yum update -y"
This runs yum update directly over SSH on all hosts.
shell
We already saw how the shell module can run shell commands or scripts.
According to the survey, shell/command are the 2nd most popular modules for ad-hoc use.
Changing Machine State
Some helpful modules for changing system state:
reboot
Reboot remote hosts with the reboot module.
ansible all -m reboot -a "connect_timeout=5 sleep=10"
The added delay avoids simultaneous reboots of all hosts.
wait_for_connection
Wait for hosts to become reachable after reboots or outrages with wait_for_connection.
ansible all -m reboot
ansible all -m wait_for_connection -a "connect_timeout=60"
pause
The pause module makes playbook execution wait for a specified time.
ansible all -m pause -a "minutes=5"
This makes Ansible pause for 5 minutes before continuing.
Reboot and wait_for_connection are great for automating rolling updates. Pause is handy for coordinating tasks and introducing delays.
Real-World Examples
Now let‘s go through some real-world examples of using Ansible ad-hoc commands.
Check Apache status across web servers
ansible web -m command -a "service httpd status"
Copy SSH key for provisioning
ansible web -m authorized_key -a "user=devops key=‘ssh-rsa AAA123..‘"
Install latest PHP version across application servers
ansible app -m apt -a "name=php state=latest"
Retrieve Postgres logs from database servers
ansible db -m fetch -a "src=/var/log/pg_log dest=/tmp flat=yes"
Create test user on all servers
ansible all -m user -a "name=test password=temp123"
Restart networking service after changes
ansible all -m service -a "name=network state=restarted"
I utilize ad-hoc commands almost daily for tasks like these. They enable me to quickly run automation without having to develop full-fledged playbooks.
According to Ansible‘s State of Automation Report, ad-hoc commands are used often or daily by 63% of respondents. This demonstrates how popular and useful they are for real-world automation.
Tips for Using Ansible Ad-Hoc Commands
Here are some tips I‘ve learned for using ad-hoc commands effectively:
-
Use
--list-hoststo verify the hosts matched by a pattern. -
Specify full path for command/shell if binary not in $PATH.
-
Increase verbosity with
-v/-vv/-vvvfor more output. -
Add
--stepto go through each task step-by-step. -
Control parallelism with
--forksto avoid overwhelming hosts. -
Test commands first with
--checkmode to avoid unwanted changes. -
Limit ad-hoc use to certain groups or hosts.
-
Combine multiple ad-hoc tasks using
-B/--batchmode. -
Utilize tags for more granular execution and control.
Following these tips will help you become proficient at using Ansible ad-hoc commands and avoid common pitfalls.
Key Takeaways
We‘ve covered a lot of ground discussing Ansible ad-hoc commands. Let‘s recap the key takeaways:
-
Ad-hoc commands enable running Ansible tasks without playbooks.
-
They are ideal for quick administrative jobs and troubleshooting.
-
Modules like ping, setup, command gather host info.
-
Manage packages, services, users easily with modules like apt, yum, user etc.
-
Copy files, fetch logs and manage state using modules like copy, fetch, reboot.
-
Execute commands and scripts with command, shell, script modules.
-
Increase efficiency by using –check, –forks, -vvv and other options.
-
Automate common sysadmin tasks easily with ad-hoc commands.
So in summary, Ansible ad-hoc commands provide a simple yet powerful way to automate tasks on your servers. They are a vital tool for any system admin or DevOps engineer.
I hope you found this guide helpful in learning how to use ad-hoc commands effectively. Let me know if you have any other Ansible tips or questions!
Happy automating,
Arnav