2

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 }
Pete
  • 35
  • 2
  • 9

1 Answers1

3

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  
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • Thanks for your reply and explanation! Your solution is very close to what I need, is there any way that I can pass in the contents of the 'Virtual_Check' and 'Rule_Check' register variables? Something similar to this: ' loop: "{{ Virtual_Check | zip(Rule_Check) | list }}" ' – Pete Jul 01 '19 at 22:23
  • This was just an example to illustrate, just adapt it to your real world vars. – Zeitounator Jul 01 '19 at 23:30
  • Thanks, @Zeitounator. I understand that what you provided was an example, I'm just not sure it will work properly with the register variables as it's passing random values into the 'modify ltm' command. For example in this instance it passed 'executed_commands' into the command which isn't part of the register variables contents: "invocation": { "module_args": { "auth_provider": null, "chdir": null, "commands": [ "modify ltm virtual executed_commands rules { executed_commands myrule10 } – Pete Jul 02 '19 at 10:36
  • You gave an example data structure, I replied with your example. `zip` will work with any type of list you provide (of strings, numbers, lists, hashmaps...). Are you expecting people here to guess what your real life variables look like and what is the result of your current implementation? Or to install an F5 infrastructure they don't have to try the commands themselves and figure out what they return? Please read [How to create a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and make sure you include the necessary info in your question. – Zeitounator Jul 02 '19 at 12:59
  • I tried to explain my scenario the best I could but this isn't something I use daily so I'm still learning and my knowledge is limited. I edited the question above to include the stdout_lines output of the 'Virtual_Check' and 'Rule_Check'. I understand that it can be frustrating working with someone newer, but sometimes you can ask questions to help guide the individual towards an answer since you have the experience. If you're still willing to help, please let me know if the new output I've provided is helpful, if not, I'll be happy to supply anything else that might be missing. – Pete Jul 02 '19 at 13:32
  • I'm not frustrated. I asked questions and pointed you to the help section.Beginners are more than welcome although that, for your best experience, you must be aware that people here will generally await for quite a bit of effort on your side. Regarding your last edit, it looks like the `stdout_lines` of your first registered are usable as is where your second one need a bit or rework. If you want to use `zip` you need to end up with two list. I would make sure there is not an other way to get that info in a good format directly. We are getting far away from the initial question here. – Zeitounator Jul 03 '19 at 14:01
  • Thanks for looking over my update. I'm currently looking into other options of making this work and if I find something I'll provide an update. Appreciate your zip solution, will come in handy :) – Pete Jul 04 '19 at 12:14