Ansible 반복문 with_X

> [Ansible Loops](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html) 의 with_X 시리즈를 실행하고, 그 결과를 기록하였습니다. :writing_hand_tone2: 
> 다음 포스팅에서 `with_X` => `loop` 마이그레이션을 다뤄보겠습니다.

![ansible_withX.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/144058/3e58b771-69c2-6a49-e3af-2c720014b30b.png)


## 테스트 환경
- [Ansible 2.9.2](https://qiita.com/leechungkyu/items/1b49a970e1facdc38d0a)



## 실행하고 결과확인

### with_list

```yaml:with_list.yml
- hosts: all
  tasks:
  - name: with_list
    debug:
      msg: "{{ item }}"
    with_list:
      - hello
      - ansible
      - world
```
실행결과

```
$ ansible-playbook ./with_list.yml

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [182.252.133.71]

TASK [with_list] *********************************************************************************************************************************
ok: [182.252.133.71] => (item=hello) => {
    "msg": "hello"
}
ok: [182.252.133.71] => (item=ansible) => {
    "msg": "ansible"
}
ok: [182.252.133.71] => (item=world) => {
    "msg": "world"
}

PLAY RECAP ***************************************************************************************************************************************
182.252.133.71             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```

### with_items

```yaml:with_items.yml
- hosts: all
  tasks:
  - name: with_items
    debug:
      msg: "{{ item }}"
    with_items: "{{ somelist }}"
    vars:
      somelist:
        - hello
        - ansible
        - world
```
실행결과

```
$ ansible-playbook ./with_items.yml

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [182.252.133.71]

TASK [with_items] ********************************************************************************************************************************
ok: [182.252.133.71] => (item=hello) => {
    "msg": "hello"
}
ok: [182.252.133.71] => (item=ansible) => {
    "msg": "ansible"
}
ok: [182.252.133.71] => (item=world) => {
    "msg": "world"
}

PLAY RECAP ***************************************************************************************************************************************
182.252.133.71             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```

### with_indexed_items

```yaml:with_indexed_items.yml
- hosts: all
  tasks:
  - name: with_indexed_items
    debug:
      msg: "{{ item }}"
    with_indexed_items:
      - hello
      - ansible
      - world
```
실행결과

```
$ ansible-playbook ./with_indexed_items.yml

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [182.252.133.71]

TASK [with_indexed_items] *********************************************************************************************************************************
ok: [182.252.133.71] => (item=[0, u'hello']) => {
    "msg": [
        0,
        "hello"
    ]
}
ok: [182.252.133.71] => (item=[1, u'ansible']) => {
    "msg": [
        1,
        "ansible"
    ]
}
ok: [182.252.133.71] => (item=[2, u'world']) => {
    "msg": [
        2,
        "world"
    ]
}

PLAY RECAP ***************************************************************************************************************************************
182.252.133.71             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```

### with_flattened

```yaml:with_flattened.yml
- hosts: all
  tasks:
  - name: with_flattened
    debug:
      msg: "{{ item }}"
    with_flattened: ["hello", ["ansible",["world"]]]
```
실행결과

```
$ ansible-playbook ./with_flattened.yml

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [182.252.133.71]

TASK [with_flattened] ****************************************************************************************************************************
ok: [182.252.133.71] => (item=hello) => {
    "msg": "hello"
}
ok: [182.252.133.71] => (item=ansible) => {
    "msg": "ansible"
}
ok: [182.252.133.71] => (item=world) => {
    "msg": "world"
}

PLAY RECAP ***************************************************************************************************************************************
182.252.133.71             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```

### with_together

```yaml:with_together.yml
- hosts: all
  tasks:
  - name: with_together
    debug:
      msg: "{{ item.0 }} {{ item.1 }} {{ item.2 }}"
    with_together:
      - ["hello1", "hello2", "hello3"]
      - ["ansible1", "ansible2", "ansible3"]
      - ["world1", "world2", "world3"]
```
실행결과

```
$ ansible-playbook ./with_together.yml

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [182.252.133.71]

TASK [with_together] *****************************************************************************************************************************
ok: [182.252.133.71] => (item=[u'hello1', u'ansible1', u'world1']) => {
    "msg": "hello1 ansible1 world1"
}
ok: [182.252.133.71] => (item=[u'hello2', u'ansible2', u'world2']) => {
    "msg": "hello2 ansible2 world2"
}
ok: [182.252.133.71] => (item=[u'hello3', u'ansible3', u'world3']) => {
    "msg": "hello3 ansible3 world3"
}

PLAY RECAP ***************************************************************************************************************************************
182.252.133.71             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```

### with_dict

```yaml:with_dict.yml
- hosts: all
  tasks:
  - name: with_dict
    debug:
      msg: "{{ item.key }} {{ item.value }}"
    with_dict: { a: "hello", b: "ansible", c: "world"}
```
실행결과

```
$ ansible-playbook ./with_dict.yml

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [182.252.133.71]

TASK [with_dict] *********************************************************************************************************************************
ok: [182.252.133.71] => (item={'value': u'hello', 'key': u'a'}) => {
    "msg": "a hello"
}
ok: [182.252.133.71] => (item={'value': u'world', 'key': u'c'}) => {
    "msg": "c world"
}
ok: [182.252.133.71] => (item={'value': u'ansible', 'key': u'b'}) => {
    "msg": "b ansible"
}

PLAY RECAP ***************************************************************************************************************************************
182.252.133.71             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```

### with_sequence

```yaml:with_sequence.yml
- hosts: all
  tasks:
  - name: with_sequence
    debug:
      msg: "{{ item }}"
    with_sequence: start=3 end=9 stride=3 format=%02x
```
실행결과

```
$ ansible-playbook ./with_sequence.yml

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [182.252.133.71]

TASK [with_sequence] *****************************************************************************************************************************
ok: [182.252.133.71] => (item=03) => {
    "msg": "03"
}
ok: [182.252.133.71] => (item=06) => {
    "msg": "06"
}
ok: [182.252.133.71] => (item=09) => {
    "msg": "09"
}

PLAY RECAP ***************************************************************************************************************************************
182.252.133.71             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```
### with_subelements

```yaml:with_subelements.yml
- hosts: all
  tasks:
  - name: with_subelements
    debug:
      msg: "name : {{ item.0.name }} , like : {{ item.1 }}"
    with_subelements:
      -
        - name: "Alice"
          likes:
            - Apple
            - Banana
            - Mango

        - name: "Bob"
          likes:
            - Soccer
            - basket

      - likes
```
실행결과

```
$ ansible-playbook ./with_subelements.yml

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [182.252.133.71]

TASK [with_subelements] **************************************************************************************************************************
ok: [182.252.133.71] => (item=[{u'name': u'Alice'}, u'Apple']) => {
    "msg": "name : Alice , like : Apple"
}
ok: [182.252.133.71] => (item=[{u'name': u'Alice'}, u'Banana']) => {
    "msg": "name : Alice , like : Banana"
}
ok: [182.252.133.71] => (item=[{u'name': u'Alice'}, u'Mango']) => {
    "msg": "name : Alice , like : Mango"
}
ok: [182.252.133.71] => (item=[{u'name': u'Bob'}, u'Soccer']) => {
    "msg": "name : Bob , like : Soccer"
}
ok: [182.252.133.71] => (item=[{u'name': u'Bob'}, u'basket']) => {
    "msg": "name : Bob , like : basket"
}

PLAY RECAP ***************************************************************************************************************************************
182.252.133.71             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```

### with_nested

```yaml:with_nested.yml
- hosts: all
  tasks:
  - name: with_nested
    debug:
      msg: "{{ item.0 }} {{ item.1 }} {{ item.2 }}"
    with_nested:
      - ["hello", "one"]
      - ["ansible", "two"]
      - ["world", "three"]
```
실행결과

```
$ ansible-playbook ./with_nested.yml

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [182.252.133.71]

TASK [with_nested] *******************************************************************************************************************************
ok: [182.252.133.71] => (item=[u'hello', u'ansible', u'world']) => {
    "msg": "hello ansible world"
}
ok: [182.252.133.71] => (item=[u'hello', u'ansible', u'three']) => {
    "msg": "hello ansible three"
}
ok: [182.252.133.71] => (item=[u'hello', u'two', u'world']) => {
    "msg": "hello two world"
}
ok: [182.252.133.71] => (item=[u'hello', u'two', u'three']) => {
    "msg": "hello two three"
}
ok: [182.252.133.71] => (item=[u'one', u'ansible', u'world']) => {
    "msg": "one ansible world"
}
ok: [182.252.133.71] => (item=[u'one', u'ansible', u'three']) => {
    "msg": "one ansible three"
}
ok: [182.252.133.71] => (item=[u'one', u'two', u'world']) => {
    "msg": "one two world"
}
ok: [182.252.133.71] => (item=[u'one', u'two', u'three']) => {
    "msg": "one two three"
}

PLAY RECAP ***************************************************************************************************************************************
182.252.133.71             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```

### with_random_choice

```yaml:with_random_choice.yml
- hosts: all
  tasks:
  - name: with_random_choice
    debug:
      msg: "{{ item }}"
    with_random_choice: ["hello", "ansible", "world"]
```
실행결과

```
$ ansible-playbook ./with_random_choice.yml

PLAY [all] ***************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
ok: [182.252.133.71]

TASK [with_random_choice] ************************************************************************************************************************
ok: [182.252.133.71] => (item=ansible) => {
    "msg": "ansible"
}

PLAY RECAP ***************************************************************************************************************************************
182.252.133.71             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```

## 참고링크
- https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html
- https://qiita.com/YumaInaura/items/faf9ee6ebb409344c4c8
- https://moonstrike.github.io/ansible/2016/10/17/Ansible-Loops.html