Ansible with_X から loop へ (with_items->loop でのリストの扱い注意)

# そして `with_X` から `loop` へ (ラーミアに乗りたい)

Ansibleでリストを扱うのは非常に謎の挙動が多くはまりどころではありますが「それもまた慈悲」だと思ってついつい飛びつくのがAnsibleなんてものを使う人の性なのだと思っています。

[Migrating from with_X to loop](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#migrating-from-with-x-to-loop)

`loop` 便利そうではありますが、リストの扱いが酷い、`with_items`使わせて下さいよ。そのうちリストサポートされそうなのがAnsibleですかね...

---
# varsです

```yaml
  vars:
    - foo: [f1, f2]
    - bar: [b1, b2]
```

---
# 実行結果

## リストが1つ

```yaml
    - name: with_items with a list
      debug: msg={{ item }}
      with_items: 
        - "{{ foo }}"
    - name: loop with a list
      debug: msg={{ item }}
      loop: "{{ foo }}"
```

```
TASK [with_items with a list] ****************************************************************************************
ok: [127.0.0.1] => (item=None) => {
    "msg": "f1"
}
ok: [127.0.0.1] => (item=None) => {
    "msg": "f2"
}

TASK [loop with a list] **********************************************************************************************
ok: [127.0.0.1] => (item=None) => {
    "msg": "f1"
}
ok: [127.0.0.1] => (item=None) => {
    "msg": "f2"
}
```

---
## リストが2つ

```yaml
    - name: with_items with muliti lists within a list
      debug: msg={{ item }}
      with_items: 
        - "{{ foo }}"
        - "{{ bar }}"
    - name: loop with multi lists within a list
      debug: msg={{ item }}
      loop:
        - "{{ foo }}"
        - "{{ bar }}"
```

```
TASK [with_items with muliti lists within a list] ********************************************************************
ok: [127.0.0.1] => (item=None) => {
    "msg": "f1"
}
ok: [127.0.0.1] => (item=None) => {
    "msg": "f2"
}
ok: [127.0.0.1] => (item=None) => {
    "msg": "b1"
}
ok: [127.0.0.1] => (item=None) => {
    "msg": "b2"
}

TASK [loop with multi lists within a list] ***************************************************************************
ok: [127.0.0.1] => (item=None) => {
    "msg": [
        "f1",
        "f2"
    ]
}
ok: [127.0.0.1] => (item=None) => {
    "msg": [
        "b1",
        "b2"
    ]
}
```

おおう...

`loop` って

---
## ということで

```yaml
    - name: loop with a list within lists of list  ( {{ list | union(list) }} )
      debug: msg={{ item }}
      loop: "{{ foo | union(bar) }}"

    - name: loop with a list within lists of list  ( {{ list + list }} )
      debug: msg={{ item }}
      loop: "{{ foo + bar }}"
```

```
TASK [loop with a list within lists of list  ( {{ list | union(list) }} )] *******************************************
ok: [127.0.0.1] => (item=None) => {
    "msg": "f1"
}
ok: [127.0.0.1] => (item=None) => {
    "msg": "f2"
}
ok: [127.0.0.1] => (item=None) => {
    "msg": "b1"
}
ok: [127.0.0.1] => (item=None) => {
    "msg": "b2"
}

TASK [loop with a list within lists of list  ( {{ list + list }} )] **************************************************
ok: [127.0.0.1] => (item=None) => {
    "msg": "f1"
}
ok: [127.0.0.1] => (item=None) => {
    "msg": "f2"
}
ok: [127.0.0.1] => (item=None) => {
    "msg": "b1"
}
ok: [127.0.0.1] => (item=None) => {
    "msg": "b2"
}
```

---
# 参考

- [Ansible 2\.5 Porting Guide — Ansible Documentation](https://docs.ansible.com/ansible/2.5/porting_guides/porting_guide_2.5.html)
- [Migrating from with_X to loop](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#migrating-from-with-x-to-loop)
- [Filters — Ansible Documentation](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html)
  - [#with-items](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-items)
- [Template Designer Documentation — Jinja2 Documentation \(2\.10\)]
(http://jinja.pocoo.org/docs/latest/)
  - [#filters](http://jinja.pocoo.org/docs/latest/templates/#filters)

---
# Playbook (Gist)

[Playing with Playbook : Migrating from with\_items to loop](https://gist.github.com/F88/d4968169dffc191cfc211c5801235a98)


```yaml
---

# Loops — Ansible Documentation
#   https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html
# Filters — Ansible Documentation
#   https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html
# Jinja2
#   http://jinja.pocoo.org/docs/latest/

- name: Testing `loop`
  hosts: 127.0.0.1
  connection: local
  vars:
    - foo: [f1, f2]
    - bar: [b1, b2]
  tasks:

    - name: with_items with a list
      debug: msg={{ item }}
      with_items: 
        - "{{ foo }}"
    - name: loop with a list
      debug: msg={{ item }}
      loop: "{{ foo }}"

    - name: with_items with muliti lists within a list
      debug: msg={{ item }}
      with_items: 
        - "{{ foo }}"
        - "{{ bar }}"
    - name: loop with multi lists within a list
      debug: msg={{ item }}
      loop:
        - "{{ foo }}"
        - "{{ bar }}"

    - name: loop with a list within lists of list  ( {{ list | union(list) }} )
      debug: msg={{ item }}
      loop: "{{ foo | union(bar) }}"

    - name: loop with a list within lists of list  ( {{ list + list }} )
      debug: msg={{ item }}
      loop: "{{ foo + bar }}"
```