You are on page 1of 10

What is Ansible 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 first? ad hoc commands demonstrate the
simplicity and power of Ansible. The concepts you learn
here will port over directly to the playbook language.
-a <MODULE_ARGS>, --args <MODULE_ARGS>
module arguments

-b, --become
run operations with become (does not imply password prompting)

-c <CONNECTION>, --connection <CONNECTION>


connection type to use (default=smart)

-e, --extra-vars
set additional variables as key=value or YAML/JSON, if filename prepend with @

-f <FORKS>, --forks <FORKS>


specify number of parallel processes to use (default=5)

-h, --help
show this help message and exit

-i, --inventory, --inventory-file


specify inventory host path or comma separated host list. –inventory-file is deprecated

-k, --ask-pass
ask for connection password

-l <SUBSET>, --limit <SUBSET>


further limit selected hosts to an additional pattern
-m <MODULE_NAME>, --module-name <MODULE_NAME>
module name to execute (default=command)

-o, --one-line
condense output

-s, --sudo
run operations with sudo (nopasswd) (deprecated, use become)

-t <TREE>, --tree <TREE>


log output to this directory

-u <REMOTE_USER>, --user <REMOTE_USER>


connect as this user (default=None)

-v, --verbose
verbose mode (-vvv for more, -vvvv to enable connection debugging)
Why use ad hoc commands?
ad hoc commands are great for tasks you repeat rarely.
For example, if you want to power off all the machines for
mintainance, you could execute a quick one-liner in Ansible
without writing a playbook. An ad hoc command looks like
this:

$ ansible [pattern] -m [module] -a "[module options]"


Rebooting probably requires privilege escalation. You
can connect to the server as username and run the
command as the root user by using the become
keyword:

$ ansible servername -a "/sbin/reboot" -f 10 -u username


--become [--ask-become-pass
Use cases for ad hoc tasks
ad hoc tasks can be used to reboot servers, copy files,
manage packages and users, and much more. You can
use any Ansible module in an ad hoc task. ad hoc tasks,
like playbooks, use a declarative model, calculating and
executing the actions required to reach a specified final
state.
Managing files
You can use the Ad-hoc commands for doing SCP (Secure Copy Protocol) lots of files in
parallel on multiple machines.

Transferring file to many servers/machines


$ Ansible localhost -m copy -a "src = /etc/yum.conf dest = /tmp/yum.conf"
Creating new directory
$ Ansible localhost -m file -a "dest = /path/user1/new mode = 777 owner = user1 group = user1
state = directory"
Deleting whole directory and files
$ Ansible localhost -m file -a "dest = /path/user1/new state = absent"

ansible localhost -b -m fetch -a "src=/etc/hosts dest=/tmp"


Managing users
Before doing this make sure you have the group
$ ansible localhost -b -m user -a "name=keen group=keen
createhome=yes"
Managing packages
You might also use an ad hoc task to install, update, or remove packages
on managed nodes using a package management module like yum. To
ensure a package is installed without updating it:

$ ansible localhost -b -m yum -a "name=ntp state=present"


$ ansible localhost -b -m service -a "name=ntpd state=started
enabled=yes"
$ ansible localhost -b -a "service ntpd stop"
$ ansible localhost -a "service status ntpd"
$ ansible localhost -b -m yum -a "name=ntp state=absent"
Gathering Facts
Facts can be used for implementing conditional statements in
playbook. You can find adhoc information of all your facts through
the following Ad-hoc command

$ Ansible localhost -m setup

You might also like