본문 바로가기

DevOps/Ansible

[Ansible] Facts란

Facts란?

  • Ansible 에서 동적으로 할당되는 변수들을 의미한다.
  • 우리는 플레이북 사용시 [gather_facts: no] 설정으로 facts를 수집하지 않았다
  • facts를 수집하지 않음으로서 Ansible의 성능향상을 했다.

Fact 확인하기

  • host1의 facts를 확인해보기
ansible host -m setup

# 엄청 길게나온다. host1에대한 정보들이다.

# grep 명령어로 ansible_distribution 부분만 찾아보자

ansible host1 -m setup | grep ansible_distribution

  • 버전이 CentOS임을 알 수 있다. 이 변수는 Facts에서 지정된 변수값이기에 플레이북에서 변수 출력하듯이 출력이 가능하다.
vim fact.yml

---
- name: test facts variable
  hosts: all
  tasks:
    - name: print facts
      debug:
        msg: "{{  ansible_distribution }}"

...

[root@controoler ansible_example]# ansible-playbook fact.yml

PLAY [test facts variable] **********************************************************************************

TASK [Gathering Facts] **************************************************************************************
[WARNING]: error loading fact - please check content
ok: [host4]
ok: [host2]
ok: [host1]
ok: [host3]

TASK [print facts] ******************************************************************************************
ok: [host1] => {
    "msg": "CentOS"
}
ok: [host2] => {
    "msg": "CentOS"
}
ok: [host3] => {
    "msg": "CentOS"
}
ok: [host4] => {
    "msg": "CentOS"
}

PLAY RECAP **************************************************************************************************
host1                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
host2                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
host3                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
host4                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@controoler ansible_example]#

# host1 ~ 4 의 ansible_distribution이 CentOS임을 알 수 있다.


사용자 지정 팩트 생성하기

  • host1 노드 root 계정으로 접속을 한다.
  • 이후 /etc/ansible/facts.d를 생성
  • test.fact파일을 생성하자
mkdir -p /etc/ansible/facts.d

vim /etc/ansible/facts.d/test.fact
fact_vars: 111
  • 이후 컨트롤 노드로 이동해서 facts 값이 실제로 저장되었는지 확인해보자
ansible -m setup host1