ad-hoc commands

What are ad-hoc command?

One of the simplest ways Ansible can be used is by using ad-hoc commands. These can be used when you want to issue some commands on a server or a bunch of servers. Ad-hoc commands are not stored for future uses but represent a fast way to interact with the desired servers.

ansible [pattern] -m [module] -a "[module options]"
[user1@controller demo-adhoc]$ ansible all  -a "uptime"
centos | CHANGED | rc=0 >>
 11:31:04 up 1 day, 51 min,  3 users,  load average: 0.00, 0.01, 0.05
ubuntu | CHANGED | rc=0 >>
 00:01:04 up 20:38,  3 users,  load average: 0.00, 0.03, 0.00

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. They achieve a form of idempotence by checking the current state before they begin and doing nothing unless the current state is different from the specified final state.

privilege escalation

By default ansible in ad-hoc mode is not going to escalate privilege

[user1@controller demo-adhoc]$ ansible all -a "whoami"
ubuntu | CHANGED | rc=0 >>
user1
centos | CHANGED | rc=0 >>
user1

as you can see, the command whoami has been execute as regular user on remote targets. If we need to run thing that requires more access or we want to escalate privileges we have to use -b or become switch:

Rebooting servers

The default module for the ansible command-line utility is the ansible built-in command module. You can use an ad-hoc task to call the command module and reboot all servers:

it gives us errors, because it doesn't escalate privilege, lets use -b switch:

And all targets have been rebooted. Lets wait for a minute and check:

shutting down servers

and lets quickly cancel it:

Installing a package

Lets install a web server on ubuntu machine:

Obviously there is not apt program on centos machine so it troughs an error but install apache2 package on ubuntu machine. Lets run it again:

lets run the command again with yum module:

state=latest not only install the package if it is not there , but it will update it if it is there with an older version!

removing a package

lets remove apache2 from ubuntu machine:

enabling a service

Lets enable httpd service on all hosts:

There is no apache2 service on centos so it couldn't start it, but it enabled apache2 service on ubuntu!!! we have already removed that! how it is possible?

We can also start a service withansible all -m service -a "name=httpd state=started" command.

File system Management using ad-hoc commands

Ansible can interact with system but it can also interact with remote file system on all of the computers. There are different modules which behave in suddenly different ways. command module, shell module, raw module, Lets compare them

command Module

shell module

raw module

doesn't use shell (bash/sh)

supports pipes and redirects

just sends commands over SSH

Can't use pipes or redirects

Can get messed up by user settings(/etc/bashrc)

doesn't need Python

safest

safer

safe

and check the results on one of target machines:

As you can see command modules does not support redirection.

file module: it gives the power of file management to us, lets remove a file:

and run it again:

Copy module: We can send files from one server to the other one:

The ad-hoc system is powerful with some limitations. Keep reading to see other Ansible options and more complicated things with play-books.

.

.

.

https://www.guru99.com/ansible-tutorial.html#6

https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html

.

Last updated

Was this helpful?