Debugging ACI query task

29 views
Skip to first unread message

Albert Short

unread,
Mar 22, 2020, 2:10:57 PM3/22/20
to Ansible Project
Hello all,

I hope we are all well in this trying period across the world.

I am to debug an ACI query to display a simple output, but having trouble getting the correct syntax to provide the output I desire.

My Ansible Script to query a BD & the debug script to try to display just the subnet information.

- name: ACI Subnet query allocated to BD
  hosts
: apics
  gather_facts
: no

  tasks
:
   
- name: Query BD
      aci_bd_subnet
:
        host
: "{{ ansible_host }}"
        username
: "{{ ansible_user }}"
        password
: "{{ ansible_password }}"
        validate_certs
: no
        tenant
: tn_TRAINER
        state
: query
     
register: subnet_output

   
- name: Debug message for ACI
      debug
:
        msg
: "Subnet : {{subnet_output.current}}"

Now my output:


$ ansible
-playbook aci_debug.yml

PLAY
[ACI Subnet query allocated to BD] ************************************************************************************************************************************************************************************************

TASK
[Query BD] ************************************************************************************************************************************************************************************************************************
ok
: [apic2]

TASK
[Debug message for ACI] ***********************************************************************************************************************************************************************************************************
ok
: [apic2] =>
  msg
: 'Subnet : [{u''fvTenant'': {u''attributes'': {u''dn'': u''uni/tn-tn_TRAINER'', u''lcOwn'': u''local'', u''ownerKey'': u'''', u''name'': u''tn_TRAINER'', u''descr'': u''Trainer Tenant'', u''extMngdBy'': u'''', u''annotation'': u'''', u''status'': u'''', u''monPolDn'': u''uni/tn-common/monepg-default'', u''modTs'': u''2020-03-21T09:32:40.162+00:00'', u''ownerTag'': u'''', u''childAction'': u'''', u''nameAlias'': u'''', u''uid'': u''15374''}, u''children'': [{u''fvBD'': {u''attributes'': {u''uid'': u''15374'', u''extMngdBy'': u'''', u''arpFlood'': u''yes'', u''seg'': u''16482194'', u''mcastAllow'': u''no'', u''ipv6McastAllow'': u''no'', u''intersiteL2Stretch'': u''no'', u''unicastRoute'': u''yes'', u''unkMcastAct'': u''flood'', u''OptimizeWanBandwidth'': u''no'', u''v6unkMcastAct'': u''flood'', u''descr'': u'''', u''hostBasedRouting'': u''no'', u''llAddr'': u''::'', u''monPolDn'': u''uni/tn-common/monepg-default'', u''modTs'': u''2020-03-22T17:41:28.775+00:00'', u''scope'': u''2228224'',
    u''rn'': u''BD-bd_VLAN101'', u''type'': u''regular'', u''ipLearning'': u''yes'', u''status'': u'''', u''vmac'': u''not-applicable'', u''nameAlias'': u'''', u''configIssues'': u'''', u''bcastP'': u''225.1.27.48'', u''mac'': u''00:22:BD:F8:19:FF'', u''epMoveDetectMode'': u'''', u''ownerTag'': u'''', u''intersiteBumTrafficAllow'': u''no'', u''annotation'': u'''', u''childAction'': u'''', u''lcOwn'': u''local'', u''ownerKey'': u'''', u''name'': u''bd_VLAN101'', u''epClear'': u''no'', u''unkMacUcastAct'': u''proxy'', u''multiDstPktAct'': u''bd-flood'', u''limitIpLearnToSubnets'': u''yes'', u''mtu'': u''inherit'', u''pcTag'': u''32771''}, u''children'': [{u''fvSubnet'': {u''attributes'': {u''status'': u'''', u''lcOwn'': u''local'', u''uid'': u''15374'', u''descr'': u'''', u''extMngdBy'': u'''', u''ip'': u''192.168.101.1/24'', u''ctrl'': u'''', u''preferred'': u''no'', u''name'': u'''', u''virtual'': u''no'', u''nameAlias'': u'''', u''scope'': u''private'', u''monPolDn'': u''uni/tn-common/monepg-default'',
    u''rn'': u''subnet-[192.168.101.1/24]'', u''modTs'': u''2020-03-22T17:41:28.723+00:00'', u''annotation'': u'''', u''childAction'': u''''}}}]}}]}}]'


PLAY RECAP
*****************************************************************************************************************************************************************************************************************************
apic2                      
: ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

The verbose output for the 'Query BD' task looks as follows:

TASK [Query BD] ************************************************************************************************************************************************************************************************************************
ok
: [apic2] => changed=false
  ansible_facts
:
    discovered_interpreter_python
: /usr/bin/python
  current
:
 
- fvTenant:
      attributes
:
        annotation
: ''
        childAction
: ''
        descr
: Trainer Tenant
        dn
: uni/tn-tn_TRAINER
        extMngdBy
: ''
        lcOwn
: local
        modTs
: '2020-03-21T09:32:40.162+00:00'
        monPolDn
: uni/tn-common/monepg-default
        name
: tn_TRAINER
        nameAlias
: ''
        ownerKey
: ''
        ownerTag
: ''
        status
: ''
        uid
: '15374'
      children
:
     
- fvBD:
          attributes
:
           
OptimizeWanBandwidth: 'no'
            annotation
: ''
            arpFlood
: 'yes'
            bcastP
: 225.1.27.48
            childAction
: ''
            configIssues
: ''
            descr
: ''
            epClear
: 'no'
            epMoveDetectMode
: ''
            extMngdBy
: ''
            hostBasedRouting
: 'no'
            intersiteBumTrafficAllow
: 'no'
            intersiteL2Stretch
: 'no'
            ipLearning
: 'yes'
            ipv6McastAllow
: 'no'
            lcOwn
: local
            limitIpLearnToSubnets
: 'yes'
            llAddr
: '::'
            mac
: 00:22:BD:F8:19:FF
            mcastAllow
: 'no'
            modTs
: '2020-03-22T17:41:28.775+00:00'
            monPolDn
: uni/tn-common/monepg-default
            mtu
: inherit
            multiDstPktAct
: bd-flood
            name
: bd_VLAN101
            nameAlias
: ''
            ownerKey
: ''
            ownerTag
: ''
            pcTag
: '32771'
            rn
: BD-bd_VLAN101
            scope
: '2228224'
            seg
: '16482194'
            status
: ''
            type
: regular
            uid
: '15374'
            unicastRoute
: 'yes'
            unkMacUcastAct
: proxy
            unkMcastAct
: flood
            v6unkMcastAct
: flood
            vmac
: not-applicable
          children
:
         
- fvSubnet:
              attributes
:
                annotation
: ''
                childAction
: ''
                ctrl
: ''
                descr
: ''
                extMngdBy
: ''
                ip
: 192.168.101.1/24
                lcOwn
: local
                modTs
: '2020-03-22T17:41:28.723+00:00'
                monPolDn
: uni/tn-common/monepg-default
                name
: ''
                nameAlias
: ''
                preferred
: 'no'
                rn
: subnet-[192.168.101.1/24]
                scope
: private
                status
: ''
                uid
: '15374'
               
virtual: 'no'




I am trying to get a debug message similar to:
msg: 'Subnet : 192.168.101.1/24'
But I can't work out how to do that from the registered output
I have tried:


   
- name: Debug message for ACI
      debug
:
        msg
: "Subnet : {{subnet_output.current.fvTenant}}"

But get this meesage:

$ ansible-playbook aci_debug.yml

PLAY
[ACI Subnet query allocated to BD] ************************************************************************************************************************************************************************************************

TASK
[Query BD] ************************************************************************************************************************************************************************************************************************
ok
: [apic2]

TASK
[Debug message for ACI] ***********************************************************************************************************************************************************************************************************
fatal
: [apic2]: FAILED! =>
  msg
: |-
   
The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'fvTenant'

   
The error appears to be in '/home/network_automation/trainer/aci_debug.yml': line 21, column 7, but may
    be elsewhere
in the file depending on the exact syntax problem.

   
The offending line appears to be:


       
- name: Debug message for ACI
         
^ here

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


I have tried to find an answer but not managed to get a solution. Anyone got some guidance?

Thanks & be safe

Dick Visser

unread,
Mar 22, 2020, 4:10:24 PM3/22/20
to ansible...@googlegroups.com
Hi


On Sun, 22 Mar 2020 at 19:11, Albert Short <alber...@gmail.com> wrote:

The error is pretty clear:


> fatal: [apic2]: FAILED! =>
>   msg: |-
>     The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'fvTenant'
>
>     The error appears to be in '/home/network_automation/trainer/aci_debug.yml': line 21, column 7, but may
>     be elsewhere in the file depending on the exact syntax problem.

 Your variable is a list of one item, which itself is deeply nested.
Try this:


    - debug:
        msg: "Subnet: {{ current.0.fvTenant.children.0.fvBD.children.0.fvSubnet.attributes.ip }}"


this is the 'ip' key which has the value that you are looking for -
not sure if this correct?


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1a14f8d3-1463-44d0-9e49-f36f1a1c22ac%40googlegroups.com.


--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Albert Short

unread,
Mar 23, 2020, 1:36:37 AM3/23/20
to Ansible Project
Dick that worked perfectl!
My next question is how would I get the same response to for several subnets without having to use the '0' in the debug message?

    - name: Debug message for ACI
      debug
:
        msg
:

         
- "Subnet : {{subnet_output.current.0.fvTenant.children.0.fvBD.children.0.fvSubnet.attributes.ip}}"
         
- "Subnet : {{subnet_output.current.0.fvTenant.children.0.fvBD.children.1.fvSubnet.attributes.ip}}"
         
- "Subnet : {{subnet_output.current.0.fvTenant.children.0.fvBD.children.2.fvSubnet.attributes.ip}}"

Regards
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Dick Visser

unread,
Mar 23, 2020, 3:55:47 AM3/23/20
to ansible...@googlegroups.com
Hi 

This is slightly more involved, you can use the json_query filter, it has a little bit different syntax.
If you are going to iterate over a number of those subnets, I suggest creating a dedicated variable for that.
Something like this:


  tasks:
    - set_fact:
        subnets: "{{ subnet_output | json_query('current[0].fvTenant.children[0].fvBD.children[].fvSubnet.attributes.ip') }}"

    - debug: var=subnets

    - debug:
        msg: "{% for subnet in subnets %}Subnet: {{ subnet }}\n{% endfor %}"







To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a4f79183-1074-48d0-9bd3-c235a5d3b809%40googlegroups.com.

J

unread,
Mar 23, 2020, 2:11:07 PM3/23/20
to ansible...@googlegroups.com
Hi Albert, 
I got the following code:

tasks:
    - name: gather info on a subnet
      aci_bd_subnet:
        host: 192.168.1.100
        username: admin
        password: "{{password}}"
        tenant: AciTest5
        bd: bd-test5
        state: query
        validate_certs: no
      delegate_to: localhost
      register: results
    - name: print results
      debug:
        var: results.current[0].fvBD.children[0].fvSubnet.attributes.rn
~                                                                       

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

J

unread,
Mar 23, 2020, 4:22:29 PM3/23/20
to ansible...@googlegroups.com
This worked for me for a specific bridge domain that had multiple subnets.
I created a loop to loop over the lists and map the subnet attribute. 

The results looked like this 



tasks:
    - name: gather info on a subnet
      aci_bd_subnet:
        host: 192.168.1.1
        username: admin
        password: "{{password}}"
        tenant: AciTest5
        bd: bd-test5
        state: query
        validate_certs: no
      delegate_to: localhost
      register: results
    - name: print results
      debug:
        msg: "{{item}}"
      loop: "{{results.current[0].fvBD.children[:] | map(attribute='fvSubnet.attributes.rn') | list }}"


TASK [print results] **********************************************************************************************************************************
ok: [192.168.1.1] => (item=subnet-[10.55.2.1/24]) => {
    "msg": "subnet-[10.55.2.1/24]"
}
ok: [192.168.1.1] => (item=subnet-[10.56.2.1/24]) => {
    "msg": "subnet-[10.56.2.1/24]"
}
ok: [192.168.1.1] => (item=subnet-[10.57.2.1/24]) => {
    "msg": "subnet-[10.57.2.1/24]"
}


Reply all
Reply to author
Forward
0 new messages