Print string when "item exists in a list" in Jinja2 template
Asked Answered
P

2

10

I'm trying to populate nsswitch.conf with values that are determined from a list. The list is of string:

openldap_nsswitch:
  - group
  - hosts
  - passwd
  - shadow

If the string is in the list I want to output something in the template.

passwd:         compat {% if openldap_nsswitch contains passwd %}ldap{% endif %}

How can I write a string only if my list contains a specific element?

Propulsion answered 28/11, 2017 at 22:47 Comment(0)
I
15

Here you are:

passwd:         compat{{ ' ldap' if ('passwd' in openldap_nsswitch) else ‘’ }}
Incult answered 28/11, 2017 at 22:52 Comment(0)
L
3

Compat didn't work for me:

{% if is in('passwd', openldap_nsswitch) %} ldap {% endif %}

Also, I was doing this outside of ansible.

Leavy answered 13/11, 2019 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.