Issue with jinja2 template

60 views
Skip to first unread message

lift...@gmail.com

unread,
Aug 12, 2021, 11:38:56 AM8/12/21
to Ansible Project
I'm trying to create a report when we do our updates via yum (rhel 7) and dnf (rhel 8).  I register the output in variables called yum_rhel7 and yum _rhel8.  Here's my j2 file:

Yum/DNF Update Report

{{ now(false, '%m/%d/%Y %I:%M:%S') }}

---
{% for i in play_hosts | sort %}
{% if {{ hostvars[i]['ansible_distribution_major_version'] == '7' }} %}
{% if {{ hostvars[i]['yum_rhel7']['skipped'] is not defined }} %}
  Host: {{ i }}
  Yum Result: {{ hostvars[i]['yum_rhel7']['results'] }}
{% endif %}
{% endif %}
---
{% endfor %}

I'm getting the following error when I run the playbook:
fatal: [localhost]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: expected token ':', got '}'. String: Yum/DNF Update Report\n\n{{ now(false, '%m/%d/%Y %I:%M:%S') }}\n\n---\n{% for i in play_hosts | sort %}\n{% if {{ hostvars[i]['ansible_distribution_major_version'] == '7' }} %}\n{% if {{ hostvars[i]['yum_rhel7']['skipped'] is not defined }} %}\n  Host: {{ i }}\n  Yum Result: {{ hostvars[i]['yum_rhel7']['results'] }}\n{% endif %}\n{% endif %}\n---\n{% endfor %}\n\n"}

Any ideas on what I'm doing wrong?
Thanks,
Harry

Stefan Hornburg (Racke)

unread,
Aug 12, 2021, 11:47:59 AM8/12/21
to ansible...@googlegroups.com
On 12/08/2021 17:38, lift...@gmail.com wrote:
> I'm trying to create a report when we do our updates via yum (rhel 7) and dnf (rhel 8).  I register the output in variables called yum_rhel7 and yum _rhel8.  Here's my j2 file:
>
> Yum/DNF Update Report
>
> {{ now(false, '%m/%d/%Y %I:%M:%S') }}
>
> ---
> {% for i in play_hosts | sort %}
> {% if {{ hostvars[i]['ansible_distribution_major_version'] == '7' }} %}
> {% if {{ hostvars[i]['yum_rhel7']['skipped'] is not defined }} %}
>   Host: {{ i }}
>   Yum Result: {{ hostvars[i]['yum_rhel7']['results'] }}
> {% endif %}
> {% endif %}
> ---
> {% endfor %}

No curly braces in conditions, e.g. use

{% if hostvars[i]['ansible_distribution_major_version'] == '7' %}

Also

{% if 'skipped' not in hostvars[i]['yum_rhel7'] %}

is probably more correct.

Regards
Racke


>
> I'm getting the following error when I run the playbook:
> fatal: [localhost]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: expected token ':', got '}'. String: Yum/DNF Update Report\n\n{{ now(false, '%m/%d/%Y %I:%M:%S') }}\n\n---\n{% for i in play_hosts | sort %}\n{% if {{ hostvars[i]['ansible_distribution_major_version'] == '7' }} %}\n{% if {{ hostvars[i]['yum_rhel7']['skipped'] is not defined }} %}\n  Host: {{ i }}\n  Yum Result: {{ hostvars[i]['yum_rhel7']['results'] }}\n{% endif %}\n{% endif %}\n---\n{% endfor %}\n\n"}
>
> Any ideas on what I'm doing wrong?
> Thanks,
> Harry
>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/be4a68ec-5c3f-45d3-a8e0-82607cdfdac5n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/be4a68ec-5c3f-45d3-a8e0-82607cdfdac5n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration.

Reply all
Reply to author
Forward
0 new messages