#Using an Ansible variable in a Jinja statement?

1 messages · Page 1 of 1 (latest)

sly shoal
#

I am trying to pass an Ansible variable to an if/then/else statement in Jinja, but it is not recognizing it as a variable. I know the "stacked mustaches" are not supported, but I also tried the square brackets [ ] and that didn't work either. Can anyone clue me in on a way to do this? This works when I am put an actual value in there for {{ protection_type }}, but not when I try to use a variable.

Basically, I'm just checking to see if a particular string exists within the "volume_comment" variable:

  ansible.builtin.set_fact:
    protection_present:
      "{%- if volume_comment | regex_search('.*{{ protection_type }}.*') -%}
        false
      {%- else -%}
        true
      {%- endif -%}"
wispy rover
#

hi @sly shoal
can you try something like below?

    - name: Check to see if the volume comment contains a note that no mirrors should be present
      ansible.builtin.set_fact:
        protection_present: >
          {% if volume_comment | regex_search('.*{{ protection_type }}.*') %}
            false
          {% else %}
            true
          {% endif %}
sly shoal
cold quail
#

From your example I don't see why regex is even necessary. If you only search for a substring try this:

#

{%- if protection_type in volume_comment -%}