Exclude json object which doesn't have value in ansible

  1. how to exclude the below from json queries or is it possible to filter in debug . I tried but couldn’t sorted out .

https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#formatting-data-yaml-and-json

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 }}”

Try this

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

Thank you so much Vlado… Worked fine .