Using with_nested and Ansible

Viewed 4575

Would anyone be able to recommend a way to take the contents of two register variables and pass them into one command? While also lining up the results of the outputs from each variable in a 1:1 fashion. (ie. VS1:rule1, VS2:rule2, and so on from the output shown below)

Here is what's stored in stdout_lines for 'Virtual_Check' and 'Rule_Check':

     "Virtual_Check.stdout_lines": [
        [
            "ltm virtual VS1 ", 
            "ltm virtual VS2 ", 
            "ltm virtual VS3 ", 
            "ltm virtual VS",           
    "Rule_Check.stdout_lines": [
        [
            "myrule1", 
            "    ", 
            "", 
            "        myrule2", 
            "    ", 
            "", 
            "        myrule3", 
            "    ", 
            "", 
            "        myrule4", 
            "    ", 
            "", 

Now, I would like to pass the contents of the variables into one command as shown below. When I run this playbook the 'Virtual_Check' portion under 'with_nested' loops as expected, but the issue I'm running into is it won't loop properly for the 'Rule_Check' portion (I've left in the two methods I tried below)

So far I've tried using with_nested to accomplish this and it seems to no be looping over the second variable correctly.

     - name:  Update VS iRule
        bigip_command:
          commands:
            - "modify ltm virtual {{ item.0 }} rules { {{ item.1 }} myrule10 }"
          provider:
            server: "{{ inventory_hostname }}"
            password: "{{ remote_passwd }}"
            user: "{{ remote_username }}"
          validate_certs: no
        delegate_to: localhost
        with_nested:
          - [ "{{ Virtual_Check['stdout'][0] | replace('ltm virtual', '') | replace('\n', '') }}"]
          - [ "{{ Rule_Check['stdout'][0] | replace('\n', '') }}" ]
          - [ "{{ Rule_Check['stdout_lines'][0] }}" ]

I would expect that the 'modify ltm virtual {{ item.0 }} rules { {{ item.1 }} myrule10 }' line would be processed with the content within the Virtual_Check & Rule_Check lists

For example:

modify ltm virtual VS1 rules { myrule1 myrule10 }
modify ltm virtual VS2 rules { myrule2 myrule10 }
modify ltm virtual VS3 rules { myrule3 myrule10 }
modify ltm virtual VS4 rules { myrule4 myrule10 }
1 Answers

The nested lookup does not accomplish what your are expecting: it creates a loop on first element with a sub-loop on second element and a sub-sub-loop on third element, etc...

What you are looking for is the zip filter which will allow you to assemble several lists in a single one joining all items of same index together in a list.

Example below with your original sample data in you question. You just have to adapt to your real case:

---
- name: zip example
  hosts: localhost
  gather_facts: false

  vars:
    servers: [ 'VS1', 'VS2', 'VS3', 'VS4' ]
    rules: [ myrule1, myrule2, myrule3, myrule4 ]

  tasks:
    - name: Show zipped data from servers and rules
      debug:
        msg: "Server {{ item.0 }} has rule: {{ item.1 }}"
      loop: "{{ servers | zip(rules) | list }}"

which gives

PLAY [zip example] ********************************************************************************************************************************************************************************************************

TASK [Show zipped data from servers and rules] ****************************************************************************************************************************************************************************
ok: [localhost] => (item=['VS1', 'myrule1']) => {
    "msg": "Server VS1 has rule: myrule1"
}
ok: [localhost] => (item=['VS2', 'myrule2']) => {
    "msg": "Server VS2 has rule: myrule2"
}
ok: [localhost] => (item=['VS3', 'myrule3']) => {
    "msg": "Server VS3 has rule: myrule3"
}
ok: [localhost] => (item=['VS4', 'myrule4']) => {
    "msg": "Server VS4 has rule: myrule4"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
Related