そして with_X
から loop
へ (ラーミアに乗りたい)
Ansibleでリストを扱うのは非常に謎の挙動が多くはまりどころではありますが「それもまた慈悲」だと思ってついつい飛びつくのがAnsibleなんてものを使う人の性なのだと思っています。
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"
}
参考
- Ansible 2.5 Porting Guide — Ansible Documentation
- Migrating from with_X to loop
- Filters — Ansible Documentation
- [Template Designer Documentation — Jinja2 Documentation (2.10)]
(http://jinja.pocoo.org/docs/latest/)
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 }}"