소개
사용자 지정 팩트 (Custom facts) 를 사용하면 관리 대상 호스트가 로컬 데이터를 Ansible 에 게시할 수 있습니다. /etc/ansible/facts.d 디렉터리에 배치된 팩트는 팩트 수집 (fact gathering) 후 ansible_local을 통해 사용할 수 있게 됩니다.
이 실습에서는 간단한 사용자 지정 팩트 파일을 설치하고, 두 번째 플레이에서 이를 사용하여 보고서를 생성하는 방법을 실습합니다.
사용자 지정 팩트 프로젝트 생성
이 단계에서는 사용자 지정 팩트를 설치하고 읽기 위한 프로젝트를 생성합니다.
cd /home/labex/project
mkdir -p rhce-ansible/{inventory,files,playbooks,reports}
cd rhce-ansible
cat > ansible.cfg <<'EOF'
[defaults]
inventory = ./inventory/hosts
host_key_checking = False
retry_files_enabled = False
EOF
cat > inventory/hosts <<'EOF'
[managed]
host ansible_connection=local
EOF
사용자 지정 팩트는 관리 대상 호스트에 설치되므로, 이 실습에서는 프로젝트 내의 소스 파일과 이를 /etc/ansible/facts.d로 복사하는 플레이북이 모두 필요합니다.
로컬 팩트 소스 파일 생성
이 단계에서는 INI 스타일의 팩트 파일을 생성합니다. 섹션 이름과 키는 ansible_local.rhce_custom.general 아래에 나타나게 됩니다.
이 단계의 명령을 실행하기 전에 Ansible 프로젝트 디렉터리로 돌아가십시오. 이렇게 하면 LabEx 가 각 단계를 개별적으로 실행하더라도 명령이 안정적으로 유지됩니다.
cd /home/labex/project/rhce-ansible
cat > files/rhce_custom.fact <<'EOF'
[general]
course=rhce-ansible
track=custom-facts
EOF
이 파일은 INI 스타일 구문을 사용합니다. 팩트 수집 후, general 섹션 이름과 그 키들은 ansible_local.rhce_custom.general 아래에서 사용할 수 있게 됩니다.
cat files/rhce_custom.fact
팩트를 설치하고 읽는 플레이북 작성
이 단계에서는 두 개의 플레이로 구성된 플레이북을 작성합니다. 첫 번째 플레이는 권한 상승 (privilege escalation) 을 사용하여 팩트 파일을 설치하고, 두 번째 플레이는 팩트를 다시 수집하여 ansible_local에 새로운 데이터가 포함되도록 합니다.
이 단계의 명령을 실행하기 전에 Ansible 프로젝트 디렉터리로 돌아가십시오.
cd /home/labex/project/rhce-ansible
cat > playbooks/custom-fact.yml <<'EOF'
---
- name: Install custom fact
hosts: managed
become: true
gather_facts: false
tasks:
- name: Ensure facts directory exists
ansible.builtin.file:
path: /etc/ansible/facts.d
state: directory
mode: '0755'
- name: Install RHCE custom fact
ansible.builtin.copy:
src: ../files/rhce_custom.fact
dest: /etc/ansible/facts.d/rhce_custom.fact
mode: '0644'
- name: Read custom fact after gathering facts
hosts: managed
gather_facts: true
tasks:
- name: Write custom fact report
ansible.builtin.copy:
dest: /home/labex/project/rhce-ansible/reports/custom-fact.txt
content: |
course={{ ansible_local.rhce_custom.general.course }}
track={{ ansible_local.rhce_custom.general.track }}
mode: '0644'
EOF
첫 번째 플레이는 /etc/ansible/facts.d가 시스템 경로이므로 become을 사용하여 팩트 파일을 설치합니다. 두 번째 플레이는 팩트를 다시 수집합니다. 이 과정이 없으면 동일한 실행 내에서 ansible_local에 새로 설치된 팩트가 포함되지 않습니다.
ansible-playbook playbooks/custom-fact.yml --syntax-check
사용자 지정 팩트 플레이북 실행
이 단계에서는 사용자 지정 팩트를 설치하고 ansible_local에서 보고서를 생성합니다. 첫 번째 플레이는 시스템 팩트 저장소를 수정하고, 두 번째 플레이는 Ansible 이 이를 읽을 수 있음을 증명합니다.
이 단계의 명령을 실행하기 전에 Ansible 프로젝트 디렉터리로 돌아가십시오.
cd /home/labex/project/rhce-ansible
ansible-playbook playbooks/custom-fact.yml | tee reports/custom-fact-run.txt
첫 번째 실행 시 팩트 파일을 설치하고 보고서를 작성하여 시스템을 변경해야 합니다. 두 번째 플레이에서 ansible_local을 찾을 수 없는 경우 저장된 출력이 유용하게 사용됩니다.
cat reports/custom-fact.txt
보고서에는 로컬 팩트 파일의 course 및 track 값이 포함되어 있어야 하며, 이는 Ansible 이 사용자 지정 팩트를 성공적으로 수집하고 사용했음을 증명합니다.
ansible_local 직접 확인
이 단계에서는 필터와 함께 setup 모듈을 사용하여 사용자 지정 팩트 트리를 직접 확인합니다.
이 단계의 명령을 실행하기 전에 Ansible 프로젝트 디렉터리로 돌아가십시오.
cd /home/labex/project/rhce-ansible
ansible managed -m ansible.builtin.setup -a 'filter=ansible_local' | tee reports/ansible-local.txt
setup 모듈의 출력은 원시 팩트 트리입니다. ansible_local로 필터링하면 로컬에서 제공된 사용자 지정 팩트에 집중하여 결과를 볼 수 있습니다.
grep -E 'rhce_custom|custom-facts|rhce-ansible' reports/ansible-local.txt
grep 출력에는 사용자 지정 팩트 이름과 작성한 값이 표시되어야 합니다. 이는 플레이북에서 ansible_local을 인식하지 못할 때 사용하는 직접적인 확인 방법입니다.
요약
호스트 로컬 사용자 지정 팩트를 설치하고, 팩트를 다시 수집하고, 플레이북에서 ansible_local을 사용하고, setup 모듈을 통해 사용자 지정 팩트를 검사하는 방법을 배웠습니다.


