loop_var - ループ変数を変更する

2019/06/02

Ansible

 Ansible Documentation

Defining inner and outer variable names with

 機能

  • ループ変数をデフォルトの item からから別の変数に変更する
  • 多重ループのときはループごとにループ変数を設定しないと次の警告メッセージが表示される
  • [WARNING]: The loop variable 'item' is already in use. You should set the `loop_var` value in the `loop_control` option for the task to something else to avoid variable collisions and unexpected behavior.

 例

■ デフォルトの例  play の内容です。ループ変数は item です。
---
- hosts: all
  gather_facts: no

  tasks:
    - name: タスクのループ
      debug:
        var: item
      loop:
        - 1
        - 3
        - 5
        - 7
        - 9
      when: 2 < item < 6 
実行結果です。
ansibleman@ubuntu-pc:~/ansible/loop$ ansible-playbook -i hosts.yml site.yml

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

TASK [タスクのループ] *****************************************************************
skipping: [node-c0706] => (item=1) 
ok: [node-c0706] => (item=3) => {
    "ansible_loop_var": "item",
    "item": 3
}
ok: [node-c0706] => (item=5) => {
    "ansible_loop_var": "item",
    "item": 5
}
skipping: [node-c0706] => (item=7) 
skipping: [node-c0706] => (item=9) 

PLAY RECAP *********************************************************************
node-c0706                 : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

ansibleman@ubuntu-pc:~/ansible/loop$ 
■ ループ変数を変更した例 ループ変数を cntr に変更した例です。
---
- hosts: all
  gather_facts: no

  tasks:
    - name: タスクのループ
      debug:
        var: cntr
      loop:
        - 1
        - 3
        - 5
        - 7
        - 9
      loop_control:
        loop_var: cntr
      when: 2 < cntr < 6 
実行結果です。
ansibleman@ubuntu-pc:~/ansible/loop$ ansible-playbook -i hosts.yml site.yml

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

TASK [タスクのループ] *****************************************************************
skipping: [node-c0706] => (item=1) 
ok: [node-c0706] => (item=3) => {
    "ansible_loop_var": "cntr",
    "cntr": 3
}
ok: [node-c0706] => (item=5) => {
    "ansible_loop_var": "cntr",
    "cntr": 5
}
skipping: [node-c0706] => (item=7) 
skipping: [node-c0706] => (item=9) 

PLAY RECAP *********************************************************************
node-c0706                 : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

ansibleman@ubuntu-pc:~/ansible/loop$ 
■ 多重ループの例 多重ループで子タスク側のループ変数を変更した例です。
親ファイルの内容です。
---
- hosts: all
  gather_facts: no

  tasks:
    - name: 親タスクのループ
      include_tasks: child-task.yml
      loop:
        - 1
        - 3
        - 5
        - 7
        - 9
      when: 2 < item < 6 
子ファイルの内容です。ループ変数を cntr に変更しています。
- name: 子タスクのループ
  debug:
    msg: "親 {{ item }} / 子 {{ cntr }}"
  loop:
    - 4
    - 6
    - 8
  loop_control:
    loop_var: cntr 
実行結果です。子タスク側で親タスクのループ変数を参照しています。
ansibleman@ubuntu-pc:~/ansible/loop$ ansible-playbook -i hosts.yml site.yml

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

TASK [親タスクのループ] ****************************************************************
skipping: [node-c0706] => (item=1) 
skipping: [node-c0706] => (item=7) 
skipping: [node-c0706] => (item=9) 
included: /home/ansibleman/ansible/loop/child-task.yml for node-c0706
included: /home/ansibleman/ansible/loop/child-task.yml for node-c0706

TASK [子タスクのループ] ****************************************************************
ok: [node-c0706] => (item=4) => {
    "msg": "親 3 / 子 4"
}
ok: [node-c0706] => (item=6) => {
    "msg": "親 3 / 子 6"
}
ok: [node-c0706] => (item=8) => {
    "msg": "親 3 / 子 8"
}

TASK [子タスクのループ] ****************************************************************
ok: [node-c0706] => (item=4) => {
    "msg": "親 5 / 子 4"
}
ok: [node-c0706] => (item=6) => {
    "msg": "親 5 / 子 6"
}
ok: [node-c0706] => (item=8) => {
    "msg": "親 5 / 子 8"
}

PLAY RECAP *********************************************************************
node-c0706                 : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

ansibleman@ubuntu-pc:~/ansible/loop$

カテゴリー

目次

QooQ