반응형

(참조: - https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement)


ㅁWhen조건

 - 조건이 참(true) 일경우 task 실행, 거짓(false)일 경우 task 미실행


ㅁ when활용

- task요약 : 


- name: /etc/passwd | use cut command

 ▶ cut --delimiter=':' --fields=1 /etc/passwd 수행하고, register를 이용해 usernames_result 변수 선언



- name: command set_fact

▶ set_fact에  username_list == usernames_result.stdout_lines 선언

 

- name: result refered to username_list

▶ 조건문 when 실행,   'testuser1'이  username_list에 있는지 in 연산자를 사용해 확인


---

- name: command result 

  hosts: localhost

  tasks:


   - name: /etc/passwd | use cut command

     command: cut --delimiter=':' --fields=1 /etc/passwd

     register: usernames_result


   - name: username_result debug

     debug:

        var: usernames_result


   - name: command set_fact

     set_fact:

       username_list: "{{ usernames_result.stdout_lines }}"


   - name: result refered to username_list

     debug:

       msg: this server exists test1

     when: "'testuser1' in username_list"



- 결과 (when이  true일 경우)


[root@system1 yaml]# ansible-playbook get-users.yml


PLAY [command result] ************************************************************************************


TASK [Gathering Facts] ***********************************************************************************

ok: [localhost]


TASK [/etc/passwd | use cut command] *********************************************************************

changed: [localhost]


TASK [username_result debug] *****************************************************************************

-- 생략 


TASK [command set_fact] **********************************************************************************

ok: [localhost]


TASK [result refered to username_list] *******************************************************************

ok: [localhost] => {}


MSG:


this server exists test1


PLAY RECAP ***********************************************************************************************

localhost                  : ok=5    changed=1    unreachable=0    failed=0   




- 결과2 (when이 false, 즉 testuser1이 username_list 內 없을 경우)

 ▶ when이 있는 task는  skipping이 출력됨

  . skipping : when이 지정한 조건을 만족하지 않았을때 하는 동작 

 

[root@system1 yaml]# ansible-playbook get-users.yml


PLAY [command result] ************************************************************************************


TASK [Gathering Facts] ***********************************************************************************

ok: [localhost]


TASK [/etc/passwd | use cut command] *********************************************************************

changed: [localhost]


TASK [username_result debug] *****************************************************************************

-- 생략 

TASK [command set_fact] **********************************************************************************

ok: [localhost]


TASK [result refered to username_list] *******************************************************************

skipping: [localhost]


PLAY RECAP ***********************************************************************************************

localhost                  : ok=4    changed=1    unreachable=0    failed=0   



반응형

'프로그래밍 > ansbie_YAML' 카테고리의 다른 글

when-dict 예제  (0) 2018.12.04
진자2(jinja2) 변수 확장  (0) 2018.11.28
register 예제  (0) 2018.11.27

+ Recent posts