ansible

ansible loops

wefree 2022. 2. 6. 21:28

playbook 에 아래와 같이 작성할 수 있다.

 

예제1

- name: loop test
  hosts: web_servers
  tasks:
    - name: echo 1 to 3
      command: echo {{ item }}
      loop:
        - 1
        - 2
        - 3

 

예제2

- name: loop test
  hosts: web_servers
  tasks:
    - name: touch sequence
      command: touch '/tmp/seq_{{ item.number }}_{{ item.char }}.txt'
      loop:
        - number: 1
          char: a
        - number: 2
          char: b
        - number: 3
          char: c

구버전 ansible 에서는 loop 대신에 with_items 를 사용하기도 한다.

 

참고: https://www.udemy.com/course/learn-ansible/