On 8/30/20 11:54 AM, Kevin Hoyt wrote:
Hi Rich,

I'm looking to collect the name field into a list and then use that in further tasks.


Something like this:

set_fact:

  keyvallist: "{{ hc_pim_info.dpListProcessesReply.dpServiceList.dpService | map(attribute='@key') | list }}"

see the examples under https://jinja.palletsprojects.com/en/2.11.x/templates/#map



Thanks,
Kevin

On Sun, Aug 30, 2020 at 12:51 PM Rich Megginson <[email protected] <mailto:[email protected]>> wrote:


    On 8/29/20 3:18 PM, Kevin Hoyt wrote:
    Ahh got it!  ok, that worked.  1 more question, since that is an
    array at dpService, I want to iterate through that.  I tried
    leaving the brackets blank, but that failed.  How would I tell it
    to give me each element?


    I guess it depends on what you want to do with it.

    Do you want to use it in a loop?
    https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#

    Do you want to select some elements from the list?
    https://jinja.palletsprojects.com/en/2.11.x/templates/#select or
    selectattr

    Do you want to perform some modification to each item?
    https://jinja.palletsprojects.com/en/2.11.x/templates/#map

    or
    
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#list-filters



    Thanks for the help!
    Kevin


    On Sat, Aug 29, 2020 at 3:32 PM Rich Megginson
    <[email protected] <mailto:[email protected]>> wrote:

        On 8/29/20 1:20 PM, Kevin Hoyt wrote:
        Hi John,

        Thank you for the response.  I just tried it and got the
        following error:
        "msg": "template error while templating string: unexpected
        char u'\\u2018' at 63. String: {{
        hc_pim_info.dpListProcessesReply.dpServiceList.dpService[0][‘@Name’]
        }}"

        Must be a copy/paste error - those single quotes around @Name
        are not ASCII quotes:

        >cat <<EOF | od -c
        > [‘@Name’]
        > EOF
        0000000   [ 342 200 230   @   N   a   m   e 342 200 231   ]  \n
        0000016

        Works fine for me if I use plain ASCII single quotes.


        It just doesn't seem to like that '@Name' at all.  Do you
        have any other suggestions?

        Thanks,
        Kevin





        On Sat, Aug 29, 2020 at 2:03 PM John Westcott
        <[email protected] <mailto:[email protected]>> wrote:

            If I’m reading the json right, try: {{
            hc_pim_info.dpListProcessesReply.dpServiceList.dpService[0][‘@Name’]
            }}

            -John

            Sent from my iPhone

            On Aug 28, 2020, at 16:41, Kevin Hoyt
            <[email protected] <mailto:[email protected]>> wrote:

            Hi everyone, I'm just learning ansible and having some
            trouble parsing out some json data. I'm hoping someone
            can help me.  What I'm looking for is the Name field
            at hc_pim_info.dpListProcessesReply.dpServiceList.dpService


            I can get to this level, but can't get past the array
            and then the @Name.  When I do, I get errors like this:
            "msg": "template error while templating string:
            expected name or number. String: {{
            
hc_pim_info.dpListProcessesReply.dpServiceList.dpService[0].\"@Name\"
            }}"

            I've tried a handful of combos to get this but nothing
            seems to work. Any help would be appreciated.

            Here is the sample data, the data I'm after is in *bold*:
                    "hc_pim_info": {
            "dpListProcessesReply": {
                            "@ReturnCode": "0",
                            "@xmlnsdp":
            "http//www.cisco.com/vtg/diagnosticportal
            <http://www.cisco.com/vtg/diagnosticportal>",
                            "dpSchema": {
                                "@Version": "1.0"
                            },
            "dpServiceList": {
            "dpService": [
                                    {
            *"@Name": "CTI Server 1A", *
            "dpProcessList": {
            "dpProcessProp": [
                {
                    "@Description": "Node Manager",
                    "@Name": "nodeman.exe",
                    "@UpTime": "10.052531.5637483"
                },
                {
                    "@Description": "Node Manager Manager",
                    "@Name": "nmm.exe",
                    "@UpTime": "10.052528.7086193"
                },
                {
                    "@Description": "CTISrvr",
                    "@Name": "ctisrvr.exe",
                    "@Status": "test-CG1A ctisvr - [ACTIVE 
            myserver SecurityMode - MIXED (NonSecurePort 42027,
            SecurePort 42030)]",
                    "@UpTime": "10.052528.6358605"
                }
            ]
                                        }
                                    },
                                    {
            *"@Name": "Peripheral Gateway 101A"*
                                    },
                                    {
            *"@Name": "Peripheral Gateway 1A", *
            "dpProcessList": {
            "dpProcessProp": [
                {
                    "@Description": "Node Manager",
                    "@Name": "nodeman.exe",
                    "@UpTime": "10.052531.6053188"
                },
                {
                    "@Description": "Node Manager Manager",
                    "@Name": "nmm.exe",
                    "@UpTime": "10.052528.7846315"
                },
                {
                    "@Description": "JtapigwJVMLauncher EXE",
                    "@Name": "jtapigw.exe",
                    "@Status": "test-PG1A jtapigw - jgw1 - [ ACTIVE
            ]",
                    "@UpTime": "10.052451.7927267"
                },
                {
                    "@Description": "Message Delivery System",
                    "@Name": "mdsproc.exe",
                    "@Status": "test-PG1A mdsproc - (InSvc Pr-Enb
            Clk)",
                    "@UpTime": "10.052451.8051968"
                },
                {
                    "@Description": "OPC-CCE",
                    "@Name": "opc-cce.exe",
                    "@Status": "test-PG1A opc-cce.exe",
                    "@UpTime": "10.052451.6220285"
                },
                {
                    "@Description": "PG DMP Agent",
                    "@Name": "pgagent.exe",
                    "@Status": "test-PG1A pgagent - (InSvc AActive
            BIdle)",
                    "@UpTime": "10.052451.6203346"
                },
                {
                    "@Description": "EAGTPIM EXE",
                    "@Name": "eagtpim.exe",
                    "@Status": "test-PG1A eagtpim - pim1 -
            [CUCM_PG01_1 ACTIVE] ",
                    "@UpTime": "10.052451.5644641"
                },
                {
                    "@Description": "rttest application",
                    "@Name": "testsync.exe",
                    "@Status": "test-PG1A testsync",
                    "@UpTime": "10.052451.5628289"
                }
            ]
                                        }
                                    },
                                    {
            "@Name": "Cisco ICM Diagnostic Framework",
            "dpProcessList": {
            "dpProcessProp": {
                "@Description": "Cisco ICM Diagnostic Framework",
                "@Name": "DiagFwSvc.exe",
                "@UpTime": "10.052318.6787144"
            }
                                        }
                                    },
                                    {
            "@Name": "Peripheral Gateway 100A",
            "dpProcessList": {
            "dpProcessProp": [
                {
                    "@Description": "Node Manager",
                    "@Name": "nodeman.exe",
                    "@UpTime": "7.015007.0718110"
                },
                {
                    "@Description": "Node Manager Manager",
                    "@Name": "nmm.exe",
                    "@UpTime": "7.015006.7374239"
                },
                {
                    "@Description": "Message Delivery System",
                    "@Name": "mdsproc.exe",
                    "@Status": "test-PG100A mdsproc - (InSvc Pr-Enb)",
                    "@UpTime": "7.015006.7339806"
                },
                {
                    "@Description": "OPC-TDM",
                    "@Name": "opc-tdm.exe",
                    "@Status": "test-PG100A opc-tdm.exe",
                    "@UpTime": "7.015006.7265826"
                },
                {
                    "@Description": "PG DMP Agent",
                    "@Name": "pgagent.exe",
                    "@Status": "test-PG100A pgagent - (InSvc
            AActive BIdle)",
                    "@UpTime": "7.015006.7233020"
                },
                {
                    "@Description": "vrupim",
                    "@Name": "vrupim.exe",
                    "@Status": "test-PG100A vrupim - pim1 - [SECURE
            CVP_PG100_1 ACTIVE]",
                    "@UpTime": "7.015006.7310432"
                },
                {
                    "@Description": "vrupim",
                    "@Name": "vrupim.exe",
                    "@Status": "test-PG100A vrupim - pim2 - [SECURE
            CVP_PG100_2 ACTIVE]",
                    "@UpTime": "7.015006.7284294"
                },
                {
                    "@Description": "vrupim",
                    "@Name": "vrupim.exe",
                    "@Status": "test-PG100A vrupim - pim3 -
            [NON-SECURE HuCallback01 ACTIVE]",
                    "@UpTime": "7.015006.7214459"
                },
                {
                    "@Description": "vrupim",
                    "@Name": "vrupim.exe",
                    "@Status": "test-PG100A vrupim - pim4 -
            [NON-SECURE HuCallback02 ACTIVE]",
                    "@UpTime": "7.015006.6647717"
                },
                {
                    "@Description": "rttest application",
                    "@Name": "testsync.exe",
                    "@Status": "test-PG100A testsync",
                    "@UpTime": "7.015006.5579308"
                }
            ]
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
-- You received this message because you are subscribed to
            the Google Groups "Ansible Development" group.
            To unsubscribe from this group and stop receiving
            emails from it, send an email to
            [email protected]
            <mailto:[email protected]>.
            To view this discussion on the web visit
            
https://groups.google.com/d/msgid/ansible-devel/04701668-e7dc-4ec9-b0de-7d02594edf51n%40googlegroups.com
            
<https://groups.google.com/d/msgid/ansible-devel/04701668-e7dc-4ec9-b0de-7d02594edf51n%40googlegroups.com?utm_medium=email&utm_source=footer>.



-- CATAPULTAM HABEO. NISI PECUNIAM OMNEM MIHI DABIS, AD CAPUT
        TUUM SAXUM IMMANE MITTAM
-- You received this message because you are subscribed to the
        Google Groups "Ansible Development" group.
        To unsubscribe from this group and stop receiving emails
        from it, send an email to
        [email protected]
        <mailto:[email protected]>.
        To view this discussion on the web visit
        
https://groups.google.com/d/msgid/ansible-devel/CABiKD-FfcqWCyGrEnAZ_0hOTKcu4YTpNY_F2nMVqukKc2%3DSBvQ%40mail.gmail.com
        
<https://groups.google.com/d/msgid/ansible-devel/CABiKD-FfcqWCyGrEnAZ_0hOTKcu4YTpNY_F2nMVqukKc2%3DSBvQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
-- You received this message because you are subscribed to the
        Google Groups "Ansible Development" group.
        To unsubscribe from this group and stop receiving emails from
        it, send an email to
        [email protected]
        <mailto:[email protected]>.
        To view this discussion on the web visit
        
https://groups.google.com/d/msgid/ansible-devel/9815e00a-894a-879d-70ff-d76b2d86dd77%40redhat.com
        
<https://groups.google.com/d/msgid/ansible-devel/9815e00a-894a-879d-70ff-d76b2d86dd77%40redhat.com?utm_medium=email&utm_source=footer>.



-- CATAPULTAM HABEO. NISI PECUNIAM OMNEM MIHI DABIS, AD CAPUT TUUM
    SAXUM IMMANE MITTAM
-- You received this message because you are subscribed to the
    Google Groups "Ansible Development" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to [email protected]
    <mailto:[email protected]>.
    To view this discussion on the web visit
    
https://groups.google.com/d/msgid/ansible-devel/CABiKD-HFMw91UdSW6mnoHnqGQxz%3DA2PmT9pDx-c-odv8C11Drw%40mail.gmail.com
    
<https://groups.google.com/d/msgid/ansible-devel/CABiKD-HFMw91UdSW6mnoHnqGQxz%3DA2PmT9pDx-c-odv8C11Drw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
-- You received this message because you are subscribed to the Google
    Groups "Ansible Development" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to [email protected]
    <mailto:[email protected]>.
    To view this discussion on the web visit
    
https://groups.google.com/d/msgid/ansible-devel/2d4ad56a-d586-a25d-a442-ebef70b733cc%40redhat.com
    
<https://groups.google.com/d/msgid/ansible-devel/2d4ad56a-d586-a25d-a442-ebef70b733cc%40redhat.com?utm_medium=email&utm_source=footer>.



--
CATAPULTAM HABEO. NISI PECUNIAM OMNEM MIHI DABIS, AD CAPUT TUUM SAXUM IMMANE MITTAM

--
You received this message because you are subscribed to the Google Groups "Ansible 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-devel/67b323ca-7043-f345-26bf-7467b7578068%40redhat.com.

Reply via email to