Ansible 2.9 ad-hoc commands

ComputerCarriage > Posts > Ansible > Ansible 2.9 ad-hoc commands
ansible-adhoc

1. Ansible Introduction

2. Pre-requisite

1. Inventory file

The below example file is for the ansible inventory, We have a webserver group in that server2(managed node) and dbserver group has server1(managed node)

devops@devops:~/ansible$ cat inventory
[webserver]
server2
[dbserver]
server1
devops@devops:~/ansible$

2. Ansible configuration file

We have a simple ansible configuration file, devops is a ansible user and specified the inventory file location and roles path, we will go through more in detail in upcoming posts.

remote_user=devops
inventory=/home/devops/ansible/inventory
roles_path=/home/devops/ansible/roles
deprecation_warnings=False
[privilege_escalation]
become=True

3. Check the server online status

Run the basic ad-hoc commands by using the module ping, using the module ping we execute for all the servers which are added in the inventory file.

devops@devops:~/ansible$ ansible all -m ping
server1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
server2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
devops@devops:~/ansible$

We could see the SUCCESS, that means the servers is online, and also we can see pong in return

To check the module list use ansible-doc -l to list all modules in ansible

ansible-doc -l
ansible-doc ping

4. Gather facts

Setup module will gather facts information like processor, RAM, disks, IP Address and much more. we can get variable for remote machines using the ansible facts.

devops@devops:~/ansible$ ansible server1 -m setup | more
server1 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.43.28",
"172.17.0.1"
],
"ansible_all_ipv6_addresses": [
"fe80::7fc:897c:a19e:da26",
"fe80::42:f6ff:fe9e:5104",
"fe80::e0af:59ff:fe90:73e5"
],
"ansible_apparmor": {
"status": "disabled"
},

a. Gather facts for processor

Get the custom facts, like for a particular variables, for example the below example will get the processor detail for the managed nodes

devops@devops:~/ansible$ ansible all -m setup -a "filter=ansible_processor"
server2 | SUCCESS => {
"ansible_facts": {
"ansible_processor": [
"0",
"GenuineIntel",
"Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz"
],
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
server1 | SUCCESS => {
"ansible_facts": {
"ansible_processor": [
"0",
"GenuineIntel",
"Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz"
],
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
devops@devops:~/ansible$

In the above example all is for all servers in inventory file, setup is a module name, -a is for the argument option, ansible_processor is argument.

b. OS family

Gather facts for the Operating system family, This command will be useful for executing yum or apt commands or operating system specific, if we want to know the Operating system running on which distribution

devops@devops:~/ansible$ ansible all -m setup -a "filter=ansible_os_family"
server1 | SUCCESS => {
"ansible_facts": {
"ansible_os_family": "RedHat",
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
server2 | SUCCESS => {
"ansible_facts": {
"ansible_os_family": "Debian",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
devops@devops:~/ansible$

c. Gather facts distribution specific

Get facts for the Operating system version and distribution specific, this will be helpful to find out which os distribution has been installed

The below output shows the Operating system which is installed on the each managed node

devops@devops:~/ansible$ ansible all -m setup -a "filter=ansible_distribution"
server2 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "Ubuntu",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
server1 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "CentOS",
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
devops@devops:~/ansible$

In above example we can see server1 is installed with CentOS and server2 is installed with Ubuntu, using setup module we can get lot of Hardware related information that will be useful to get hardware inventory.

5. One time commands

a. Uptime

If we need to get the uptime or load average for the server using the adhoc commands

devops@devops:~/ansible$ ansible all -m shell -a "uptime"
server1 | CHANGED | rc=0 >>
09:45:00 up 39 min, 2 users, load average: 0.05, 0.05, 0.06
server2 | CHANGED | rc=0 >>
13:44:59 up 3:04, 2 users, load average: 0.00, 0.00, 0.00
devops@devops:~/ansible$

b. Kernel Version

Check the kernel version of the servers using the shell module

devops@devops:~/ansible$ ansible all -m shell -a "uname -r"
server2 | CHANGED | rc=0 >>
4.15.0-99-generic
server1 | CHANGED | rc=0 >>
4.18.0-147.8.1.el8_1.x86_64
devops@devops:~/ansible$

c. Package Installation

Below out shows the samba package installation,

devops@devops:~/ansible$ ansible all -m apt -a "name=samba state=present"
server1 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"cmd": "apt-get update",
"msg": "[Errno 2] No such file or directory: b'apt-get': b'apt-get'",
"rc": 2
}
server2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"cache_update_time": 1588172481,
"cache_updated": false,
"changed": false
}
devops@devops:~/ansible$

We could see server1 package installation is failed since the server1 is installed with CentOS which uses yum module.

d. Removing a package

Removing the package using apt module, you can use yum depending upon the OS version you are using.

devops@devops:~/ansible$ ansible server2 -m apt -a "name=smbd state=absent"
server2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
devops@devops:~/ansible$

e. Script execution

If we want to execute particular script on a specific server onetime we can use the command module, In the below example we have backup.sh file that has to be executed

devops@devops:~/ansible$ ansible server2 -m command -a "sh /home/devops/backup.sh"
server2 | CHANGED | rc=0 >>
Mon May 4 14:02:39 UTC 2020
server2
14:02:39 up 5 min, 1 user, load average: 0.05, 0.19, 0.12
devops@devops:~/ansible$

There are lot of onetime usage, according to the requirement, we can easily get the information of remote machines with logging in and time consuming. It also avoids manual errors. Hope you have got some idea how to use ansible ad-hoc commands. let us know your feedback and share your inputs.

Also Refer

Easy Install Ansible – https://computercarriage.com/2020/06/07/install-ansible-command-line/

Ansible Playbook – https://computercarriage.com/2020/05/21/ansible-playbook-for-package/

Redhat Official Ansible Doc – https://docs.ansible.com

Leave a Reply