jinja2 conditional help

Good day, all.

I’ve created a simple package update playbook, which also creates a report of updated packages:

  • hosts: mytesthosts
    gather_facts: false
    become: yes

tasks:

  • name: install all updates
    yum:
    name: ‘*’
    update_cache: yes
    state: latest

  • name: List updated packages
    shell: rpm -qa --last | grep “$(date +%a\ %d\ %b\ %Y)” |cut -f 1 -d " "|sort -n
    register: result
    tags:

  • lsupdates

  • debug:
    msg: “{{ result.stdout_lines }}”
    tags:

  • prupdates

  • local_action:
    module: copy
    content: |
    {% set t1 = now().strftime(“%m/%d/%Y”) %}
    {% for host in ansible_play_hosts %}
    {{ host }}:

The following packages were updated on {{ t1 }}

{{ hostvars[host][‘result’][‘stdout’] }}

{% endfor -%}
dest: “/home/dyioulos/updates.txt”
run_once: yes
tags:

  • report

This does almost exactly what I want it to. The report looks like this:

test1:

The following packages were updated on 11/27/2023

alternatives-1.24-1.el9.x86_64
apr-util-1.6.1-23.el9.x86_64
apr-util-bdb-1.6.1-23.el9.x86_64

I haven’t tested this but does something like:

{{ hostvars[host][‘result’][‘stdout’]|default(“None”, true) }}

Give the output you need?

cf:

Will, worked perfectly - thanks!