Ansible
220613_4_앤서블_심화_플레이북 동적 구성-if
kimhope
2022. 6. 13. 23:53
728x90
if 조건문
if 조건문
- 기존의 when 조건문에 비해 코드가 간결하고, 확장성이 뛰어남
- 하나의 태스크로 수행이 가능하여 보다 효율적임
- skipping 태스크를 없앨 수 있음 (효율적인 태스크 실행)
if 조건문을 이용한 태스크 실행
1) CentOS.yml
- name: install epel-release
action: "{{ ansible_pkg_mgr }} name=epel-release state=latest"
- name: install nginx web server
action: "{{ ansible_pkg_mgr }} name=nginx state=present"
- name: upload default index.html for web server
get_url: url=https://www.nginx.com dest=/usr/share/nginx/html/ mode=0644
- name: start nginx web server
service: name=nginx state=started
2) Ubuntu.yml
- name: install nginx web server
action: "{{ ansible_pkg_mgr }} name=nginx state=present update_cache=yes"
- name: upload default index.html for web server
get_url: url=https://www.apache.com dest=/usr/share/nginx/html/
mode=0644 validate_certs=no
3) nginx_install_w_if.yml
---
- name: Install nginx on the nodes
hosts: nodes
become: yes
vars:
lnx_name: "{{ 'CentOS' if ansible_distribution == 'CentOS'
else 'Ubuntu' if ansible_distribution == 'Ubuntu'
else 'Just Linux' }}"
tasks:
- name: nginx for any linux
include_tasks: "{{ lnx_name }}.yml"
4) 실행
skipping 태스크를 확인할 수 없음 > 태스크의 낭비를 줄임
[vagrant@ansible-server install]$ anp nginx_install_w_if.yml
728x90