Exclude json object which doesn't have value in ansible

20 views
Skip to first unread message

Ramesh AR

unread,
Dec 31, 2020, 4:53:57 PM12/31/20
to Ansible Project
1. how to exclude the below from json queries or is it possible to filter in debug . I tried but couldn't sorted out . 
+++++++++++++++++++++++++++++++++++++  
for example, I don't want to be displayed the below output. The below one doesn't have any value like that I'm getting so many .. so how to rid of it from Json query . 

   {
                "accountStatus": "inactive",
                "id": "15",
                "name": "",
                "priv": ""
            }
+++++++++++++++++++++++++++++++++++++
   register: result
  - set_fact:
      user: "{{ result.configResolveClass.children|json_query(my_query) }}"
    vars:
      my_query: "[].outConfigs.children[].aaaUser.attributes[].{id: id, name: name, priv: priv}"
  - debug: msg="{{ user |join (" ") }}"
+++++++++++++++++++++++++++++++++++++
2. is there a way to align debug o/p ?

MSG:

{'id': '1', 'name': 'xxx', 'priv': 'admin'} {'id': '2', 'name': 'xxx', 'priv': 'admin'} {'id': '3', 'name': 'xxxx', 'priv': 'admin'} {'id': '4', 'name': 'xxxx', 'priv': 'admin'} {'id': '5', 'name': 'xxxx', 'priv': 'admin'} {'id': '6', 'name': 'xxxxx', 'priv': 'admin'} {'id': '7', 'name': 'xxxxx', 'priv': 'admin'} 

expected o/p:

{'id': '1', 'name': 'xxx', 'priv': 'admin'} 
{'id': '2', 'name': 'xxx', 'priv': 'admin'} 
{'id': '3', 'name': 'xxxx', 'priv': 'admin'} 
{'id': '4', 'name': 'xxxx', 'priv': 'admin'} 
{'id': '5', 'name': 'xxxx', 'priv': 'admin'} 
{'id': '6', 'name': 'xxxxx', 'priv': 'admin'} 
{'id': '7', 'name': 'xxxxx', 'priv': 'admin'}  

Thanks ,
Ramesh 



Dick Visser

unread,
Dec 31, 2020, 5:58:21 PM12/31/20
to ansible...@googlegroups.com

--
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/bba3fe2f-ebfd-4fe1-9249-08810f3ef0c1n%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Ramesh AR

unread,
Jan 4, 2021, 5:40:10 PM1/4/21
to Ansible Project
can someone help me on how to remove inactive user from the below debug output, 

TASK [debug] *************************************************************************************************************************************
ok: [host1] => {}

MSG:

- {accountStatus: active, id: '1', name: admin, priv: admin}
- {accountStatus: active, id: '2', name: VIDEO, priv: admin}
- {accountStatus: active, id: '3', name: CDVR, priv: admin}
- {accountStatus: active, id: '4', name: VIP, priv: admin}
- {accountStatus: active, id: '5', name: vie, priv: admin}
- {accountStatus: active, id: '6', name: op5, priv: admin}
- {accountStatus: active, id: '7', name: DEU, priv: admin}
- {accountStatus: active, id: '8', name: syn, priv: admin}
- {accountStatus: inactive, id: '9', name: '', priv: ''}
- {accountStatus: inactive, id: '10', name: '', priv: ''}
- {accountStatus: inactive, id: '11', name: '', priv: ''}
- {accountStatus: inactive, id: '12', name: '', priv: ''}
- {accountStatus: inactive, id: '13', name: '', priv: ''}
- {accountStatus: inactive, id: '14', name: '', priv: ''}
- {accountStatus: inactive, id: '15', name: '', priv: ''}

Playbook:

    register: result
  - set_fact:
      user: "{{ result.configResolveClass.children|json_query(my_query) }}"
    vars:
      my_query: "[].outConfigs.children[].aaaUser.attributes[].{id: id, name: name, priv: priv, accountStatus: accountStatus}"
  - debug:
      msg: "{{ user | to_yaml }}"


Vladimir Botka

unread,
Jan 4, 2021, 8:56:48 PM1/4/21
to Ramesh AR, ansible...@googlegroups.com
On Mon, 4 Jan 2021 14:40:10 -0800 (PST)
Ramesh AR <rames...@gmail.com> wrote:

> remove *inactive* user
>
> user:
> - {accountStatus: active, id: '1', name: admin, priv: admin}
> - {accountStatus: active, id: '2', name: VIDEO, priv: admin}
> - {accountStatus: active, id: '3', name: CDVR, priv: admin}
> - {accountStatus: active, id: '4', name: VIP, priv: admin}
> - {accountStatus: active, id: '5', name: vie, priv: admin}
> - {accountStatus: active, id: '6', name: op5, priv: admin}
> - {accountStatus: active, id: '7', name: DEU, priv: admin}
> - {accountStatus: active, id: '8', name: syn, priv: admin}
> - {accountStatus: inactive, id: '9', name: '', priv: ''}
> - {accountStatus: inactive, id: '10', name: '', priv: ''}
> - {accountStatus: inactive, id: '11', name: '', priv: ''}
> - {accountStatus: inactive, id: '12', name: '', priv: ''}
> - {accountStatus: inactive, id: '13', name: '', priv: ''}
> - {accountStatus: inactive, id: '14', name: '', priv: ''}
> - {accountStatus: inactive, id: '15', name: '', priv: ''}

Try this

- debug:
msg: "{{ user|
rejectattr('accountStatus', 'eq', 'inactive')|
list }}"

--
Vladimir Botka

Ramesh AR

unread,
Jan 5, 2021, 10:13:32 AM1/5/21
to Ansible Project
Thank you so much Vlado.. Worked fine . 
Reply all
Reply to author
Forward
0 new messages