Skip to content

Instantly share code, notes, and snippets.

@IMHOSUNG
Forked from carlessanagustin/loop_over_lists.md
Created September 16, 2022 05:43
Show Gist options
  • Save IMHOSUNG/d1ed656ace991972fa46d67f47fb075c to your computer and use it in GitHub Desktop.
Save IMHOSUNG/d1ed656ace991972fa46d67f47fb075c to your computer and use it in GitHub Desktop.
ANSIBLE: Loop over 2 lists
  • playbook test.yml
---
- hosts: localhost
  gather_facts: no
  connection: local

  vars:
    type: st1
    size: 2000
    instance_ids:
      - xxx
      - yyy
      - zzz
    objs:
      - { key1: /dev/sdb1, key2: "{{instance_ids}}" }
      - { key1: /dev/sdc1, key2: "{{instance_ids}}" }
      - { key1: /dev/sdd1, key2: "{{instance_ids}}" }

  tasks:

    - name: 2nd --------
      debug: msg="{{ item.0.key1 }} and {{ item.1 }} and {{ size }} and {{ type }}"
      with_subelements:
        - "{{ objs }}"
        - key2
  • usage: ansible-playbook test.yml -i hosts/all | grep -i "msg"

  • output:

"msg": "/dev/sdb1 and xxx and 2000 and st1"
"msg": "/dev/sdb1 and yyy and 2000 and st1"
"msg": "/dev/sdb1 and zzz and 2000 and st1"
"msg": "/dev/sdc1 and xxx and 2000 and st1"
"msg": "/dev/sdc1 and yyy and 2000 and st1"
"msg": "/dev/sdc1 and zzz and 2000 and st1"
"msg": "/dev/sdd1 and xxx and 2000 and st1"
"msg": "/dev/sdd1 and yyy and 2000 and st1"
"msg": "/dev/sdd1 and zzz and 2000 and st1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment