Ad Hoc commands in ansible

With ad-hoc command you can run some Ansible action from the command line.

An example ad-hoc command:

$ ansible cisco-routers -i myinventory -m raw -a "Show Clock" -u yourlogin --ask-pass

This option basically is used in cases where you need to check something, for example, the operation of the module. Or just perform some one-time action that does not need to be saved.

Anyway, this is a simple and fast way to use Ansible.

First you need to create an inventory file in the local directory. Let’s call it myinventory:

[cisco-routers]
192.168.0.1
192.168.0.2
192.168.0.3

[cisco-switches]
192.168.0.100

When connecting to devices for the first time, it’s best to connect to them manually first so that the device keys are stored locally. Ansible has an option to disable this initial key checking. We will see how to do this in the section on the configuration file (this option may be needed if you need to connect to a large number of devices).

Let’s a look parameters of command:

$ ansible cisco-routers -i myinventory -m raw -a "Show Clock" -u yourlogin --ask-pass

cisco-routers – group of devices to apply actions to this group must exist in the inventory file

-i myinventory – the -i option allows you to specify an inventory file

-m raw -a “Show Clock” – the -m raw option means that the raw module is used

-a “Show Clock” – the -a option specifies which command to send

-u yourlogin – connect as user yourlogin

–ask-pass – a parameter that must be specified so that authentication is by password, not by keys

Leave a Reply

Your email address will not be published. Required fields are marked *