3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-07-19
1 / 8

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

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

Migrating from with_X to loop

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


varsです

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

実行結果

リストが1つ

    - 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つ

    - 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 って


ということで

    - 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"
}

参考


Playbook (Gist)

Playing with Playbook : Migrating from with_items to loop

---

# 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 }}"
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?