7
7

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で2重ループ

Posted at

ansible2.0から2重ループができるようになってました。
playbookを書いてるとたまに2重ループしたくなる時があって、それのメモ

#loop.yml
- hosts: localhost
  connection: local
  vars: 
     hosts:
       - ip: 192.168.33.99
       - ip: 192.168.33.100
     attributes:
       - web
       - db
  tasks:
    - name: test double loop
      include: ./inner_loop.yml
      with_items: "{{ hosts }}" 
      loop_control:
        loop_var: outer_item

#inner_loop.yml
- debug: msg="{{ outer_item.ip }}  {{ item }}"
  with_items: "{{ attributes }}" 

これで、


TASK [debug] *******************************************************************
ok: [localhost] => (item=web) => {
    "item": "web",
    "msg": "192.168.33.99  web"
}
ok: [localhost] => (item=db) => {
    "item": "db",
    "msg": "192.168.33.99  db"
}

TASK [debug] *******************************************************************
ok: [localhost] => (item=web) => {
    "item": "web",
    "msg": "192.168.33.100  web"
}
ok: [localhost] => (item=db) => {
    "item": "db",
    "msg": "192.168.33.100  db"
}

PLAY RECAP *********************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=0

こんな結果が取得できるようになる。
2重ループが必要なplaybookは極力ないような方がいいな。

リンク

playbooks_loops - loop-control

7
7
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?