ansible
가능 : 루핑
수색…
with_items - 간단한 목록
with_items
의 with_items
루프를 사용하면 값을 쉽게 반복 할 수 있습니다.
- name: Add lines to this file
lineinfile: dest=/etc/file line={{ item }} state=present
with_items:
- Line 1
- Line 2
- Line 3
with_items - 미리 정의 된 목록
변수 목록을 반복 할 수도 있습니다.
vars에서 :
favorite_snacks:
- hotdog
- ice cream
- chips
그리고 나서 루프 :
- name: create directories for storing my snacks
file: path=/etc/snacks/{{ item }} state=directory
with_items: '{{ favorite_snacks }}'
An 2.0+를 사용하는 경우 변수 호출에 대해 따옴표를 사용해야합니다.
with_items - 미리 정의 된 사전
사전을 사용하여 더 복잡한 루프를 만들 수 있습니다.
vars에서 :
packages:
- present: tree
- present: nmap
- absent: apache2
다음 루프 :
- name: manage packages
package: name={{ item.value }} state={{ item.key }}
with_items: '{{ packages }}'
또는 키 값을 사용하지 않으려면 다음을 수행하십시오.
바르 :
packages:
- name: tree
state: present
- name: nmap
state: present
- name: apache2
state: absent
다음 루프 :
- name: manage packages
package: name={{ item.name }} state={{ item.state }}
with_items: '{{ packages }}'
with_items - 사전
좀 더 복잡한 루프를 위해 사전을 사용할 수 있습니다.
- name: manage packages
package: name={{ item.name }} state={{ item.state }}
with_items:
- { name: tree, state: present }
- { name: nmap, state: present }
- { name: apache2, state: absent }
중첩 루프
with_nested
를 사용하여 중첩 루프를 만들 수 있습니다.
vars에서 :
keys:
- key1
- key2
- key3
- key4
다음 루프 :
- name: Distribute SSH keys among multiple users
lineinfile: dest=/home/{{ item[0] }}/.ssh/authorized_keys line={{ item[1] }} state=present
with_nested:
- [ 'calvin', 'josh', 'alice' ]
- '{{ keys }}'
이 작업은 각 사용자를 반복하고 authorized_keys
파일을 목록에 정의 된 4 개의 키로 채 웁니다.
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow