컬쥐네 다락방
Ansible | 조건문과 핸들러 본문
조건문
https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#basic-conditionals-with-when
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#playbooks-tests
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#playbooks-filters
작업에서 when
키워드 사용, 조건을 정의 test
, filter
사용
If 문을 사용한다고 보면 된다.
조건 정의시
{{ }}
중괄호 사용 X
- hosts: 192.168.100.11
vars:
switch: "on"
tasks:
- debug:
msg: "hello switch on"
when: switch == "on"
- debug:
msg: "hello switch off"
when: switch == "off"
조건문에 많이 사용하는 팩트 변수
https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#commonly-used-facts
- ansible_facts["distribution"]
- ansible_distribution
- hosts: wp
tasks:
- debug:
msg: "hello CentOS"
when: ansible_facts["distribution"] == "CentOS"
- debug:
msg: "hello Ubuntu"
when: ansible_facts["distribution"] == "Ubuntu"
핸들러
Idempotent: 멱등
가능하면 멱등성을 만족
모든 모듈, 모듈의 파라미터가 멱등성을 만족 하지는 않음
문제가 있는 코드
- hosts: 192.168.100.11
become: yes
vars:
web_svc_port: "80"
tasks:
- yum:
name: httpd
state: installed
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen'
line: 'Listen {{ web_svc_port }}'
- service:
name: httpd
state: restarted
enabled: yes
문제를 해결하기 위한 코드
- hosts: 192.168.100.11
become: yes
vars:
web_svc_port: "80"
tasks:
- yum:
name: httpd
state: installed
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen'
line: 'Listen {{ web_svc_port }}'
register: result
- service:
name: httpd
state: started
enabled: yes
- service:
name: httpd
state: restarted
enabled: yes
when: result is changed
플레이, 작업의 이름
- name: Name Test Playbook
hosts: 192.168.100.11
tasks:
- name: task1
debug:
msg: hello world
- name: task2
debug:
msg: hello world
- name: task3
debug:
msg: hello world
- debug:
msg: hello world
name: task4
핸들러
이유? 특정 작업이 변경사항을 발생하는 경우에만 실행하기 위한 작업을 지정
핸들러의 작업은 반드시 이름이 있어야 함
핸들러가 실행되는 순서?
- 알림을 받은 핸들러 작업만 순서대로 실행
- 모든 작업(
tasks
)이 완료되어야 핸들러가 실행 - 알림을 받은 회수와 상관 없이 한번만 실행
예제)
- name: handler example
hosts: 192.168.100.11
tasks:
- name: task1
file:
path: /tmp/test1
state: touch
notify:
- handle2
- handle1
#- name: error
# command: ls -P
- name: task2
file:
path: /tmp/test2
state: touch
notify:
- handle1
handlers:
- name: handle1
debug:
msg: "handle1"
- name: handle2
debug:
msg: "handle2"
- name: Handler Example
hosts: 192.168.100.11
become: yes
vars:
web_svc_port: "80"
tasks:
- name: Install httpd Package
yum:
name: httpd
state: installed
- name: Reconfigure httpd service port
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen'
line: 'Listen {{ web_svc_port }}'
notify:
- Restart httpd Service
- name: Start httpd Service
service:
name: httpd
state: started
enabled: yes
handlers:
- name: Restart httpd Service
service:
name: httpd
state: restarted
enabled: yes
핸들러가 실행되지 않고 후속 작업에서 실패 한경우
결론: 핸들러가 실행되지 않음
- name: Flush handlers
meta: flush_handlers
ansible-playbook test.yaml --force-handlers
강제로 핸들러를 실행하게 하는 설정
'클라우드 > Ansible' 카테고리의 다른 글
Ansible | 템플릿 주석 (0) | 2022.04.21 |
---|---|
Ansible | 블록과 태그 (0) | 2022.04.21 |
Ansible | 반복문 (0) | 2022.04.18 |
Ansible | Ansible 명령에서의 변수 활용 (0) | 2022.04.18 |
Ansible | Playbook (0) | 2022.04.18 |
Comments