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]"
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?
Service module does not make sure that the service exist! So sometimes you should make sure what state your system is in!
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.
[user1@controller demo-adhoc]$ ansible all -m command -a "reboot"
centos | FAILED | rc=2 >>
[Errno 2] No such file or directory
ubuntu | FAILED | rc=1 >>
Failed to set wall message, ignoring: Interactive authentication required.
Failed to reboot system via logind: Interactive authentication required.
Failed to open /dev/initctl: Permission denied
Failed to talk to init daemon.non-zero return code
[user1@controller demo-adhoc]$ ansible all -b -a "reboot"
centos | FAILED | rc=-1 >>
Failed to connect to the host via ssh: ssh: connect to host centos port 22: Connection refused
ubuntu | FAILED | rc=-1 >>
Failed to connect to the host via ssh: ssh: connect to host ubuntu port 22: Connection refused