728x90
ansible 팩트변수, 매직변수
팩트변수(Facts Variables)
- setup 모듈을 자동으로 실행, 시스템에서 발견된 정보를 가지고 변수처럼 사용할 수 있는 것이 바로 fact
팩트변수 확인
ansible localhost -m setup -a "filter=*내용*"
ansible -i ./inventory kube-node1 -m setup -a "filter=ansible_distribution"
$ ansible -i ./inventory kube-node1 -m setup -a "filter=ansible_distribution"
kube-node1 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "Ubuntu",
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false
}
ansible -i ./inventory kube-node1 -m setup -a "filter=*ansible_distribution*"
$ ansible -i ./inventory kube-node1 -m setup -a "filter=*ansible_distribution*"
kube-node1 | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "Ubuntu",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/os-release",
"ansible_distribution_file_variety": "Debian",
"ansible_distribution_major_version": "22",
"ansible_distribution_release": "jammy",
"ansible_distribution_version": "22.04",
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false
}
$ ansible -i ./inventory kube-node1 -m setup | egrep ansible_distribution
"ansible_distribution": "Ubuntu",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/os-release",
"ansible_distribution_file_variety": "Debian",
"ansible_distribution_major_version": "22",
"ansible_distribution_release": "jammy",
"ansible_distribution_version": "22.04",
사용자 지정 팩트 설정
ansible config 설정(kube-control1)
vim ansible.cfg
fact_path=/etc/ansible/facts.d/
$ cat ansible.cfg | grep -C4 fact_path
# (string) This option allows you to globally configure a custom path for 'local_facts' for the implied M(ansible.builtin.setup) task when using fact gathering.
# If not set, it will fallback to the default from the M(ansible.builtin.setup) module: ``/etc/ansible/facts.d``.
# This does **not** affect user defined tasks that use the M(ansible.builtin.setup) module.
fact_path=/etc/ansible/facts.d/
# (pathspec) Colon separated paths in which Ansible will search for Jinja2 Filter Plugins.
;filter_plugins=~/.ansible/plugins/filter:/usr/share/ansible/plugins/filter
node 설정(kube-node1)
sudo mkdir /etc/ansible/facts.d/
date_time.fact 작성
vim date_time.fact
#!/usr/bin/bash
DATE=`date`
echo "{\"date\" : \"${DATE}\"}"
chmod +x /etc/ansible/facts.d/date_time.fact
test.fact 작성
vim test.fact
{"fact_key": "fact_value"}
ansible ad-hoc 명령 실행
ansible -i ./inventory kube-node1 -m setup -a "filter=ansible_local"
$ ansible -i ./inventory kube-node1 -m setup -a "filter=ansible_local"
kube-node1 | SUCCESS => {
"ansible_facts": {
"ansible_local": {
"date_time": {
"date": "Mon Nov 7 02:39:25 PM KST 2022"
},
"test": {
"fact_key": "fact_value"
}
},
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false
}
check_date.yml 작성
vim check_date.yml
---
- hosts: all
tasks:
- name: Get custom facts
debug:
msg: The custom fact is {{ansible_local.date_time}}
- name: get key:value
debug:
msg: "{{ansible_local.test}}"
ansible-playbook 실행
ansible-playbook -i ./inventory --limit kube-node1 check_date.yml
$ ansible-playbook -i ./inventory --limit kube-node1 check_date.yml
PLAY [all] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [kube-node1]
TASK [Get custom facts] ********************************************************************************************
ok: [kube-node1] => {
"msg": "The custom fact is {'date': 'Mon Nov 7 02:44:41 PM KST 2022'}"
}
TASK [get key:value] ***********************************************************************************************
ok: [kube-node1] => {
"msg": {
"fact_key": "fact_value"
}
}
PLAY RECAP *********************************************************************************************************
kube-node1 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
매직변수(Magic Variables)
- ansible에 의해 자동으로 수집되는 변수
변수 | 설명 |
hostvars | 인벤토리의 모든 호스트와 호스트에 할당된 변수가 포함된 사전/맵 |
group_names | 현재 호스트가 속한 그룹 목록은 항상 Inventory_hostname을 반영하고 위임을 무시합니다. |
groups | 인벤토리의 모든 그룹이 포함된 사전/맵 및 각 그룹에는 해당 그룹에 속한 호스트 목록이 있습니다. |
inventory_hostname | 플레이에서 반복되는 '현재' 호스트의 인벤토리 이름입니다. 이는 위임의 영향을 받지 않으며 항상 작업의 원래 호스트를 반영합니다. |
ansible-playbook -i ./inventory --limit kube-node1 check_date.yml
매직변수 확인
gatherMagic.yaml 작성
vim gatherMagic.yaml
---
- name: gather magic
hosts: all
gather_facts: false
tasks:
- name: magic variable
debug:
var: "{{ item }}"
loop:
- ansible_config_file
- playbook_dir
- inventory_dir
- inventory_file
- ansible_check_mode
- ansible_diff_mode
- ansible_forks
- ansible_verbosity
- inventory_hostname
- ansible_play_hosts
- ansible_version
ansible-playbook 실행
ansible-playbook -i ./inventory --limit kube-node1 gatherMagic.yaml
$ ansible-playbook -i ./inventory --limit kube-node1 gatherMagic.yaml
PLAY [gather magic] ************************************************************************************************
TASK [magic variable] **********************************************************************************************
ok: [kube-node1] => (item=ansible_config_file) => {
"ansible_config_file": "/home/vagrant/ansible-spec/ansible.cfg",
"ansible_loop_var": "item",
"item": "ansible_config_file"
}
ok: [kube-node1] => (item=playbook_dir) => {
"ansible_loop_var": "item",
"item": "playbook_dir",
"playbook_dir": "/home/vagrant/ansible-spec"
}
ok: [kube-node1] => (item=inventory_dir) => {
"ansible_loop_var": "item",
"inventory_dir": "/home/vagrant/ansible-spec/inventory",
"item": "inventory_dir"
}
ok: [kube-node1] => (item=inventory_file) => {
"ansible_loop_var": "item",
"inventory_file": "/home/vagrant/ansible-spec/inventory/inventory.yaml",
"item": "inventory_file"
}
ok: [kube-node1] => (item=ansible_check_mode) => {
"ansible_check_mode": false,
"ansible_loop_var": "item",
"item": "ansible_check_mode"
}
ok: [kube-node1] => (item=ansible_diff_mode) => {
"ansible_diff_mode": false,
"ansible_loop_var": "item",
"item": "ansible_diff_mode"
}
ok: [kube-node1] => (item=ansible_forks) => {
"ansible_forks": 5,
"ansible_loop_var": "item",
"item": "ansible_forks"
}
ok: [kube-node1] => (item=ansible_verbosity) => {
"ansible_loop_var": "item",
"ansible_verbosity": 0,
"item": "ansible_verbosity"
}
ok: [kube-node1] => (item=inventory_hostname) => {
"ansible_loop_var": "item",
"inventory_hostname": "kube-node1",
"item": "inventory_hostname"
}
ok: [kube-node1] => (item=ansible_play_hosts) => {
"ansible_loop_var": "item",
"ansible_play_hosts": [
"kube-node1"
],
"item": "ansible_play_hosts"
}
ok: [kube-node1] => (item=ansible_version) => {
"ansible_loop_var": "item",
"ansible_version": {
"full": "2.12.5",
"major": 2,
"minor": 12,
"revision": 5,
"string": "2.12.5"
},
"item": "ansible_version"
}
PLAY RECAP *********************************************************************************************************
kube-node1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
MAGIC VARIABLE MAPPING
MAGIC_VARIABLE_MAPPING = dict(
connection = ('ansible_connection',),
remote_addr = ('ansible_ssh_host', 'ansible_host'),
remote_user = ('ansible_ssh_user', 'ansible_user'),
port = ('ansible_ssh_port', 'ansible_port'),
accelerate_port = ('ansible_accelerate_port',),
password = ('ansible_ssh_pass', 'ansible_password'),
private_key_file = ('ansible_ssh_private_key_file', 'ansible_private_key_file'),
pipelining = ('ansible_ssh_pipelining', 'ansible_pipelining'),
shell = ('ansible_shell_type',),
become = ('ansible_become',),
become_method = ('ansible_become_method',),
become_user = ('ansible_become_user',),
become_pass = ('ansible_become_password','ansible_become_pass'),
become_exe = ('ansible_become_exe',),
become_flags = ('ansible_become_flags',),
ssh_common_args = ('ansible_ssh_common_args',),
docker_extra_args= ('ansible_docker_extra_args',),
sftp_extra_args = ('ansible_sftp_extra_args',),
scp_extra_args = ('ansible_scp_extra_args',),
ssh_extra_args = ('ansible_ssh_extra_args',),
sudo = ('ansible_sudo',),
sudo_user = ('ansible_sudo_user',),
sudo_pass = ('ansible_sudo_password', 'ansible_sudo_pass'),
sudo_exe = ('ansible_sudo_exe',),
sudo_flags = ('ansible_sudo_flags',),
su = ('ansible_su',),
su_user = ('ansible_su_user',),
su_pass = ('ansible_su_password', 'ansible_su_pass'),
su_exe = ('ansible_su_exe',),
su_flags = ('ansible_su_flags',),
executable = ('ansible_shell_executable',),
module_compression = ('ansible_module_compression',),
)
- https://github.com/ansible/ansible/blob/c600ab81ee/lib/ansible/playbook/play_context.py#L46-L55
- https://github.com/ansible/ansible/blob/stable-2.14/lib/ansible/playbook/play_context.py
ansible localhost -m debug -a "var=hostvars['localhost']"
참고URL
- [Ansible] facts 모듈(facts module) : https://scbyun.com/1379
- Ansible Documentation : Special Variables - Magic variables
728x90
'리눅스' 카테고리의 다른 글
Ansible을 위한 기본 Bash 완성을 설치하는 방법 (0) | 2022.11.07 |
---|---|
[리눅스] watchdog: BUG: soft lockup - CPU#0 stuck for 63s! (0) | 2022.11.07 |
파이썬 가상 환경을 구성하고 설정하는 방법(python virtual environment) (0) | 2022.11.07 |
Ansible 인벤토리를 YAML 파일로 설정하는 방법 (0) | 2022.10.31 |
Ansible 구성 설정(Configuration Settings) 파일의 우선 순위에 대한 설명 (0) | 2022.10.31 |