Site icon UnixArena

Ansible – How to Gather facts on Remote Server ?

Ansible Gather Facts - Setup Module

Ansible Gather Facts - Setup Module

Ansible  – “setup” module is responsible to gather facts of the remote hosts. The system facts are nothing but the system configuration which includes the hostname, IP address, filesystems, OS releases, Users, Network parameters, CPU, memory and many more. This module is automatically included in all the playbooks to gather useful variables which can be used to create the dynamic inventory or perform the specific tasks. There is a way to write the custom facts about the hosts to filter further. Let’s start exploring it.

Assumptions:

 

Ansible – SETUP module:

1. Here is the temporary inventory file.

[linadm@ansible-server automation]$ cat lin-servers.1
gpfslinapp1
[linadm@ansible-server automation]$

 

2. To get the facts of the remote hosts, use the following command.

[linadm@ansible-server automation]$ ansible -m  setup -i lin-servers.1 all 
gpfslinapp1 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.3.151"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::5af3:5374:a618:9c07"
        ],
        "ansible_apparmor": {
            "status": "disabled"


<<>>

 

3. If you would like to run the remote facts on the specific hosts from the inventory, use the following command.

[linadm@ansible-server automation]$ ansible -m  setup -i lin-servers.1 gpfslinapp1
gpfslinapp1 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.3.151"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::5af3:5374:a618:9c07"
        ],
        "ansible_apparmor": {
            "status": "disabled"
        },
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "05/20/2014",
        "ansible_bios_version": "6.00",
        "ansible_cmdline": {
            "BOOT_IMAGE": "/vmlinuz-3.10.0-862.3.3.el7.x86_64",
            "LANG": "en_US.UTF-8",

 

“setup” Module – Use Filter in Adhoc Mode:

1. Limit the setup module facts, use “filter” option. The following command filters the currently mounted filesystems on the remote host.

[linadm@ansible-server automation]$ ansible -m  setup -i lin-servers.1 gpfslinapp1 -a 'filter=ansible_mounts'
gpfslinapp1 | SUCCESS => {
    "ansible_facts": {
        "ansible_mounts": [
            {
                "block_available": 4250037,
                "block_size": 4096,
                "block_total": 4638976,
                "block_used": 388939,
                "device": "/dev/sda3",
                "fstype": "xfs",
                "inode_available": 9240033,
                "inode_total": 9283072,
                "inode_used": 43039,
                "mount": "/",
                "options": "rw,relatime,attr2,inode64,noquota",
                "size_available": 17408151552,
                "size_total": 19001245696,
                "uuid": "1abdbd4f-d020-4b23-a68d-5518b98e7ec0"
            },
            {
                "block_available": 36413,
                "block_size": 4096,
                "block_total": 75945,
                "block_used": 39532,
                "device": "/dev/sda1",
                "fstype": "xfs",
                "inode_available": 153266,
                "inode_total": 153600,
                "inode_used": 334,
                "mount": "/boot",
                "options": "rw,relatime,attr2,inode64,noquota",
                "size_available": 149147648,
                "size_total": 311070720,
                "uuid": "ebf6278a-2098-493e-8e3a-7f7d8d4841d5"
            }
        ]
    },
    "changed": false
}
[linadm@ansible-server automation]$

2. To filter the remote host’s OS distribution, use the following ansible variable.

[linadm@ansible-server automation]$ ansible -m  setup -i lin-servers.1 gpfslinapp1 -a 'filter=ansible_distribution'
gpfslinapp1 | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution": "CentOS"
    },
    "changed": false
}
[linadm@ansible-server automation]$

Classifying the hosts using the “setup” Module:

1.  To find out the running kernel on all the hosts, use the following command.

[linadm@ansible-server automation]$ ansible -m setup -i lin-servers.1 all -a 'filter="ansible_kernel"' -o
gpfslinapp1 | SUCCESS => {"ansible_facts": {"ansible_kernel": "3.10.0-862.3.3.el7.x86_64"}, "changed": false}
uaans69     | SUCCESS => {"ansible_facts": {"ansible_kernel": "2.6.32-696.20.1.el6.x86_64"},"changed": false}
[linadm@ansible-server automation]$

 

Note: “uaans69” – host has been added into the inventory.

 

Hope this article is informative to you. Share it! Comment it! Be Sociable!

Exit mobile version