Understanding Ad-hoc commands in Ansible
What is an ad hoc command?
An Ansible ad hoc command uses the /usr/bin/ansible command-line tool to automate a single task on one or more managed nodes. ad hoc commands are quick and easy, but they are not reusable. So why learn about ad hoc commands? ad hoc commands demonstrate the simplicity and power of Ansible. The concepts you learn here will port over directly to the playbook language.
Various use cases for ad hoc tasks:
Rebooting servers: The default module for the
ansible
command-line utility is the ansible.builtin.command module. You can use an ad hoc task to call the command module and reboot all web servers in Atlanta, 10 at a time. Before Ansible can do this, you must have all servers in Atlanta listed in a group called [atlanta] in your inventory, and you must have working SSH credentials for each machine in that group. To reboot all the servers in the [atlanta] group:$ ansible atlanta -a "/sbin/reboot"
Managing files: An ad hoc task can harness the power of Ansible and SCP to transfer many files to multiple machines in parallel. To transfer a file directly to all servers in the [atlanta] group:
$ ansible atlanta -m ansible.builtin.copy -a "src=/etc/hosts dest=/tmp/hosts"
Managing packages: An ad hoc can be used task to install, update, or remove packages on managed nodes using a package management module such as
yum
. Package management modules support common functions to install, remove, and generally manage packages. Some specific functions for a package manager might not be present in the Ansible module since they are not part of general package management.To ensure a package is installed without updating it:
$ ansible webservers -m ansible.builtin.yum -a "name=acme state=present"
To ensure a specific version of a package is installed:
$ ansible webservers -m ansible.builtin.yum -a "name=acme-1.5 state=present"
Managing users and groups: You can create, manage, and remove user accounts on your managed nodes with ad hoc tasks:
$ ansible all -m ansible.builtin.user -a "name=foo password=<encrypted password here>" $ ansible all -m ansible.builtin.user -a "name=foo state=absent"
Managing services:Ensure a service is started on all webservers:
$ ansible webservers -m ansible.builtin.service -a "name=httpd state=started"
Alternatively, restart a service on all webservers:
$ ansible webservers -m ansible.builtin.service -a "name=httpd state=restarted"
Ensure a service is stopped:
$ ansible webservers -m ansible.builtin.service -a "name=httpd state=stopped"
Gathering facts: Facts represent discovered variables about a system. You can use facts to implement conditional execution of tasks but also just to get ad hoc information about your systems. To see all facts:
$ ansible all -m ansible.builtin.setup
Check mode: In check mode, Ansible does not make any changes to remote systems. Ansible prints the commands only. It does not run the commands.
$ ansible all -m copy -a "content=foo dest=/root/bar.txt" -C
Patterns and ad-hoc commands: You use a pattern almost any time you execute an ad hoc command or a playbook. The pattern is the only element of an ad hoc command that has no flag. It is usually the second element:
ansible <pattern> -m <module_name> -a "<module options>"
For example:
ansible webservers -m service -a "name=httpd state=restarted"
Examples
Write an ansible ad hoc ping command to ping 3 servers from the inventory file
Write an ansible ad hoc command to check uptime