Filter the output with jmespath

I have a playbook which install windows updates with win_updates.
The output is registred. See example output below.
I want to filter that output with the result title,kb where the installed value is equal to true

I was trying something like this:

updates.*.[?installed==“true”]{name:title,installed:installed}
But that is not working.

{
“updates”: {
“adab657d-7124-4d67-98a0-78bb2d9db5bf”: {
“id”: “adab657d-7124-4d67-98a0-78bb2d9db5bf”,
“title”: “2023-09 Cumulative Update for .NET Framework 3.5, 4.8 and 4.8.1 for Microsoft server operating system version 21H2 for x64 (KB5030186)”,
“categories”: [
“Microsoft Server operating system-21H2”,
“Security Updates”
],
“kb”: [
“5030186”
],
“installed”: true
},
“91722aa2-d45a-4a79-b59a-2365f590b8c6”: {
“id”: “91722aa2-d45a-4a79-b59a-2365f590b8c6”,
“title”: “Windows Malicious Software Removal Tool x64 - v5.117 (KB890830)”,
“categories”: [
“Update Rollups”,
“Windows Server 2016”,
“Windows Server 2019”,
“Windows Server, version 1903 and later”
],
“kb”: [
“890830”
],
“installed”: true
},
“f1e3c23a-45c2-47ae-a9ca-7139585571d5”: {
“id”: “f1e3c23a-45c2-47ae-a9ca-7139585571d5”,
“title”: “2023-09 Cumulative Update for Microsoft server operating system version 21H2 for x64-based Systems (KB5030216)”,
“categories”: [
“Security Updates”
],
“kb”: [
“5030216”
],
“installed”: true
},
“696baba6-f559-4e24-b099-3170606e6096”: {
“id”: “696baba6-f559-4e24-b099-3170606e6096”,
“title”: “Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.397.1155.0)”,
“categories”: [
“Definition Updates”,
“Microsoft Defender Antivirus”
],
“kb”: [
“2267602”
],
“installed”: true
}
},
“failed_update_count”: 0,
“found_update_count”: 4,
“changed”: true,
“reboot_required”: true,
“installed_update_count”: 4,
“filtered_updates”: {},
“_ansible_no_log”: false,
“failed_when_result”: false
}

2 Likes

Can you just use the selectattr filter and do selectattr('installed', 'equalto', true) and then get what you want out of that data structure?

To your original question maybe something like this->

    - name: Filter and display updates
      debug:
        msg: "{{ your_json_variable | json_query(jmespath_query) }}"
      vars:
        jmespath_query: "result.updates.* | [?installed == `true`] | [].[title, kb]"
5 Likes

thnx @IPvSean
that jmespath filter is working.

I try later this week also your suggestion about the selectattr

2 Likes

The JMESPath Terminal makes working out expressions easier, I cleaned up your JSON (removing smart quotes):

{
  "updates": {
    "adab657d-7124-4d67-98a0-78bb2d9db5bf": {
      "id": "adab657d-7124-4d67-98a0-78bb2d9db5bf",
      "title": "2023-09 Cumulative Update for .NET Framework 3.5",
      "4.8 and 4.8.1 for Microsoft server operating system version 21H2 for x64 (KB5030186)": "",
      "categories": [
        "Microsoft Server operating system-21H2",
        "Security Updates"
      ],
      "kb": [
        "5030186"
      ],
      "installed": true
    },
    "91722aa2-d45a-4a79-b59a-2365f590b8c6": {
      "id": "91722aa2-d45a-4a79-b59a-2365f590b8c6",
      "title": "Windows Malicious Software Removal Tool x64 - v5.117 (KB890830)",
      "categories": [
        "Update Rollups",
        "Windows Server 2016",
        "Windows Server 2019",
        "Windows Server, version 1903 and later"
      ],
      "kb": [
        "890830"
      ],
      "installed": true
    },
    "f1e3c23a-45c2-47ae-a9ca-7139585571d5": {
      "id": "f1e3c23a-45c2-47ae-a9ca-7139585571d5",
      "title": "2023-09 Cumulative Update for Microsoft server operating system version 21H2 for x64-based Systems (KB5030216)",
      "categories": [
        "Security Updates"
      ],
      "kb": [
        "5030216"
      ],
      "installed": true
    },
    "696baba6-f559-4e24-b099-3170606e6096": {
      "id": "696baba6-f559-4e24-b099-3170606e6096",
      "title": "Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.397.1155.0)",
      "categories": [
        "Definition Updates",
        "Microsoft Defender Antivirus"
      ],
      "kb": [
        "2267602"
      ],
      "installed": true
    }
  },
  "failed_update_count": 0,
  "found_update_count": 4,
  "changed": true,
  "reboot_required": true,
  "installed_update_count": 4,
  "filtered_updates": {},
  "_ansible_no_log": false,
  "failed_when_result": false
}

Saved it as a file locally and then opened it using JMESPath Terminal and this expression:

updates.*|[?installed==`true`].{name:title,kb:kb[0]}

Returns:

[
  {
    "name": "2023-09 Cumulative Update for .NET Framework 3.5",
    "kb": "5030186"
  },
  {
    "name": "Windows Malicious Software Removal Tool x64 - v5.117 (KB890830)",
    "kb": "890830"
  },
  {
    "name": "2023-09 Cumulative Update for Microsoft server operating system version 21H2 for x64-based Systems (KB5030216)",
    "kb": "5030216"
  },
  {
    "name": "Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.397.1155.0)",
    "kb": "2267602"
  }
]

It took me a while to realise that the back ticks @IPvSean used above are essential — booleans and numbers have to use backticks.

Hi @daemenseth! It looks like the post might be solved - could you check to see if the response by @chris worked for you?

If so, it would be super helpful if you could click the :heavy_check_mark: on their post to accept the solution - it helps users find solutions (solved topics have a higher search priority), recognises the input of the people who help you, helps our volunteers find new issues to answer, and keeps the forum nice and tidy. It’s just a nice way to give back, and only takes a moment :slight_smile:

Thanks!
(this is template reply, do feel free to reply if I’ve misunderstood the situation!)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.