조건부 작업에서 수집된 fact 사용
수집된 fact 의 가장 강력한 사용 사례 중 하나는 플레이북에서 조건부 로직을 구현하는 것입니다. 이 단계에서는 실행할 작업을 결정하기 위해 fact 를 사용하는 플레이북을 만들 것입니다.
Ansible 에서 조건부 작업 이해
Ansible 을 사용하면 when 키워드를 사용하여 변수, fact 또는 작업 결과에 따라 작업을 조건부로 실행할 수 있습니다. 이를 통해 더 동적이고 적응 가능한 플레이북을 만들 수 있습니다.
조건부 작업이 있는 플레이북 생성
운영 체제에 따라 다른 작업을 수행하는 플레이북을 만들어 보겠습니다.
- WebIDE 에서
/home/labex/project/ansible 디렉토리로 이동합니다.
conditional_facts_playbook.yml이라는 새 파일을 생성합니다.
- 다음 내용을 추가합니다.
---
- name: Conditional Tasks Based on Facts
hosts: local
gather_facts: true
tasks:
- name: Display OS information
debug:
msg: "Running on {{ ansible_distribution }} {{ ansible_distribution_version }}"
- name: Task for Ubuntu systems
debug:
msg: "This is an Ubuntu system. Would run apt commands here."
when: ansible_distribution == "Ubuntu"
- name: Task for CentOS systems
debug:
msg: "This is a CentOS system. Would run yum commands here."
when: ansible_distribution == "CentOS"
- name: Task for systems with at least 2GB RAM
debug:
msg: "This system has {{ ansible_memtotal_mb }} MB RAM, which is sufficient for our application."
when: ansible_memtotal_mb >= 2048
- name: Task for systems with less than 2GB RAM
debug:
msg: "This system has only {{ ansible_memtotal_mb }} MB RAM, which may not be sufficient."
when: ansible_memtotal_mb < 2048
이 플레이북은 다음과 같습니다.
- 시스템에 대한 모든 fact 를 수집합니다.
- 운영 체제 정보를 표시합니다.
- 운영 체제 유형에 따라 작업을 조건부로 실행합니다.
- RAM 용량에 따라 작업을 조건부로 실행합니다.
조건부 플레이북 실행
플레이북을 실행하여 조건부 작업이 작동하는 것을 확인해 보겠습니다.
cd ~/project/ansible
ansible-playbook -i hosts conditional_facts_playbook.yml
Ubuntu 에서 실행 중이므로 다음과 유사한 출력을 볼 수 있습니다.
PLAY [Conditional Tasks Based on Facts] **************************************
TASK [Gathering Facts] *******************************************************
ok: [localhost]
TASK [Display OS information] ************************************************
ok: [localhost] => {
"msg": "Running on Ubuntu 22.04"
}
TASK [Task for Ubuntu systems] ***********************************************
ok: [localhost] => {
"msg": "This is an Ubuntu system. Would run apt commands here."
}
TASK [Task for CentOS systems] ***********************************************
skipping: [localhost]
TASK [Task for systems with at least 2GB RAM] ********************************
ok: [localhost] => {
"msg": "This system has 3907 MB RAM, which is sufficient for our application."
}
TASK [Task for systems with less than 2GB RAM] *******************************
skipping: [localhost]
PLAY RECAP *******************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
일부 작업은 실행되고 다른 작업은 조건에 따라 건너뛰는 것을 알 수 있습니다. CentOS 작업은 Ubuntu 에서 실행 중이므로 건너뛰고, "2GB RAM 미만" 작업은 시스템에 2GB RAM 이상이 있으므로 건너뜁니다.
더 실용적인 예제 만들기
이제 실제 환경에서 사용할 수 있는 더 실용적인 예제를 만들어 보겠습니다.
- WebIDE 에서
practical_conditional_playbook.yml이라는 새 파일을 생성합니다.
- 다음 내용을 추가합니다.
---
- name: Practical Conditional Playbook
hosts: local
gather_facts: true
vars:
app_dir: "/home/labex/project/app"
tasks:
- name: Create application directory
file:
path: "{{ app_dir }}"
state: directory
mode: "0755"
- name: Configure for production environment
copy:
dest: "{{ app_dir }}/config.yml"
content: |
environment: production
memory_limit: high
debug: false
when: ansible_memtotal_mb >= 4096
- name: Configure for development environment
copy:
dest: "{{ app_dir }}/config.yml"
content: |
environment: development
memory_limit: low
debug: true
when: ansible_memtotal_mb < 4096
- name: Display configuration
command: cat {{ app_dir }}/config.yml
register: config_content
- name: Show configuration
debug:
msg: "{{ config_content.stdout_lines }}"
이 플레이북은 다음과 같습니다.
- 애플리케이션용 디렉토리를 만듭니다.
- 사용 가능한 시스템 메모리에 따라 다른 구성 파일을 작성합니다.
- 결과 구성을 표시합니다.
실용적인 플레이북을 실행합니다.
ansible-playbook -i hosts practical_conditional_playbook.yml
이 예제는 수집된 fact 를 사용하여 시스템 특성에 따라 구성을 자동으로 조정하는 방법을 보여줍니다.