본문 바로가기

리눅스

[Ansible] facts 모듈(facts module)

반응형

Ansible facts 모듈(facts module)

 : 원격 호스트에 대한 정보 수집

 

kube-node1에 대한 정보 수집

ansible -i ~/ansible-spec/inventory kube-node1 -m gather_facts
$ ansible -i ~/ansible-spec/inventory -m setup kube-node1
kube-node1 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "172.17.0.1",
            "169.254.25.10",
            "10.233.0.1",
            "10.233.0.3",
            "10.233.51.47",
            "192.168.0.61",
            "10.233.73.64"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::a00:27ff:fe55:8f2e",
            "fe80::6456:23ff:fe80:b40f",
            "fe80::ecee:eeff:feee:eeee"
        ],
        "ansible_apparmor": {
            "status": "enabled"
        },
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "12/01/2006",
        "ansible_bios_vendor": "innotek GmbH",
        "ansible_bios_version": "VirtualBox",
        "ansible_board_asset_tag": "NA",
        "ansible_board_name": "VirtualBox",
        "ansible_board_serial": "NA",
        "ansible_board_vendor": "Oracle Corporation",
        "ansible_board_version": "1.2",
        "ansible_calib776ad321a9": {
            "active": true,
            "device": "calib776ad321a9",
            "features": {
                "esp_hw_offload": "off [fixed]",
                "esp_tx_csum_hw_offload": "off [fixed]",
                "fcoe_mtu": "off [fixed]",
                "generic_receive_offload": "off",
                "generic_segmentation_offload": "on",
                "highdma": "on",
...
        "discovered_interpreter_python": "/usr/bin/python3",
        "gather_subset": [
            "all"
        ],
        "module_setup": true
    },
    "changed": false
}

수집한 정보를 /tmp/facts 디렉터리 하위에 파일로 저장

ansible -i ~/ansible-spec/inventory kube-node1 -m gather_facts --tree /tmp/facts
$ tree /tmp/facts/
/tmp/facts/
└── kube-node1

한 줄로 많은 내용이 출력되어 내용을 볼 수가 없다.

cat /tmp/facts/kube-node1

jq 명령을 사용하여 가독성을 높였다

cat /tmp/facts/kube-node1 | jq .
$ cat /tmp/facts/kube-node1 | jq .
{
  "ansible_facts": {
    "ansible_all_ipv4_addresses": [
      "172.17.0.1",
      "169.254.25.10",
      "10.233.0.1",
      "10.233.0.3",
      "10.233.51.47",
      "192.168.0.61",
      "10.233.73.64"
    ],
    "ansible_all_ipv6_addresses": [
      "fe80::a00:27ff:fe55:8f2e",
      "fe80::6456:23ff:fe80:b40f",
      "fe80::ecee:eeff:feee:eeee"
    ],
    "ansible_apparmor": {
      "status": "enabled"
    },
...
    "discovered_interpreter_python": "/usr/bin/python3",
    "gather_subset": [
      "all"
    ],
    "module_setup": true
  },
  "changed": false,
  "deprecations": [],
  "warnings": []
}

gatherFacts.yaml 파일 작성

vim gatherFacts.yaml
---
- name: gather facts
  hosts: all

  tasks:
  - name: Print all available facts
    ansible.builtin.debug:
      var: ansible_facts

  - name: Print the package facts
    ansible.builtin.debug:
      var: ansible_facts.all_ipv4_addresses

ansible-playbook 실행

ansible-playbook -i ~/ansible-spec/inventory gatherFacts.yaml --limit "kube-node1"
$ ansible-playbook -i ~/ansible-spec/inventory gatherFacts.yaml --limit "kube-node1"

PLAY [gather facts] ****************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [kube-node1]

TASK [Print all available facts] ***************************************************************************************
ok: [kube-node1] => {
    "ansible_facts": {
        "all_ipv4_addresses": [
            "172.17.0.1",
            "169.254.25.10",
            "10.233.0.1",
            "10.233.0.3",
            "10.233.51.47",
            "192.168.0.61",
            "10.233.73.64"
        ],
        "all_ipv6_addresses": [
            "fe80::a00:27ff:fe55:8f2e",
            "fe80::6456:23ff:fe80:b40f",
            "fe80::ecee:eeff:feee:eeee"
        ],
        "ansible_local": {},
        "apparmor": {
            "status": "enabled"
        },
...
            "hw_timestamp_filters": [],
            "ipv4": {
                "address": "10.233.73.64",
                "broadcast": "",
                "netmask": "255.255.255.255",
                "network": "10.233.73.64"
            },
            "ipv6": [
                {
                    "address": "fe80::6456:23ff:fe80:b40f",
                    "prefix": "64",
                    "scope": "link"
                }
            ],
            "macaddress": "66:56:23:80:b4:0f",
            "mtu": 1450,
            "promisc": false,
            "speed": 1000,
            "timestamping": [],
            "type": "ether"
        }
    }
}

TASK [Print the package facts] *****************************************************************************************
ok: [kube-node1] => {
    "ansible_facts.all_ipv4_addresses": [
        "172.17.0.1",
        "169.254.25.10",
        "10.233.0.1",
        "10.233.0.3",
        "10.233.51.47",
        "192.168.0.61",
        "10.233.73.64"
    ]
}

PLAY RECAP *************************************************************************************************************
kube-node1                 : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

참고URL

- [Ansible] setup 모듈(setup module) : https://scbyun.com/1002

- facts module : https://docs.ansible.com/ansible/latest/collections/ansible/builtin/gather_facts_module.html

- package facts module : https://docs.ansible.com/ansible/latest/collections/ansible/builtin/package_facts_module.html

- set_fact module : https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_fact_module.html

- setup module : https://docs.ansible.com/ansible/latest/collections/ansible/builtin/setup_module.html

 

728x90
반응형