Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues using with_items with from a list of dictionary #148

Closed
lmshippy opened this issue Dec 22, 2016 · 9 comments
Closed

Issues using with_items with from a list of dictionary #148

lmshippy opened this issue Dec 22, 2016 · 9 comments

Comments

@lmshippy
Copy link

I am trying to use the show ip int brief command and then use a variable from it do a show run int {{ variable }} Below is my code. I am wanting to reference one key out the list of dictionaries but not able to get it to work.


  • name: COLLECT DATA TEST
    hosts: iosrouters
    gather_facts: no
    connection: local

    tasks:

    • name: GET INTERFACES
      ntc_show_command:
      connection=ssh
      platform={{ vendor }}{{ os }}{{ api }}
      index_file=index
      template_dir=/usr/lib/python2.7/dist-packages/ansible/modules/ntc-ansible/ntc-templates/templates
      command='show ip int brief'
      host={{ inventory_hostname }}
      username={{ un }}
      password={{ pwd }}
      tags: test
      register: interfaces

    • name: SET FACT
      set_fact:
      interface: "{{ interfaces.response }}"
      tags: test

    • name: GET INTERFACES
      ntc_show_command:
      connection=ssh
      platform={{ vendor }}{{ os }}{{ api }}
      index_file=interface.index
      template_dir=/usr/lib/python2.7/dist-packages/ansible/modules/ntc-ansible/ntc-templates/templates
      command="show run int {{ item.intf }}" <--- where I am having issues tried
      host={{ inventory_hostname }}
      username={{ un }}
      password={{ pwd }}
      tags: test
      with_items: "{{ interface }}"
      register: interfaceinfo

@GGabriele
Copy link
Contributor

Hi @lmshippy ,
that's because the module only supports a limited amount of commands and show run X is not one of them.

You can take a look at all the supported commands here

@lmshippy
Copy link
Author

I am using a custom index file, and a custom template. The problem is not with that, the problem I am having is with being a pull the interfaces dynamically and then use them with with_items. Basically following the example Jason posted in the link below.

http://jedelman.com/home/network-automation-with-ansible-dynamically-configuring-interface-descriptions/

@jedelman8
Copy link
Member

Hi @lmshippy,

Can you show the output running in verbose mode or add a task like this:

debug: var=interface

And do this after your set_fact task. Show us this output.

Thanks.

@lmshippy
Copy link
Author

image

@jedelman8
Copy link
Member

Actually something seems off. You did a debug and you're still seeing the response key. Let's eliminate the set_fact for now and just do this:

with_items: "{{ interfaces.response }}"

@lmshippy
Copy link
Author

lmshippy commented Dec 22, 2016

Here is the new playbook and below is the debug output.

---

  - name: COLLECT DATA TEST
    hosts: iosrouters
    gather_facts: no
    connection: local

    tasks:
      - name: GET INTERFACES
        ntc_show_command:
          connection=ssh
          platform={{ vendor }}_{{ os }}_{{ api }}
          index_file=index
          template_dir=/usr/lib/python2.7/dist-packages/ansible/modules/ntc-ansible/ntc-templates/templates
          command='show ip int brief'
          host={{ inventory_hostname }}
          username={{ un }}
          password={{ pwd }}
        tags: test
        register: interfaces

      - name: GET INTERFACES DETAIL
        ntc_show_command:
          connection=ssh
          platform={{ vendor }}_{{ os }}_{{ api }}
          index_file=interface.index
          template_dir=/usr/lib/python2.7/dist-packages/ansible/modules/ntc-ansible/ntc-templates/templates
          command="show run int {{ item.intf }}"
          host={{ inventory_hostname }}
          username={{ un }}
          password={{ pwd }}
        tags: test
        with_items: "{{ interfaces.response }}"
        register: interfaceinfo



TASK [GET INTERFACES] **********************************************************
ok: [x.x.x.x] => {"changed": false, "response": [{"intf": "Embedded-Service-Engine0/0", "ipaddr": "unassigned", "proto": "down", "status": "administratively down"}, {"intf": "GigabitEthernet0/0", "ipaddr": "x.x.x.x", "proto": "up", "status": "up"}, {"intf": "GigabitEthernet0/1", "ipaddr": "x.x.x.x", "proto": "up", "status": "up"}, {"intf": "Loopback0", "ipaddr": "x.x.x.x", "proto": "up", "status": "up"}, {"intf": "Tunnel1", "ipaddr": "x.x.x.x", "proto": "up", "status": "up"}, {"intf": "Tunnel2", "ipaddr": "x.x.x.x", "proto": "up", "status": "up"}], "response_list": []}

TASK [GET INTERFACES DETAIL] ***************************************************
ok: [x.x.x.x] => (item={u'status': u'administratively down', u'intf': u'Embedded-Service-Engine0/0', u'ipaddr': u'unassigned', u'proto': u'down'}) => {"changed": false, "item": {"intf": "Embedded-Service-Engine0/0", "ipaddr": "unassigned", "proto": "down", "status": "administratively down"}, "response": [{"helper": "", "hsrp": "", "interface": "Embedded-Service-Engine0/0", "ip_addr": "", "sub_mask": ""}], "response_list": []}
ok: [x.x.x.x] => (item={u'status': u'up', u'intf': u'GigabitEthernet0/0', u'ipaddr': u'x.x.x.x', u'proto': u'up'}) => {"changed": false, "item": {"intf": "GigabitEthernet0/0", "ipaddr": "x.x.x.x", "proto": "up", "status": "up"}, "response": [{"helper": "", "hsrp": "", "interface": "GigabitEthernet0/0", "ip_addr": "x.x.x.x", "sub_mask": "x.x.x.x"}], "response_list": []}
ok: [x.x.x.x] => (item={u'status': u'up', u'intf': u'Loopback0', u'ipaddr': u'x.x.x.x', u'proto': u'up'}) => {"changed": false, "item": {"intf": "Loopback0", "ipaddr": "x.x.x.x", "proto": "up", "status": "up"}, "response": [{"helper": "", "hsrp": "", "interface": "Loopback0", "ip_addr": "x.x.x.x", "sub_mask": "x.x.x.x"}], "response_list": []}
ok: [x.x.x.x] => (item={u'status': u'up', u'intf': u'Tunnel1', u'ipaddr': u'x.x.x.x', u'proto': u'up'}) => {"changed": false, "item": {"intf": "Tunnel1", "ipaddr": "x.x.x.x", "proto": "up", "status": "up"}, "response": [{"helper": "", "hsrp": "", "interface": "Tunnel1", "ip_addr": "x.x.x.x", "sub_mask": "x.x.x.x"}], "response_list": []}
ok: [x.x.x.x] => (item={u'status': u'up', u'intf': u'Tunnel2', u'ipaddr': u'x.x.x.x', u'proto': u'up'}) => {"changed": false, "item": {"intf": "Tunnel2", "ipaddr": "x.x.x.x", "proto": "up", "status": "up"}, "response": [{"helper": "", "hsrp": "", "interface": "Tunnel2", "ip_addr": "x.x.x.x", "sub_mask": "x.x.x.x"}], "response_list": []}

@jedelman8
Copy link
Member

Looks like it worked...what am I missing?

If you add a debug: var=interfaceinfo, you should have what you need, no?

@lmshippy
Copy link
Author

Actually looking, you are correct, the information is in there. The file we were creating was putting in a bunch of extra stuff at the top but we were able to figure it out. Quick question, with the loop it is reconnecting to the device each time for the looped command, is there any way to have it maintain the connection for the loop?

@GGabriele
Copy link
Contributor

Hey @lmshippy ,

this is not possible yet, but it's a feature we will release in the future :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants