Find matched element in key value

116 views
Skip to first unread message

Rama

unread,
Jan 7, 2019, 11:21:41 AM1/7/19
to Ansible Project
Hi All,

Our required in-built module gives huge output of different keys that have multiple values with objects. I used 'with_dict' and when to narrow and get desired output as below.  Here also am facing a road block that how can I get the element where any value doesn't meet. How can get output of the block when "status": "CRITICAL" instead of all.

Output:
"msg": {
        "key": "devices",
        "value": [
            {
                "configuration": "Configured",
                "version": "PD-0.7",
                "label": "device-1",
                "location": "1",
                "serial_number": "PWH12GVF",
                "status": "OK"
            },
            {
                "configuration": "Configured",
                "version": "PD-0.7",
                "label": "device-2",
                "location": "2",
                "serial_number": "PWH12GWF",
                "status": "OK"
            },
            {
                "configuration": "Configured",
                "version": "PD-0.7",
                "label": "device-3",
                "location": "3",
                "serial_number": "PWH12GXF",
                "status": "CRITICAL"
            }
        ]
    }



Desired output:
"configuration": "Configured",
 version": "PD-0.7",
 "label": "device-3",
 "location": "3",
 "serial_number": "PWH12GXF",
 "status": "CRITICAL"

Kai Stian Olstad

unread,
Jan 7, 2019, 12:16:16 PM1/7/19
to ansible...@googlegroups.com
You can use the Jinja selectattr

{{ msg.value | selectattr('status', 'equalto', 'CRITICAL') | list }}


--
Kai Stian Olstad


Rama

unread,
Jan 8, 2019, 8:31:23 AM1/8/19
to Ansible Project
Hi,

Thank you for your reply.  I added another 'debug', var with given jinja variable. But unfortunately, it gives error with below. Can you please assist.


fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'msg' is undefined\n\nThe error appears to have been in '/root/devices..yml': line 20, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n   - debug:\n     ^ here\n"}

Kai Stian Olstad

unread,
Jan 8, 2019, 11:09:48 AM1/8/19
to ansible...@googlegroups.com
On Tuesday, 8 January 2019 14:31:23 CET Rama wrote:
> Thank you for your reply. I added another 'debug', var with given jinja
> variable. But unfortunately, it gives error with below. Can you please
> assist.
>
>
> fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an
> undefined variable. The error was: 'msg' is undefined\n\nThe error appears
> to have been in '/root/devices..yml': line 20, column 6, but may\nbe
> elsewhere in the file depending on the exact syntax problem.\n\nThe
> offending line appears to be:\n\n\n - debug:\n ^ here\n"}

Without the code there is nothing I can do to assist.


--
Kai Stian Olstad


Rama

unread,
Jan 8, 2019, 12:26:54 PM1/8/19
to Ansible Project
Hi,

The playbook is as follows and highlighted text is added.

---
- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
   - name: ILO Facts
     hpilo_facts:
      host: 10.20.64.51
      login: xxxxxxx
      password: xxxxxxx
     delegate_to: localhost

   - debug:
       msg: "{{item}}"
     with_dict: "{{ hw_health.storage['Controller on System Board']['logical_drives'] }}"
     when:
      - "'physical_drives' in item.key"


   - debug:
      var: "{{ msg| selectattr('status', 'equalto', 'CRIRICAL') | list }}"

Kai Stian Olstad

unread,
Jan 8, 2019, 3:20:28 PM1/8/19
to ansible...@googlegroups.com
On Tuesday, 8 January 2019 18:26:54 CET Rama wrote:
> Hi,
>
> The playbook is as follows and highlighted text is added.
>
> ---
> - hosts: localhost
> connection: local
> gather_facts: no
> tasks:
> - name: ILO Facts
> hpilo_facts:
> host: 10.20.64.51
> login: xxxxxxx
> password: xxxxxxx
> delegate_to: localhost
>
> - debug:
> msg: "{{item}}"
> with_dict: "{{ hw_health.storage['Controller on System
> Board']['logical_drives'] }}"
> when:
> - "'physical_drives' in item.key"
>
>
> - debug:
> var: "{{ msg| selectattr('status', 'equalto', 'CRIRICAL') | list }}"

You don't have any variable in here called msg, so when you use msg in the debug task it will fail with undefined variable.
In you first mail you did have an output that was called msg.


--
Kai Stian Olstad


Rama

unread,
Jan 8, 2019, 3:35:55 PM1/8/19
to Ansible Project
Hi,

With this playbook we still getting msg output with first debug that i mentioned in my mail. It fails where second debug defined with 'selectattr'

Kai Stian Olstad

unread,
Jan 8, 2019, 3:58:03 PM1/8/19
to ansible...@googlegroups.com
On Tuesday, 8 January 2019 21:35:54 CET Rama wrote:
> Hi,
>
> With this playbook we still getting msg output with first debug that i
> mentioned in my mail. It fails where second debug defined with 'selectattr'

OK, then I understand where the output are from.

Your with_dict debug task make things more confusing, it would be better to get the output of this task

- debug:
var: hw_health.storage['Controller on System Board']['logical_drives']


--
Kai Stian Olstad


Rama

unread,
Jan 8, 2019, 4:21:44 PM1/8/19
to Ansible Project
Hi,

Edited the PB and it is

---
- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
   - name: ILO Facts
     hpilo_facts:
      host: 10.20.64.51
      login: xxxxxxx
      password: xxxxxxx
     delegate_to: localhost

   - debug:
       var: hw_health.storage['Controller on System Board']['logical_drives']
      
      
      
Got output:
TASK [debug] **************************************************************************************************************************************
ok: [localhost] => {
    "hw_health.storage['Controller on System Board']['logical_drives']": [
        {
            "encryption_status": "Not Encrypted",
            "label": "01",
            "physical_drives": [

              {
                "configuration": "Configured",
                "version": "PD-0.7",
                "label": "device-1",
                "location": "1",
                "serial_number": "PWH12GVF",
                "status": "OK"
              },
              {
                "configuration": "Configured",
                "version": "PD-0.7",
                "label": "device-2",
                "location": "2",
                "serial_number": "PWH12GWF",
                "status": "OK"
              },
              {
                "configuration": "Configured",
                "version": "PD-0.7",
                "label": "device-3",
                "location": "3",
                "serial_number": "PWH12GXF",
                "status": "CRITICAL"
              }
            ],
            "status": "OK"
        }
    ]
}

Kai Stian Olstad

unread,
Jan 8, 2019, 5:26:58 PM1/8/19
to ansible...@googlegroups.com
This makes it easier to understand structure of the variable, you should get the critical ones with this

{{ hw_health.storage['Controller on System Board']['logical_drives'][0]['physical_drives'] | selectattr('status', 'equalto', 'CRITICAL') | list }}


--
Kai Stian Olstad


Rama

unread,
Jan 8, 2019, 5:48:17 PM1/8/19
to Ansible Project
Hi,

Enclosed the var and made PB as below and resulted FAILED output.

PB:
---
- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
   - name: ILO Facts
     hpilo_facts:
      host: 10.20.64.51
      login: XXXXXX
      password: XXXXXX
     delegate_to: localhost

   - debug:
      var: "{{ hw_health.storage['Controller on System Board']['logical_drives'][0]['physical_drives']  | selectattr('status', 'equalto', 'CRITICAL') | list }}"


ERROR:
TASK [debug] **************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "Unexpected failure during module execution."}

Kai Stian Olstad

unread,
Jan 9, 2019, 10:56:13 AM1/9/19
to ansible...@googlegroups.com
On Tuesday, 8 January 2019 23:48:16 CET Rama wrote:
> Hi,
>
> Enclosed the var and made PB as below and resulted FAILED output.
>
> PB:
> ---
> - hosts: localhost
> connection: local
> gather_facts: no
> tasks:
> - name: ILO Facts
> hpilo_facts:
> host: 10.20.64.51
> login: XXXXXX
> password: XXXXXX
> delegate_to: localhost
>
> - debug:
> var: "{{ hw_health.storage['Controller on System
> Board']['logical_drives'][0]['physical_drives'] | selectattr('status',
> 'equalto', 'CRITICAL') | list }}"

var: takes a variable not the content, change your var: to msg:


--
Kai Stian Olstad


Ramaswamy Rajavarapu

unread,
Jan 9, 2019, 12:11:07 PM1/9/19
to ansible...@googlegroups.com
ah, again the same error with msg rather than var
TASK [debug] **************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "Unexpected failure during module execution."}
--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3008905.SmHLR2bRnS%40x1.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Jan 9, 2019, 12:31:47 PM1/9/19
to ansible...@googlegroups.com
On Wednesday, 9 January 2019 18:10:43 CET Ramaswamy Rajavarapu wrote:
> ah, again the same error with msg rather than var
> TASK [debug]
> **************************************************************************************************************************************
> fatal: [localhost]: FAILED! => {"msg": "Unexpected failure during module
> execution."}

Then you have some other typos in you debug task somewhere.


--
Kai Stian Olstad


Ramaswamy Rajavarapu

unread,
Jan 19, 2019, 4:36:59 AM1/19/19
to ansible...@googlegroups.com
Hi,

Thank you for your assistance. In this case selectattr match works that as I expected.

--
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 post to this group, send email to ansible...@googlegroups.com.

Rama

unread,
Jan 21, 2019, 11:38:10 AM1/21/19
to Ansible Project
Hi,

I am facing another issue. In the first scenario we used {{hw_health.storage['Controller on System Board']['logical_drives'][0]['physical_drives'] | selectattr('status', 'match', 'Failed') | list}} where only one physical_drives.  Some servers having two "physical_drives" sections. The status: Failed may happen either sections.   Can you please help me here how can we get the Failed list from either. 


Output :
"storage": {
                "Controller on System Board": {
                    "logical_drives": [
                        {
                            "physical_drives": [

                                {
                                    "configuration": "Configured",
                                    "version": "PD-0.7",
                                    "label": "device-1",
                                    "location": "1",
                                    "serial_number": "PWH12GVF",
                                    "status": "OK"
                                },
                                {
                                    "configuration": "Configured",
                                    "version": "PD-0.7",
                                    "label": "device-2",
                                    "location": "2",
                                    "serial_number": "PWH12GWF",
                                    "status": "OK"
                                }
                            ],                          
                        },
                        {
                            "physical_drives": [

                                {
                                    "configuration": "Configured",
                                    "version": "PD-0.7",
                                    "label": "device-3",
                                    "location": "3",
                                    "serial_number": "PWH12GVA",

                                    "status": "OK"
                                },
                                {
                                    "configuration": "Configured",
                                    "version": "PD-0.7",
                                    "label": "device-4",
                                    "location": "4",
                                    "serial_number": "PWH12GWB",
                                    "status": "OK"
                                }
                            ],                           
                        }
                    ],             
                }
            }

On Saturday, January 19, 2019 at 3:06:59 PM UTC+5:30, Rama wrote:
Hi,

Thank you for your assistance. In this case selectattr match works that as I expected.

On Wed, Jan 9, 2019 at 11:01 PM Kai Stian Olstad <ansible-project+list@olstad.com> wrote:
On Wednesday, 9 January 2019 18:10:43 CET Ramaswamy Rajavarapu wrote:
> ah, again the same error with msg rather than var
> TASK [debug]
> **************************************************************************************************************************************
> fatal: [localhost]: FAILED! => {"msg": "Unexpected failure during module
> execution."}

Then you have some other typos in you debug task somewhere.


--
Kai Stian Olstad


--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Kai Stian Olstad

unread,
Jan 22, 2019, 3:24:56 PM1/22/19
to ansible...@googlegroups.com
On Monday, 21 January 2019 17:38:10 CET Rama wrote:
> Hi,
>
> I am facing another issue. In the first scenario we used *{{hw_health.storage['Controller
> on System Board']['logical_drives'][0]['physical_drives'] |
> selectattr('status', 'match', 'Failed') | list}} *where only one
> physical_drives. Some servers having two "physical_drives" sections. The
> status: Failed may happen either sections. Can you please help me here
> how can we get the Failed list from either.

It's not pretty and it's probably a better way to this, but this should work

"{{ hw_health.storage['Controller on System Board'].logical_drives | map(attribute='physical_drives') | list | flatten | selectattr('status', 'match', 'OK') | list }}"


--
Kai Stian Olstad


Reply all
Reply to author
Forward
0 new messages