Search code examples
arraysdockeransibleansible-inventory

Ansible command loop with synched array


I need to iterate through 2 synched array to run bash commands

list1: a b c

list2: 1 2 3

I want to obtain

a1 b2 c3

and not

a1 a2 a3 b1 b2 b2 c1 c2 c3

I'm tryng with "with_togheter" but without success

this is my task

- name: create volume
  shell: docker volume create {{ item.0 }} {{ item.1 }}
  when: volume_exists|failed
  run_once: true
  with_togheter:
    - "{{ volumename }}"
    - "{{ volumeopts }}"
  tags:
    - dockervolumenested

this is the inventory

[pgwatch-master]
host.domain

[pgwatch-master:vars]
volumename=["pgw-master-grafana","pgw-master-influxdb","pgw-master-persistent-config","pgw-master-postgresql"]
volumeopts=["--opt o=size=10m --opt device=local --opt type=local","--opt o=size=15000m --opt device=local --opt type=local","--opt o=size=1m --opt device=local --opt type=local","--opt o=size=200m --opt device=local --opt type=local"]

this is the error:

FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to have been in '/home/rgi/ansible/roles/promotedocker/tasks/main.yml': line 267, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: create volume\n  ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'item' is undefined"}

How to do it?

thanks


Solution

  • This is a simple demonstration of the zip filter that will fix your issue. I used 3 input list just to show it can work with more that two (and also because your "abc, 123" example planted a song in my brain for the rest of the day....)

    Note: following your comment, you can acheive the exact same result as below in ansible 2.4 by replacing loop with with_list

    The demo playbook

    ---
    - hosts: localhost
      gather_facts: false
    
      vars:
        list1: [a, b, c]
        list2: [1, 2, 3]
        list3: [do, re, mi]
    
      tasks:
        - name: Love receipe
          debug:
            msg: "{{ item.0 }}, it's easy as {{ item.1 }}, as simple as {{ item.2 }}" 
          loop: "{{ list1 | zip(list2, list3) | list }}"
    

    And the result:

    PLAY [localhost] ***************************************************************************************************************************************************************
    
    TASK [Love receipe] ************************************************************************************************************************************************************
    ok: [localhost] => (item=['a', 1, 'do']) => {
        "msg": "a, it's easy as 1, as simple as do"
    }
    ok: [localhost] => (item=['b', 2, 're']) => {
        "msg": "b, it's easy as 2, as simple as re"
    }
    ok: [localhost] => (item=['c', 3, 'mi']) => {
        "msg": "c, it's easy as 3, as simple as mi"
    }
    
    PLAY RECAP *********************************************************************************************************************************************************************
    localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0