Playbook Variable and Jinja 2 -- Namespace Problem?

ansible-playbook [core 2.11.5]
python version = 3.9.7 (default, Sep 3 2021, 12:45:31) [Clang 12.0.0 (clang-1200.0.32.29)]
jinja version = 2.11.3

Hi,

I’m trying to write a play that will connect to a managed Windows node, gather a list of Windows Updates that are available to be installed on it, and output that list to stdout. Below is what I’ve cobbled together, but it doesn’t fully work. While the list is dumped as expected to the local file “win_updates.log”, it is not also copied to stdout. The error I get is “msg”: “The task includes an option with an undefined variable. The error was: list object has no element”.

It appears that the ‘k’ variable in the for loop in the Jinja 2 routine is unset, presumably because the “update_results” variable in the play isn’t available to it. Can you help me understand what I’m doing wrong?

Thanks!

  • name: List all updates that might be installed
    hosts: all

tasks:

  • name: Find possible updates
    become: true
    become_method: runas
    become_user: SYSTEM
    win_updates:
    category_names: ‘*’
    state: searched
    log_path: C:/Users/MY_USER/win_updates.log
    register: update_results

  • debug:

msg: |
{% for k in update_results.updates %}
{{ update_results.updates[k].title }}
{% endfor %}

Can you post a little of update_results?

Sometimes with this error you just need to include the list reference. 0 being the first list.

update_results.0.updstes[k]

Once we get the entire update_results we can see for sure.

I thought we were on Jinja 3?

Sorry, i probably should have done that. Here’s the output from a managed node that was left behind on patches for the purpose of testing.

ok: [MY_IP] => {
“update_results.updates”: [
{
“categories”: [
“Definition Updates”,
“Microsoft Defender Antivirus”
],
“downloaded”: true,
“id”: “971038cc-9d05-4a4c-9c53-51110f6b525a”,
“installed”: true,
“kb”: [
“2267602”
],
“title”: “Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.351.755.0)”
},
{
“categories”: [
“Update Rollups”,
“Windows Server 2016”,
“Windows Server 2019”
],
“downloaded”: true,
“id”: “3af26db7-4c87-4c6e-ba44-5fa00913995e”,
“installed”: true,
“kb”: [
“890830”
],
“title”: “Windows Malicious Software Removal Tool x64 - v5.94 (KB890830)”
},
{
“categories”: [
“Security Updates”,
“Windows Server 2016”
],
“downloaded”: true,
“id”: “ff623435-c638-4c25-a907-843fa16c22b3”,
“installed”: true,
“kb”: [
“5006669”
],
“title”: “2021-10 Cumulative Update for Windows Server 2016 for x64-based Systems (KB5006669)”
},
{
“categories”: [
“Feature Packs”,
“Silverlight”
],
“downloaded”: true,
“id”: “ca3bb521-a8ea-4e26-a563-2ad6e3108b9a”,
“installed”: true,
“kb”: [
“4481252”
],
“title”: “Microsoft Silverlight (KB4481252)”
},
{
“categories”: [
“Definition Updates”,
“Microsoft Defender Antivirus”
],
“downloaded”: true,
“id”: “f6a66dbc-2311-4ee0-ad24-2392dbf48641”,
“installed”: true,
“kb”: [
“2267602”
],
“title”: “Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.351.759.0)”
},
{
“categories”: [
“Security Updates”,
“Windows Server 2016”
],
“downloaded”: true,
“id”: “73f45b23-8537-455d-83fd-840e7e93b835”,
“installed”: true,
“kb”: [
“5005698”
],
“title”: “2021-09 Servicing Stack Update for Windows Server 2016 for x64-based Systems (KB5005698)”
}
]
}

Do you recommend upgrading to Jinja2 3.x for my version of Ansible?