Skip to content

Instantly share code, notes, and snippets.

@carlessanagustin
Last active November 18, 2022 17:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save carlessanagustin/c563347ad828746dcca5a2a52472a54f to your computer and use it in GitHub Desktop.
Save carlessanagustin/c563347ad828746dcca5a2a52472a54f 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"
@branov
Copy link

branov commented Jan 17, 2021

Thanks for sharing, this helped me a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment