728x90
Facts
ansible에서 제공하는 호스트에 대한 정보.
$ ansible-playbook test.yaml
PLAY [show return value of command module] **********************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************************
ok: [vagrant1]
ansible-playbook 을 실행하여 입력한 task를 수행하기 전, ansible에선 Gathering Facts 단계를 거치게 되는데, 해당 단계에서 호스트에 대한 모든 종류의 세부 정보를 쿼리한다. 이때 쿼리된 정보들이 fact라고 하는 변수로 저장되며 다른 변수처럼 작동한다.
print out operating system playbook
$ cat print_out_operaintg_system.yaml
- name: print out operating system
hosts: all
gather_facts: True
tasks:
- debug: var=ansible_distribution
$ ansible-playbook print_out_operaintg_system.yaml
PLAY [print out operating system] *******************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************************
ok: [vagrant3]
ok: [vagrant1]
ok: [vagrant2]
TASK [debug] ****************************************************************************************************************************************************************************************************************************************
ok: [vagrant1] => {
"ansible_distribution": "Ubuntu"
}
ok: [vagrant2] => {
"ansible_distribution": "Ubuntu"
}
ok: [vagrant3] => {
"ansible_distribution": "Ubuntu"
}
PLAY RECAP ******************************************************************************************************************************************************************************************************************************************
vagrant1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
vagrant2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
vagrant3 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
위와 같이 운영체제를 정상적으로 출력하는 것을 확인할 수 있다.
Setup module
ansible은 setup module을 통해 fact를 수집하므로 수동으로 직접 정보를 수집할 수도 있으며, 아래 명령어를 통해 모든 팩트를 출력할 수 있다.
$ ansible vagrant1 -m setup
vagrant1 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.16.132.138"
],
"ansible_all_ipv6_addresses": [
"fe80::20c:29ff:fe2a:412f"
],
...
"ansible_virtualization_tech_guest": [],
"ansible_virtualization_tech_host": [],
"ansible_virtualization_type": "NA",
"gather_subset": [
"all"
],
"module_setup": true
},
"changed": false
}
구조는 대략 ansible_facts 하위에 딕셔너리를 포함하고 있는 형태이다.
fact 하위 집합 조회
setup 모듈을 사용하여 팩트 조회 시에는 filtering을 통해 특정 이름 별로 filtering할 수 있다.
$ ansible vagrant1 -m setup -a 'filter=ansible_architecture'
vagrant1 | SUCCESS => {
"ansible_facts": {
"ansible_architecture": "aarch64"
},
"changed": false
}
또한 아래와 같이 ec2 등에서 지원하는 facts들도 사용할 수 있다.
$ cat ec2_facts.yaml
- name: ec2 facts
hosts: ec2
tasks:
# Gather EC2 metadata facts
- amazon.aws.ec2_metadata_facts:
- debug:
msg: "This instance is a t3.small"
when: ansible_ec2_instance_type == "t3.small"
$ ansible-playbook ec2_facts.yaml --private-key=~/.ssh/id_rsa
PLAY [ec2 facts] ************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host ec2 is using the discovered Python interpreter at /usr/bin/python3.7, but future installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-
core/2.12/reference_appendices/interpreter_discovery.html for more information.
ok: [ec2]
TASK [amazon.aws.ec2_metadata_facts] ****************************************************************************************************************************************************************************************************************
ok: [ec2]
TASK [debug] ****************************************************************************************************************************************************************************************************************************************
ok: [ec2] => {
"msg": "This instance is a t3.small"
}
PLAY RECAP ******************************************************************************************************************************************************************************************************************************************
ec2 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
set_fact 를 활용한 새로운 변수 정의
ansible은 set_fact module을 사용해 태스크에서 팩트를 설정할 수 있다.
- name: get snapshot id
shell: >
aws ec2 describe-snapshots --filters
Name=tag:Name,Values=my-snapshot
| jq --raw-output ".Snapshots[].SnapshotId"
register: snap_result
- set_fact: snap={{ snap_result.stdout }}
- name: delete old snapshot
command: aws ec2 delete-snapshot --snapshot-id "{{ snap }}"
'DevOps > Ansible' 카테고리의 다른 글
Ansible run_once (0) | 2023.07.03 |
---|---|
Ansible serial 활용 (0) | 2023.07.03 |
Ansible host 지정 패턴 (0) | 2023.07.03 |
Ansible lookup (0) | 2023.07.02 |
requirements.txt 을 활용해 Ansible에서 package 설치 (0) | 2023.06.30 |