lundi 3 août 2020

How to check if dict value is true in Jinja2 using for loops and if statements

I am trying to automate adding and removing redirects in Nginx using Ansible.

Added this in /group_vars/all.yml:

general_redirects:
  enabled: true
  option:
    - { tournaments: true }
    - { leader: false }

This is task responsible for adding all redirects:

    - name: Add general redirects
      template:
        src: "general.redirects.j2"
        dest: /etc/nginx/conf.d/general.redirects
      when: general_redirects and general_redirects.enabled == true
      notify: restart nginx
      tags: general

But I have a problem how to add proper ifs and fors in Jinja2 template: general.redirects.j2.

Currently I have this, but when deploying, status is CHANGED and file with redirects is empty. The idea is to have commented redirects when true and uncommented when false:



I am wondering what's wrong with template. Maybe there is some better solution to automate this?

Aucun commentaire:

Enregistrer un commentaire