-
ansible conditionalsansible 2022. 2. 6. 19:49
When 사용하기
예제1: OS 에 따라 package manager(apt / yum) 을 각각 다르게 사용하기
- name: Install NGINX hosts: web_servers tasks: - name: Install NGINX on Debian apt: name: nginx state: present when: ansible_os_family == "Debian" and ansible_distribution_version == "16.04" - name: Install NGINX on Redhat yum: name: nginx state: present when: ansible_os_family == "RedHat" or ansible_os_family == "SUSE"
ansible_os_family, ansible_distribution_version 은 ansible 내부에 이미 정의된 variable
예제2
- name: 'Am I an Adult or a Child?' hosts: web_servers vars: age: 25 tasks: - command: 'echo "I am a Child"' when: "age < 18" - command: 'echo "I am an Adult"' when: "age >= 18"
Conditionals in Loops
- name: Install Softwares hosts: web_servers vars: packages: - name: nginx required: True - name: mysql required: True - name: apache required: False tasks: - name: Install "{{ item.name }} on Debian apt: name: "{{ item.name }}" state: present when: item.required == True loop: "{{ packages }}"
Conditionals and Register
- name: Check status of a service and email if its down hosts: web_servers tasks: - command: service httpd status register: result - mail: to: admin@company.com subject: Service Alert body: Httpd service is down when: result.stdout.find('down') != -1
'ansible' 카테고리의 다른 글
ansible roles (0) 2022.02.06 ansible loops (0) 2022.02.06 ansible variables (0) 2022.02.06 ansible modules (0) 2022.02.06 ansible playbooks (0) 2022.02.06