Ansible uses ad-hoc to perform tasks

In the previous article I installed Ansible on Ubuntu, in this article I will discuss how to use Ansible.

There are two ways for Ansible to perform tasks, ad-hoc and playbook. Here I will show how to use ad-hoc to perform tasks.

Create directories

Create an ad-hoc folder to store ansible.cfg / hosts and logs. Create a new logs folder in it.

root@haifeli-ubuntu-2:~# mkdir ad-hoc
root@haifeli-ubuntu-2:~# cd ad-hoc
root@haifeli-ubuntu-2:~/ad-hoc#
root@haifeli-ubuntu-2:~/ad-hoc# mkdir logs
root@haifeli-ubuntu-2:~/ad-hoc# ls
logs
root@haifeli-ubuntu-2:~/ad-hoc#

New ansible.cfg file

This is Ansible configuration file, there are many options, the following is my simple configuration.

root@haifeli-ubuntu-2:~/ad-hoc# vi ansible.cfg
:wq

root@haifeli-ubuntu-2:~/ad-hoc# cat ansible.cfg
[defaults]
inventory = ./hosts
sudo_user = root
remote_port = 22
host_key_checking = False
remote_user = root
log_path = ./logs/ansible.log
module_name = raw
root@haifeli-ubuntu-2:~/ad-hoc#

inventory: hosts file path
sudo_user: The user who executing the Ansible command.
remote_port: SSH connection port, generally the default is 22.
host_key_checking: When we connect to an SSH client for the first time, will be prompted to enter Yes / No (related to the fingerprint key). This setting can skip this step.
log_path: log file path.
module_name: The default is “command”. If the device does not support Python, we need to use “raw”. We can add “-m raw” to the command instead.

If host_key_checking is not configured, the following error may occur.

Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host.

New hosts file

The hosts file stores host information, such as IP addresses.

root@haifeli-ubuntu-2:~/ad-hoc# vi hosts
:wq
root@haifeli-ubuntu-2:~/ad-hoc#
root@haifeli-ubuntu-2:~/ad-hoc# cat hosts
192.168.1.111
root@haifeli-ubuntu-2:~/ad-hoc#

Verification

Use the command “show ip int brief” to query the interface information.

root@haifeli-ubuntu-2:~/ad-hoc# ansible 192.168.1.111 -a "show ip int brief" -u admin -k
SSH password:
192.168.1.111 | CHANGED | rc=0 >>

Interface              IP-Address      OK? Method Status                Protocol
Te0/0/0                unassigned      YES unset  down                  down
Te0/0/1                unassigned      YES unset  down                  down
Te0/0/2                unassigned      YES unset  down                  down
Te0/0/3                unassigned      YES unset  down                  down
Te0/0/4                unassigned      YES unset  down                  down
Te0/0/5                unassigned      YES unset  down                  down
Te0/0/6                unassigned      YES unset  up                    up
Te0/0/7                unassigned      YES unset  up                    up
GigabitEthernet0       unassigned      YES NVRAM  up                    up
Capwap2                unassigned      YES unset  up                    up
Vlan1                  unassigned      YES NVRAM  up                    down
Vlan62                 192.168.1.111   YES NVRAM  up                    up      Shared connection to 192.168.1.111 closed.

root@haifeli-ubuntu-2:~/ad-hoc#

-a: The command will be executed.
-u: SSH username.
-k: Will prompt for password.

Confirm the log.

root@haifeli-ubuntu-2:~/ad-hoc# cat logs/ansible.log
2020-05-01 03:39:24,244 p=46981 u=root n=ansible | 192.168.1.111 | CHANGED | rc=0 >>

Interface              IP-Address      OK? Method Status                Protocol
Te0/0/0                unassigned      YES unset  down                  down
Te0/0/1                unassigned      YES unset  down                  down
Te0/0/2                unassigned      YES unset  down                  down
Te0/0/3                unassigned      YES unset  down                  down
Te0/0/4                unassigned      YES unset  down                  down
Te0/0/5                unassigned      YES unset  down                  down
Te0/0/6                unassigned      YES unset  up                    up
Te0/0/7                unassigned      YES unset  up                    up
GigabitEthernet0       unassigned      YES NVRAM  up                    up
Capwap2                unassigned      YES unset  up                    up
Vlan1                  unassigned      YES NVRAM  up                    down
Vlan62                 192.168.1.111   YES NVRAM  up                    up      Shared connection to 192.168.1.111 closed.

root@haifeli-ubuntu-2:~/ad-hoc#

Advanced

In the above example, only one device executes commands, but in fact we may need to execute commands on multiple devices at the same time – this can be achieved by editing the hosts file.

I divided the devices into two groups – [C9800] and [CSR1000V].

root@haifeli-ubuntu-2:~/ad-hoc# cat hosts
[C9800]
10.1.1.111
10.1.1.9

[CSR1000V]
10.1.1.53
root@haifeli-ubuntu-2:~/ad-hoc#

The following commands can execute commands for specific groups.

root@haifeli-ubuntu-2:~/ad-hoc# ansible CSR1000V -a "show clock det" -u admin -k
SSH password:
10.1.1.53 | CHANGED | rc=0 >>

*05:38:15.596 UTC Fri May 1 2020
No time sourceShared connection to 10.1.1.53 closed.

root@haifeli-ubuntu-2:~/ad-hoc# ansible C9800 -a "show clock det" -u admin -k
SSH password:
10.1.1.111 | CHANGED | rc=0 >>

*00:00:14.693 UTC Fri May 1 2020
No time sourceShared connection to 10.1.1.111 closed.

10.1.1.9 | CHANGED | rc=0 >>

05:38:27.896 UTC Fri May 1 2020
No time sourceShared connection to 10.1.1.9 closed.

root@haifeli-ubuntu-2:~/ad-hoc#

If we need to execute commands on all groups, we can use “all” parameter.

root@haifeli-ubuntu-2:~/ad-hoc# ansible all -a "show clock det" -u admin -k
SSH password:
10.1.1.111 | CHANGED | rc=0 >>

*00:02:22.810 UTC Fri May 1 2020
No time sourceShared connection to 10.1.1.111 closed.

10.106.53.53 | CHANGED | rc=0 >>

*05:40:34.088 UTC Fri May 1 2020
No time sourceShared connection to 10.1.1.53 closed.

10.1.1.9 | CHANGED | rc=0 >>

05:40:36.012 UTC Fri May 1 2020
No time sourceShared connection to 10.1.1.9 closed.

root@haifeli-ubuntu-2:~/ad-hoc#

References

Introduction to ad-hoc commands

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

Leave a Reply

Your email address will not be published.