• ansible 서버에서 ansible 클라이언트에 따라 동적으로 할당한 변수를 의미
  • ansible 클라이언트의 OS, IP, Hostname 등 다양한 정보를 변수로 저장

1. FACT(s) 확인 방법

  • ansible 클라이언트가 제공하는 동적 변수를 확인 가능
    $ ansible all -m setup
    192.168.1.13 | SUCCESS => {
        "ansible_facts": {
            "ansible_all_ipv4_addresses": [
                "192.168.1.13"
            ],
            "ansible_all_ipv6_addresses": [
                "fe00:1429:2222:31f5:fcf3"
            ],
            "ansible_apparmor": {
                "status": "disabled"
            },
            "ansible_architecture": "x86_64",
            "ansible_bios_date": "03/07/2013",
            "ansible_bios_version": "1.6.0",
            "ansible_cmdline": {
                "BOOT_IMAGE": "/vmlinuz-3.10.0-1160.11.1.el7.x86_64",
                "LANG": "en_US.UTF-8",
                "crashkernel": "auto",
                "quiet": true,
                "rhgb": true,
                "ro": true,
                "root": "UUID=c82653ea-81c1-4ed6-b566-e02cfe1f6ad4"
            },
    [... 생략]

2. FACT(s) 동적 변수를 사용 방법

  • Ansible Playbook YML 파일을 작성할 때 기본이 되는 "gather_facts:no" 부분을 주석 처리 → 기본 설정이 "gather_facts=yes"
  • "gather_facts:yes"로 표시하여 gather_facts 동적 변수 사용 명시
  • gather_fact:no 가 YML 파일에 정의되어있으면 FACT(s)를 수집하지 않도록 하여 ansible의 성능을 향상시킬 수 있음

3. FACT(s)를 사용한 예

  1. debug → 제대로 실행되었는지 알고싶을 때 사용하는 옵션

    • debug 없이 playbook을 실행하였을 때 성공했으면 ok, 실패했으면 failed, 변화가 발생하면 change 출력 → 정상적으로 실행되었는지 여부만 확인 가능
    • debug는 더 자세한 실행과정들을 알고 싶을 때 사용하는 모듈 → 표기된 메세지나 변수를 콘솔에 출력하는 역할
  2. msg → 변수들을 msg형태로 출력

  3. var → Ansible의 playbook은 작성 시 일반 프로그래밍 언어와 같은 변수(var)입력 가능

  4. {{ items }} → with_items: 에서 설정한 변수들을 하나씩 사용 가능

  5. inventory_hostname → /etc/ansible/hosts 에 정의된 hostname을 사용 가능

    # ansible 클라이언트의 ip를 출력하는 playbook 코드
    $ vi ip_address.yml
    ---
    - name: print ipv4 address for ansible client
     hosts: all
     #gather_facts: no
    
     tasks:
       - name: debug by msg
         debug:
           msg:
             - "ip is {{ ansible_all_ipv4_addresses[0] }}"
    
       - name: debug by var
         debug:
           var: "{{ item }}"
         with_items:
           - hostvars[inventory_hostname]['ansible_default_ipv4']['address']

Ansible에서 배열과 같은 리스트를 주어 주고 반복할 때 with_items를 사용하면 편함

  • with_items에 리스트로 추가한 항목들이 item이라는 변수에 하나씩 들어와서 사용 가능
  • 반복할 항목은 with_items에 key: value 형태로 배열로 추가
  • 각 항목의 값은 ``으로 사용

4. FACT(s) 실행 출력 결과

$ ansible-playbook ip_address.yml

PLAY [print ipv4 address for ansible client] **************************************************************

TASK [Gathering Facts] ******************************************************************************
ok: [192.168.1.13]

TASK [debug by msg] *******************************************************************************
ok: [192.168.1.13] => {
    "msg": [
        "ip is 192.168.1.13"
    ]
}

TASK [debug by var] ********************************************************************************
ok: [192.168.1.13] => (item=hostvars[inventory_hostname]['ansible_default_ipv4']['address']) => {
    "ansible_loop_var": "item",
    "hostvars[inventory_hostname]['ansible_default_ipv4']['address']": "192.168.1.13",
    "item": "hostvars[inventory_hostname]['ansible_default_ipv4']['address']"
}

PLAY RECAP ****************************************************************************************
192.168.1.13              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

+ Recent posts