Ranjit Singh
Ranjit Singh

Reputation: 37

loop over with_subelements ansible

Hi have following variable:

couchbase:
 - name: incre1
   ipaddress:
    - 10.16.9.177
    - 10.16.9.178
   buckets:
    - AA1
    - aa1

And my plabook have following:

 - debug:
    msg: "Running backup as {{CBBACKUPMGR}} backup -r {{ item.1 }} --cluster couchbase://{{ item.0.ipaddress }}"
   register: example
   with_subelements:
     - "{{ couchbase }}"
     -  buckets

And i want to iterate over ipaddress and then with buckets, so basically i want to see:

Running backup as /opt/ouchbase backup -r AA1 --cluster couchbase://10.16.9.177
Running backup as /opt/ouchbase backup -r aa1 --cluster couchbase://10.16.9.177
Running backup as /opt/ouchbase backup -r AA1 --cluster couchbase://10.16.9.178
Running backup as /opt/ouchbase backup -r aa1 --cluster couchbase://10.16.9.178

However, when running the playbook i'm seeing as following:

Running backup as /opt/ouchbase backup -r AA1 --cluster couchbase://[u'10.16.9.177', u'10.16.9.178']
Running backup as /opt/ouchbase backup -r aa1 --cluster couchbase://[u'10.16.9.177', u'10.16.9.178']

Upvotes: 1

Views: 2263

Answers (1)

ilias-sp
ilias-sp

Reputation: 6705

thats not what with_subelements does. If you use this "debug" loop to print the {{ item }}, you will see that on each iteration, it creates a list of:

  1. the parent element from the couchbase list that holds the subelement you specified, WITHOUT the hash of that subelement and
  2. the value of the subelement of that iteration.

here is the output:

TASK [debug] ********************************************************************************************************************************************************************************************************
ok: [localhost] => (item=None) => {
    "msg": [
        {
            "ipaddress": [
                "10.16.9.177", 
                "10.16.9.178"
            ], 
            "name": "incre1"
        }, 
        "AA1"
    ]
}
ok: [localhost] => (item=None) => {
    "msg": [
        {
            "ipaddress": [
                "10.16.9.177", 
                "10.16.9.178"
            ], 
            "name": "incre1"
        }, 
        "aa1"
    ]
}

PLAY RECAP

As you clarified, your intention is to generate all possible combinations between ipaddress and buckets.

to achieve this, try this task:

  - debug:
      msg: "Running backup as {{CBBACKUPMGR}} backup -r {{ item[0] }} --cluster couchbase://{{ item[1] }}"
    register: example
    with_items:
      - "{{ lookup('nested', couchbase[0].ipaddress, couchbase[0].buckets) }}"

This assumes you will have the couchbase list variable with only one element, just as it is in your example.

result:

TASK [debug] ********************************************************************************************************************************************************************************************************
ok: [localhost] => (item=None) => {
    "msg": "Running backup as /opt/ouchbase backup -r 10.16.9.177 --cluster couchbase://AA1"
}
ok: [localhost] => (item=None) => {
    "msg": "Running backup as /opt/ouchbase backup -r 10.16.9.177 --cluster couchbase://aa1"
}
ok: [localhost] => (item=None) => {
    "msg": "Running backup as /opt/ouchbase backup -r 10.16.9.178 --cluster couchbase://AA1"
}
ok: [localhost] => (item=None) => {
    "msg": "Running backup as /opt/ouchbase backup -r 10.16.9.178 --cluster couchbase://aa1"
}

PLAY RECAP

hope it helps.

Upvotes: 1

Related Questions