컬쥐네 다락방
Ansible | 반복문 본문
반복문
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#playbooks-loops
작업(Task)에서 loop
, with_
, until
반복문 구성
여기서 사용되는 item은 흔히 사용되는 for문의 i이다.(ex for i in X 또는 for int i ; ;)
리스트(목록) 반복문
- 리스트를 사용해 다양한 방법으로 활용할 수 있다.
loop
대신,with_items
,with_list
- hosts: 192.168.100.11
gather_facts: no
tasks:
- debug:
msg: "{{ item }}"
with_items:
- apple
- banana
- carrot
- hosts: 192.168.100.11
gather_facts: no
vars:
fruits:
- apple
- banana
- carrot
tasks:
- debug:
msg: "{{ item }}"
with_items:
"{{ fruits }}"
- hosts: 192.168.100.11
gather_facts: no
vars:
fruits:
- apple
- banana
- carrot
tasks:
- debug:
msg: "{{ item }}"
loop:
"{{ fruits }}"
사전 반복문
- 리스트가 아닌 딕셔너리 형식의 변수도 활용 가능하다.
loop
대신with_dict
- name: Add several users
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
loop:
- name: 'testuser1'
groups: 'wheel'
- name: 'testuser2'
groups: 'root'
#[ {name: 'testuser1', groups: 'wheel'}, {name: 'testuser2', group: 'root'} ]
- hosts: 192.168.100.11
gather_facts: no
vars:
fruits:
- name: apple
count: 2
- name: banana
count: 3
tasks:
- debug:
msg: "{{ item.name }} / {{ item.count }}"
loop:
'{{ fruits }}'
'클라우드 > Ansible' 카테고리의 다른 글
Ansible | 블록과 태그 (0) | 2022.04.21 |
---|---|
Ansible | 조건문과 핸들러 (0) | 2022.04.18 |
Ansible | Ansible 명령에서의 변수 활용 (0) | 2022.04.18 |
Ansible | Playbook (0) | 2022.04.18 |
Ansible | Ad_hoc (0) | 2022.04.18 |
Comments