[ANSIBLE] facts 팩트 기본
Posted by Albert 288Day 16Hour 56Min 18Sec ago [2024-07-05]
팩트는 앤서블 관리 호스트에서 자동으로 검색한 변수 리스트
1. web 호스트서버의 전체팩트 정보 수집하는 플레이북 생성 및 확인
- hosts: web
tasks:
- name: Print facts info
ansible.builtin.debug:
var: ansible_facts
실행(web 서버의 전체 팩트내용을 확인할수있는 json 값들 확인할수있다.)
[root@asimblecontroller asimble]' ansible-playbook facts.yml
PLAY [web] ******************************************************************** *
TASK [Gathering Facts] ******************************************************** *
ok: [tnode1]
TASK [Print facts info] ******************************************************* *
ok: [tnode1] => {
"ansible_facts": {
"all_ipv4_addresses": [
"192.168.200.154",
"192.168.122.1"
],
"all_ipv6_addresses": [
"fe80::b0f3:82f6:e834:a726"
],
"ansible_local": {},
"apparmor": {
"status": "disabled"
},
"architecture": "x86_64",
"bios_date": "12/01/2006",
"bios_version": "VirtualBox",
"cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-1160.119.1.el7.x86_64",
"LANG": "ko_KR.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rd.lvm.lv": "centos/swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root"
},
......
"virtualization_role": "guest",
"virtualization_type": "virtualbox"
}
}
PLAY RECAP ******************************************************************** *
tnode1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2. fact의 특정항목 출력하는 플레이북 생성
[root@asimblecontroller asimble]' vi facts1.yml
- hosts: web
tasks:
- name: Print facts info
ansible.builtin.debug:
msg: >
The default IPv4 address of {{ ansible_facts.fqdn }}
is {{ ansible_facts.default_ipv4.address }}
And kernel is {{ ansible_kernel }}
해당 플레이북 실행시 web서버 host 명, ipv4 주소정보 및 kernel 정보를 출력하는부분 확인할수 있다.
[root@asimblecontroller asimble]' ansible-playbook facts1.yml
PLAY [web] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [tnode1]
TASK [Print facts info] ********************************************************
ok: [tnode1] => {
"msg": "The default IPv4 address of tnode1 is 192.168.200.154 And kernel is 3.10.0-1160.119.1.el7.x86_64\n"
}
PLAY RECAP *********************************************************************
tnode1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
자주사용되는 앤서블 팩트 정보
팩트 | ansible_facts |
호스트명 | ansible_facts.hostname |
도메인기반 호스트명 | ansible_facts.fqdn |
IPv4 | ansible_facts.default_ipv4.address |
네트워크 인터페이스 리스트 | ansible_facts.interfaces |
/dev/vda1 디스크 파티션 크기 | ansible_facts.device.vda.partitions.vda1.size |
DNS 서버 목록 | ansible_facts.dns.nameservers |
현재 실행중인 커널 | ansible_facts.kernel |
운영체제 종류 | ansible_facts.distribution |
팩트의 간략한 네임스페이스 표기법 (ansible_facts. -> ansible_로 대체)
ansible_facts.* | ansible_* |
ansible_facts.hostname | ansible_hostname |
ansible_facts.fqdn | ansible_fqdn |
ansible_facts.default_ipv4.address | ansible_default_ipv4.address |
ansible_facts.interfaces | ansible_interfaces |
ansible_facts.device.vda.partitions.vda1.size | ansible_device.vda.partitions.vda1.size |
ansible_facts.dns.nameservers | ansible_dns.nameservers |
ansible_facts.kernel | ansible_kernel |
ansible_facts.distribution | ansible_distribution |
facts 비활성화
[root@asimblecontroller asimble]' vi ansible.cfg
[defaults]
inventory = ./inventory
remote_user = root
ask_pass = false
inject_facts_as_vars = false
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
플레이북 내부에서 강제 facts 비활성화
[root@asimblecontroller asimble]' vi facts2.yml
- hosts: web
gather_facts: no
tasks:
- name: Print facts info
ansible.builtin.debug:
msg: >
The default IPv4 address of {{ ansible_facts.fqdn }}
is {{ ansible_facts.default_ipv4.address }}
And kernel is {{ ansible_kernel }}
매뉴얼한 방법으로 팩트수집
- hosts: web
gather_facts: no
tasks:
- name: Manually gather facts
ansible.builtin.setup:
- name: Print all facts
ansible.builtin.debug:
msg: >
The default IPv4 address of {{ ansible_facts.fqdn }}
is {{ ansible_facts.default_ipv4.address }}
And kernel is {{ ansible_kernel }}
사용자 커스텀 팩트만들기
앤서블은 /etc/ansible/facts.d 폴더상 .fact 파일의 내용을 자동으로 팩트수집처리한다.
우선 /etc/ansible/facts.d 폴더 생성후 custom.fact 파일생성한다
[root@asimblecontroller asimble]' mkdir /etc/ansible/facts.d
[root@asimblecontroller facts.d]' vi /etc/ansible/facts.d/custom.fact
[webPackages]
lang = java
ui = vue
framework = springboot
[dbUsers]
user1 = albert
user2 = kara
커스텀 팩트확인 플레이북 생성
[root@asimblecontroller asimble]' vi facts5.yml
- hosts: localhost
tasks:
- name: Show all localhost custom facts
ansible.builtin.debug:
var: ansible_local
플레이북 실행 및 결과 확인
[root@asimblecontroller asimble]' ansible-playbook facts5.yml
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Show all localhost custom facts] *****************************************
ok: [localhost] => {
"ansible_local": {
"custom": {
"dbUsers": {
"user1": "albert",
"user2": "kara"
},
"webPackages": {
"framework": "springboot",
"lang": "java",
"ui": "vue"
}
}
}
}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
끝