Ansible(3)
Ansible(3)
제어 구문 - job control
반복문과 조건문
반복문
단순 반복문(loop, with_items)
중첩 반복문(with_nested)
기타 일반 반복 지시문
- with_file
- with_fileglob
- with_random_choice
- 변수는 키-값 형식으로 정의해야 한다.
- 반복문에서 사용되는 변수는 목록 형식이어야 한다.
1
2
3
4
...: ...:
with_items: with_items:
- value2 - v1: k1
- value2 - v2: k2
조건문
when 구문
- when 구문 조건 예제
- ansible_machine == “x86_64”
- max_memory == 512
- min_memory != 512
- min_memory < 128
- min_memory >= 512
- min_memory is defined
- max_memory is not defined
- memory_available
- note memory_available
- my_special_user in superusers
- when 구문 예제
- 둘중 한가지 조건이 만족하면 참
when: ansible_distribution == “RedHat” or ansible_distribution == “Fedora”
- 두개의 조건이 모두 만족하면 참
when: ansible_distribution_version == “7.5” and ansible_kernel == “3.10.0-327.el7.x86_64” when:
- ansible_distribution_version == “7.5”
- ansible_kernel == “3.10.0-327.el7.x86_64”- 여러가지 조건인 경우
when:
( ansible_distribution == “RedHat” and
ansible_distribution_major_version == “7” )
or
( ansible_distribution == “Fedora” and
ansible_distribution_major_version == “28” )
- 반복문 + 조건문
1
2
3
4
5
6
7
---
- name: install mariadb-server if enough space on root
yum:
name: mariadb-server
state: latest
with_items: ""
when: item.mount == "/" and item.size_available > 300000000
핸들러 구현
- notify ~ handler 구문에서 handlers 안의 내용이 실행되는 시점
- (중요) notify 동작하는 시점: tasks가 changed 상태이면
- (중요) handlers 동작하는 시점: tasks가 완료 상태이면
작업 오류 처리
1
2
3
4
5
6
7
tasks:
- name: TASK1
file:
src: httpd.conf.template
dest: /etc/httpd/conf/httpd.conf
notify:
- restarted
해당 내용은 Ansible 중고급 내용이다.
- 작업 실패 무시 »> ignore erros: yes
- 작업 실패 후에도 핸들러 강제 실행하기(ok, failed) »> force_handlers: yes
- 작업 실패 무시와 핸들러 강제 실행은 들여쓰기 정도가 다르다.
- 작업 실패 조건 지정하기(1) »> failed_when
- 작업 실패 조건 지정하기(2) »> fail 모듈, fail ~ when 사용
- 작업 결과를 “changed”로 보고하는 시점 지정하기 - changed_when
- 블록 및 오류 처리 - block ~ rescue ~ always
- block ~ rescue ~ always: block 안에 있는 task가 실패하면 rescue가, 성공했다면 always가 실행된다.
- block ~ when: 조건에 따라 when 실행
- block ~ rescue: block 안에 있는 task가 실패하면 rescue가 실행
태그 구현
include_tasks는 tasks를 반복할 순 없다.
| yum/dnf/pip services/systemd firewalld command shell | copy fetch file replace | user ansible.posix.authorized_keys community.mysql.mysql_user | uri debug get_url community.general.shutdown |
|---|---|---|---|
This post is licensed under CC BY 4.0 by the author.