Ad hoc commands

Sometimes you want to execute a single command/task on the compute nodes. These single tasks are known as ad hoc commands in Ansible. In such commands, the option -m stands for module, without this option the default module to be executed is command. Arguments can be passed to the modules with the option -a. For instance, let’s execute the command hostname in all compute nodes at once:

[root@master ~]# ansible compute -i inventory.yml -a 'hostname'
c02 | SUCCESS | rc=0 >>
c02

c03 | SUCCESS | rc=0 >>
c03

c04 | SUCCESS | rc=0 >>
c04

c01 | SUCCESS | rc=0 >>
c01

This is an additional example with the module service, please see the playbook version here for the case of the httpd daemon:

[root@master ~]# ansible master -i inventory  -m service -a "name=httpd state=started enabled=yes"

Finally, another example with the module yum to install the package smartmontools:

[root@master ~]# ansible compute -i inventory  -o -m yum -a "name=smartmontools state=latest"

The option -o is used here to condense the output of the command.