patternMinor
Ansible Jinja template if statement
Viewed 0 times
jinjatemplateansiblestatement
Problem
This is a snippet from my Ansible jinja template which populates an environment specific template.
This fails with a pretty generic error message:
I'm almost certain it's to do with escaping the quotes here but can't for the life of me work out what I'm doing wrong, any ideas?
docker_compose_mq:
docker_compose_profiles: "string"
{% if "{{ risk_docker_compose_mq }}" == "string" %}
{% "{{ risk_docker_compose_profiles: "string1" }}" %}
{% endif %}This fails with a pretty generic error message:
"Syntax Error while loading YAML.\n\n\nThe error appears to have been in 'True': line 26, column 2, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\n(could not open file to display line)"}I'm almost certain it's to do with escaping the quotes here but can't for the life of me work out what I'm doing wrong, any ideas?
Solution
The syntax failures are caused by the presence of the
I only used standalone jinja2 templates, so I'm not 100% certain if this applies to Ansible jinja templates as well, but I suspect so. In the Jinja2
{{...}} expression blocks (normally used for filling the template output with the corresponding content) inside the {%...%} statement blocks.I only used standalone jinja2 templates, so I'm not 100% certain if this applies to Ansible jinja templates as well, but I suspect so. In the Jinja2
{%...%} statement blocks variables are referenced directly (and variable assignments are done in {% set ...%} statements), so what you're after may be along these lines:{% set docker_compose_mq = %}
{% set docker_compose_profiles = "string" %}
{% if risk_docker_compose_mq == "string" %}
{% set risk_docker_compose_profiles = "string1" %}
{% endif %}Code Snippets
{% set docker_compose_mq = <string-passed from Jenkins> %}
{% set docker_compose_profiles = "string" %}
{% if risk_docker_compose_mq == "string" %}
{% set risk_docker_compose_profiles = "string1" %}
{% endif %}Context
StackExchange DevOps Q#4138, answer score: 3
Revisions (0)
No revisions yet.