Ansible loop over nested dictionary












0














I have the following data being returned from an API call



"napalm_interfaces_ip": {
"Vlan5": {
"ipv4": {
"10.45.230.250": {
"prefix_length": 24
}
}
}
}


How do I debug print the Vlan info, the IP and the prefix length?



This is what I have



- debug:
msg: "Interface: {{ item.key }}, IP: {{ item.value.ipv4 }}"
with_dict:
- "{{ napalm_interfaces_ip }}"


It produces the below output which shows the IP pointing to another dictionary




TASK [validate_device_ips : debug]





ok: => (item={'value': {u'ipv4': {u'10.45.230.250':
{u'prefix_length': 24}}}, 'key': u'Vlan5'}) => {
"msg": "Interface: Vlan5, IP: {u'10.45.230.250': {u'prefix_length': 24}}" }











share|improve this question





























    0














    I have the following data being returned from an API call



    "napalm_interfaces_ip": {
    "Vlan5": {
    "ipv4": {
    "10.45.230.250": {
    "prefix_length": 24
    }
    }
    }
    }


    How do I debug print the Vlan info, the IP and the prefix length?



    This is what I have



    - debug:
    msg: "Interface: {{ item.key }}, IP: {{ item.value.ipv4 }}"
    with_dict:
    - "{{ napalm_interfaces_ip }}"


    It produces the below output which shows the IP pointing to another dictionary




    TASK [validate_device_ips : debug]





    ok: => (item={'value': {u'ipv4': {u'10.45.230.250':
    {u'prefix_length': 24}}}, 'key': u'Vlan5'}) => {
    "msg": "Interface: Vlan5, IP: {u'10.45.230.250': {u'prefix_length': 24}}" }











    share|improve this question



























      0












      0








      0







      I have the following data being returned from an API call



      "napalm_interfaces_ip": {
      "Vlan5": {
      "ipv4": {
      "10.45.230.250": {
      "prefix_length": 24
      }
      }
      }
      }


      How do I debug print the Vlan info, the IP and the prefix length?



      This is what I have



      - debug:
      msg: "Interface: {{ item.key }}, IP: {{ item.value.ipv4 }}"
      with_dict:
      - "{{ napalm_interfaces_ip }}"


      It produces the below output which shows the IP pointing to another dictionary




      TASK [validate_device_ips : debug]





      ok: => (item={'value': {u'ipv4': {u'10.45.230.250':
      {u'prefix_length': 24}}}, 'key': u'Vlan5'}) => {
      "msg": "Interface: Vlan5, IP: {u'10.45.230.250': {u'prefix_length': 24}}" }











      share|improve this question















      I have the following data being returned from an API call



      "napalm_interfaces_ip": {
      "Vlan5": {
      "ipv4": {
      "10.45.230.250": {
      "prefix_length": 24
      }
      }
      }
      }


      How do I debug print the Vlan info, the IP and the prefix length?



      This is what I have



      - debug:
      msg: "Interface: {{ item.key }}, IP: {{ item.value.ipv4 }}"
      with_dict:
      - "{{ napalm_interfaces_ip }}"


      It produces the below output which shows the IP pointing to another dictionary




      TASK [validate_device_ips : debug]





      ok: => (item={'value': {u'ipv4': {u'10.45.230.250':
      {u'prefix_length': 24}}}, 'key': u'Vlan5'}) => {
      "msg": "Interface: Vlan5, IP: {u'10.45.230.250': {u'prefix_length': 24}}" }








      ansible






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 4:14

























      asked Nov 20 at 23:05









      Anthony Pineda

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          0














          dict2items should help:



          - debug:
          msg: "IP: {{ item.key }} prefix_length: {{ item.value.prefix_length }}"
          loop: "{{ napalm_interfaces_ip.Vlan5.ipv4 | dict2items }}"


          with_dict works too



          - debug:
          msg: "IP: {{ item.key }} prefix_length: {{ item.value.prefix_length }}"
          with_dict: "{{ napalm_interfaces_ip.Vlan5.ipv4 }}"


          Example how to loop the interfaces is below



          - hosts: localhost
          gather_facts: no
          vars:
          interfaces:
          - Vlan0:
          ipv4:
          10.45.230.250:
          prefix_length: 24
          - Vlan1:
          ipv4:
          10.45.230.251:
          prefix_length: 24
          - Vlan2:
          ipv4:
          10.45.230.252:
          prefix_length: 24
          tasks:
          - name: List selected variables
          vars:
          msg: |
          vlan {{ item|dict2items|json_query('.key') }}
          ip {{ item|dict2items|json_query('.value.ipv4|[0]')|dict2items|json_query('.key') }}
          prefix {{ item|dict2items|json_query('.value.ipv4|[0]')|dict2items|json_query('.value.prefix_length') }}
          debug:
          msg: "{{ msg.split('n') }}"
          loop: "{{ interfaces }}"


          Once you decide to make your live easier below is an option



          - hosts: localhost
          vars:
          interfaces:
          - Vlan0:
          ipv4: "10.45.230.250"
          prefix_length: "24"
          - Vlan1:
          ipv4: "10.45.230.251"
          prefix_length: "24"
          - Vlan2:
          ipv4: "10.45.230.252"
          prefix_length: "24"
          tasks:
          - name: List selected variables
          vars:
          msg: |
          vlan {{ item|dict2items|json_query('.key') }}
          ip {{ item|dict2items|json_query('.value.ipv4') }}
          prefix {{ item|dict2items|json_query('.value.prefix_length') }}
          debug:
          msg: "{{ msg.split('n') }}"
          loop: "{{ interfaces }}"





          share|improve this answer























          • Hi Vladimir...thanks you for the response. I actually need to print the interface name, i.e. Vlan5 and then extract the nested IP information and prefix. Also there could be multiple vlans with IPs so I need to find a way to loop through this.
            – Anthony Pineda
            Nov 21 at 14:41










          • Have you tried? What were the results? Any errors?
            – Vladimir Botka
            Nov 22 at 4:53












          • Yes I tried however that is not the solution I was going for. The vlan cannot be emberded in the loop because that information changes with each API call. Also there can be multiple vlans with IP returned so that has to be considered
            – Anthony Pineda
            Nov 23 at 5:21










          • Thank you very much for looking again into this. I appreciate your response. I tested using the code above and the error message I get back. It seems like it won't work with the dictionary information I receive from the system
            – Anthony Pineda
            Nov 26 at 16:47











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402942%2fansible-loop-over-nested-dictionary%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          dict2items should help:



          - debug:
          msg: "IP: {{ item.key }} prefix_length: {{ item.value.prefix_length }}"
          loop: "{{ napalm_interfaces_ip.Vlan5.ipv4 | dict2items }}"


          with_dict works too



          - debug:
          msg: "IP: {{ item.key }} prefix_length: {{ item.value.prefix_length }}"
          with_dict: "{{ napalm_interfaces_ip.Vlan5.ipv4 }}"


          Example how to loop the interfaces is below



          - hosts: localhost
          gather_facts: no
          vars:
          interfaces:
          - Vlan0:
          ipv4:
          10.45.230.250:
          prefix_length: 24
          - Vlan1:
          ipv4:
          10.45.230.251:
          prefix_length: 24
          - Vlan2:
          ipv4:
          10.45.230.252:
          prefix_length: 24
          tasks:
          - name: List selected variables
          vars:
          msg: |
          vlan {{ item|dict2items|json_query('.key') }}
          ip {{ item|dict2items|json_query('.value.ipv4|[0]')|dict2items|json_query('.key') }}
          prefix {{ item|dict2items|json_query('.value.ipv4|[0]')|dict2items|json_query('.value.prefix_length') }}
          debug:
          msg: "{{ msg.split('n') }}"
          loop: "{{ interfaces }}"


          Once you decide to make your live easier below is an option



          - hosts: localhost
          vars:
          interfaces:
          - Vlan0:
          ipv4: "10.45.230.250"
          prefix_length: "24"
          - Vlan1:
          ipv4: "10.45.230.251"
          prefix_length: "24"
          - Vlan2:
          ipv4: "10.45.230.252"
          prefix_length: "24"
          tasks:
          - name: List selected variables
          vars:
          msg: |
          vlan {{ item|dict2items|json_query('.key') }}
          ip {{ item|dict2items|json_query('.value.ipv4') }}
          prefix {{ item|dict2items|json_query('.value.prefix_length') }}
          debug:
          msg: "{{ msg.split('n') }}"
          loop: "{{ interfaces }}"





          share|improve this answer























          • Hi Vladimir...thanks you for the response. I actually need to print the interface name, i.e. Vlan5 and then extract the nested IP information and prefix. Also there could be multiple vlans with IPs so I need to find a way to loop through this.
            – Anthony Pineda
            Nov 21 at 14:41










          • Have you tried? What were the results? Any errors?
            – Vladimir Botka
            Nov 22 at 4:53












          • Yes I tried however that is not the solution I was going for. The vlan cannot be emberded in the loop because that information changes with each API call. Also there can be multiple vlans with IP returned so that has to be considered
            – Anthony Pineda
            Nov 23 at 5:21










          • Thank you very much for looking again into this. I appreciate your response. I tested using the code above and the error message I get back. It seems like it won't work with the dictionary information I receive from the system
            – Anthony Pineda
            Nov 26 at 16:47
















          0














          dict2items should help:



          - debug:
          msg: "IP: {{ item.key }} prefix_length: {{ item.value.prefix_length }}"
          loop: "{{ napalm_interfaces_ip.Vlan5.ipv4 | dict2items }}"


          with_dict works too



          - debug:
          msg: "IP: {{ item.key }} prefix_length: {{ item.value.prefix_length }}"
          with_dict: "{{ napalm_interfaces_ip.Vlan5.ipv4 }}"


          Example how to loop the interfaces is below



          - hosts: localhost
          gather_facts: no
          vars:
          interfaces:
          - Vlan0:
          ipv4:
          10.45.230.250:
          prefix_length: 24
          - Vlan1:
          ipv4:
          10.45.230.251:
          prefix_length: 24
          - Vlan2:
          ipv4:
          10.45.230.252:
          prefix_length: 24
          tasks:
          - name: List selected variables
          vars:
          msg: |
          vlan {{ item|dict2items|json_query('.key') }}
          ip {{ item|dict2items|json_query('.value.ipv4|[0]')|dict2items|json_query('.key') }}
          prefix {{ item|dict2items|json_query('.value.ipv4|[0]')|dict2items|json_query('.value.prefix_length') }}
          debug:
          msg: "{{ msg.split('n') }}"
          loop: "{{ interfaces }}"


          Once you decide to make your live easier below is an option



          - hosts: localhost
          vars:
          interfaces:
          - Vlan0:
          ipv4: "10.45.230.250"
          prefix_length: "24"
          - Vlan1:
          ipv4: "10.45.230.251"
          prefix_length: "24"
          - Vlan2:
          ipv4: "10.45.230.252"
          prefix_length: "24"
          tasks:
          - name: List selected variables
          vars:
          msg: |
          vlan {{ item|dict2items|json_query('.key') }}
          ip {{ item|dict2items|json_query('.value.ipv4') }}
          prefix {{ item|dict2items|json_query('.value.prefix_length') }}
          debug:
          msg: "{{ msg.split('n') }}"
          loop: "{{ interfaces }}"





          share|improve this answer























          • Hi Vladimir...thanks you for the response. I actually need to print the interface name, i.e. Vlan5 and then extract the nested IP information and prefix. Also there could be multiple vlans with IPs so I need to find a way to loop through this.
            – Anthony Pineda
            Nov 21 at 14:41










          • Have you tried? What were the results? Any errors?
            – Vladimir Botka
            Nov 22 at 4:53












          • Yes I tried however that is not the solution I was going for. The vlan cannot be emberded in the loop because that information changes with each API call. Also there can be multiple vlans with IP returned so that has to be considered
            – Anthony Pineda
            Nov 23 at 5:21










          • Thank you very much for looking again into this. I appreciate your response. I tested using the code above and the error message I get back. It seems like it won't work with the dictionary information I receive from the system
            – Anthony Pineda
            Nov 26 at 16:47














          0












          0








          0






          dict2items should help:



          - debug:
          msg: "IP: {{ item.key }} prefix_length: {{ item.value.prefix_length }}"
          loop: "{{ napalm_interfaces_ip.Vlan5.ipv4 | dict2items }}"


          with_dict works too



          - debug:
          msg: "IP: {{ item.key }} prefix_length: {{ item.value.prefix_length }}"
          with_dict: "{{ napalm_interfaces_ip.Vlan5.ipv4 }}"


          Example how to loop the interfaces is below



          - hosts: localhost
          gather_facts: no
          vars:
          interfaces:
          - Vlan0:
          ipv4:
          10.45.230.250:
          prefix_length: 24
          - Vlan1:
          ipv4:
          10.45.230.251:
          prefix_length: 24
          - Vlan2:
          ipv4:
          10.45.230.252:
          prefix_length: 24
          tasks:
          - name: List selected variables
          vars:
          msg: |
          vlan {{ item|dict2items|json_query('.key') }}
          ip {{ item|dict2items|json_query('.value.ipv4|[0]')|dict2items|json_query('.key') }}
          prefix {{ item|dict2items|json_query('.value.ipv4|[0]')|dict2items|json_query('.value.prefix_length') }}
          debug:
          msg: "{{ msg.split('n') }}"
          loop: "{{ interfaces }}"


          Once you decide to make your live easier below is an option



          - hosts: localhost
          vars:
          interfaces:
          - Vlan0:
          ipv4: "10.45.230.250"
          prefix_length: "24"
          - Vlan1:
          ipv4: "10.45.230.251"
          prefix_length: "24"
          - Vlan2:
          ipv4: "10.45.230.252"
          prefix_length: "24"
          tasks:
          - name: List selected variables
          vars:
          msg: |
          vlan {{ item|dict2items|json_query('.key') }}
          ip {{ item|dict2items|json_query('.value.ipv4') }}
          prefix {{ item|dict2items|json_query('.value.prefix_length') }}
          debug:
          msg: "{{ msg.split('n') }}"
          loop: "{{ interfaces }}"





          share|improve this answer














          dict2items should help:



          - debug:
          msg: "IP: {{ item.key }} prefix_length: {{ item.value.prefix_length }}"
          loop: "{{ napalm_interfaces_ip.Vlan5.ipv4 | dict2items }}"


          with_dict works too



          - debug:
          msg: "IP: {{ item.key }} prefix_length: {{ item.value.prefix_length }}"
          with_dict: "{{ napalm_interfaces_ip.Vlan5.ipv4 }}"


          Example how to loop the interfaces is below



          - hosts: localhost
          gather_facts: no
          vars:
          interfaces:
          - Vlan0:
          ipv4:
          10.45.230.250:
          prefix_length: 24
          - Vlan1:
          ipv4:
          10.45.230.251:
          prefix_length: 24
          - Vlan2:
          ipv4:
          10.45.230.252:
          prefix_length: 24
          tasks:
          - name: List selected variables
          vars:
          msg: |
          vlan {{ item|dict2items|json_query('.key') }}
          ip {{ item|dict2items|json_query('.value.ipv4|[0]')|dict2items|json_query('.key') }}
          prefix {{ item|dict2items|json_query('.value.ipv4|[0]')|dict2items|json_query('.value.prefix_length') }}
          debug:
          msg: "{{ msg.split('n') }}"
          loop: "{{ interfaces }}"


          Once you decide to make your live easier below is an option



          - hosts: localhost
          vars:
          interfaces:
          - Vlan0:
          ipv4: "10.45.230.250"
          prefix_length: "24"
          - Vlan1:
          ipv4: "10.45.230.251"
          prefix_length: "24"
          - Vlan2:
          ipv4: "10.45.230.252"
          prefix_length: "24"
          tasks:
          - name: List selected variables
          vars:
          msg: |
          vlan {{ item|dict2items|json_query('.key') }}
          ip {{ item|dict2items|json_query('.value.ipv4') }}
          prefix {{ item|dict2items|json_query('.value.prefix_length') }}
          debug:
          msg: "{{ msg.split('n') }}"
          loop: "{{ interfaces }}"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 at 11:29

























          answered Nov 20 at 23:41









          Vladimir Botka

          79648




          79648












          • Hi Vladimir...thanks you for the response. I actually need to print the interface name, i.e. Vlan5 and then extract the nested IP information and prefix. Also there could be multiple vlans with IPs so I need to find a way to loop through this.
            – Anthony Pineda
            Nov 21 at 14:41










          • Have you tried? What were the results? Any errors?
            – Vladimir Botka
            Nov 22 at 4:53












          • Yes I tried however that is not the solution I was going for. The vlan cannot be emberded in the loop because that information changes with each API call. Also there can be multiple vlans with IP returned so that has to be considered
            – Anthony Pineda
            Nov 23 at 5:21










          • Thank you very much for looking again into this. I appreciate your response. I tested using the code above and the error message I get back. It seems like it won't work with the dictionary information I receive from the system
            – Anthony Pineda
            Nov 26 at 16:47


















          • Hi Vladimir...thanks you for the response. I actually need to print the interface name, i.e. Vlan5 and then extract the nested IP information and prefix. Also there could be multiple vlans with IPs so I need to find a way to loop through this.
            – Anthony Pineda
            Nov 21 at 14:41










          • Have you tried? What were the results? Any errors?
            – Vladimir Botka
            Nov 22 at 4:53












          • Yes I tried however that is not the solution I was going for. The vlan cannot be emberded in the loop because that information changes with each API call. Also there can be multiple vlans with IP returned so that has to be considered
            – Anthony Pineda
            Nov 23 at 5:21










          • Thank you very much for looking again into this. I appreciate your response. I tested using the code above and the error message I get back. It seems like it won't work with the dictionary information I receive from the system
            – Anthony Pineda
            Nov 26 at 16:47
















          Hi Vladimir...thanks you for the response. I actually need to print the interface name, i.e. Vlan5 and then extract the nested IP information and prefix. Also there could be multiple vlans with IPs so I need to find a way to loop through this.
          – Anthony Pineda
          Nov 21 at 14:41




          Hi Vladimir...thanks you for the response. I actually need to print the interface name, i.e. Vlan5 and then extract the nested IP information and prefix. Also there could be multiple vlans with IPs so I need to find a way to loop through this.
          – Anthony Pineda
          Nov 21 at 14:41












          Have you tried? What were the results? Any errors?
          – Vladimir Botka
          Nov 22 at 4:53






          Have you tried? What were the results? Any errors?
          – Vladimir Botka
          Nov 22 at 4:53














          Yes I tried however that is not the solution I was going for. The vlan cannot be emberded in the loop because that information changes with each API call. Also there can be multiple vlans with IP returned so that has to be considered
          – Anthony Pineda
          Nov 23 at 5:21




          Yes I tried however that is not the solution I was going for. The vlan cannot be emberded in the loop because that information changes with each API call. Also there can be multiple vlans with IP returned so that has to be considered
          – Anthony Pineda
          Nov 23 at 5:21












          Thank you very much for looking again into this. I appreciate your response. I tested using the code above and the error message I get back. It seems like it won't work with the dictionary information I receive from the system
          – Anthony Pineda
          Nov 26 at 16:47




          Thank you very much for looking again into this. I appreciate your response. I tested using the code above and the error message I get back. It seems like it won't work with the dictionary information I receive from the system
          – Anthony Pineda
          Nov 26 at 16:47


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402942%2fansible-loop-over-nested-dictionary%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'