0

I'd like to generate and deploy nginx reverse proxy configurations via ansible.
In the template, there is a part for intercepting errors (404, 501, etc), this should not be active for ALL the deploys. I'd like to be able to toggle the inclusion of these lines in the template on the condition of the value of the variable {{customError}}.

Is this possible? Most solutions I've seen are working with different templates and deciding in the task based on a condition which template to use. I very much don't like the idea of multiple templates, since this would not force me to create way to many templates for all the possibilities.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
maxap
  • 90
  • 5
  • `Is this possible?` => Of course ! I would not see the point of using templates if it was not !. Now, what did you try to achieve this and what is the exact problem you are facing ? A good place to start is the [jinja2 template designer documentation](https://jinja.palletsprojects.com/en/2.10.x/templates/), more specifically the [`if` statement](https://jinja.palletsprojects.com/en/2.10.x/templates/#if) – Zeitounator Jan 10 '20 at 21:32
  • I tried googling till now, was sure this must be possible (see answers, but was not sure how) – maxap Jan 12 '20 at 08:59

2 Answers2

1

It sure is, if for you a deploy means a different Ansible host then, you can assign your variable customError at host level, in the inventory.

Here is an example to start with:

The inventory, that show the definition of the variable customError:

all:
  hosts:
    server1.nginx.tld:
      customError: true
    server2:.nginx.tld:
      customError: false

The playbook, containing a basic deploy of a template file:

---
- hosts: all

  tasks: 
    - name: Copy default Nginx config
      template:
        src: default.j2
        dest: /etc/nginx/sites-enabled/default

The template, in Jinja format, using a basic condition, based on the host variable, default.j2:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;


{% if customError %}
        error_page 404 /custom_404.html;
        location = /custom_404.html {
                root /usr/share/nginx/html;
                internal;
        }
{% endif %}
}

Executing it:

$ ansible-playbook playbook.yml --inventory=inventory.yaml

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [server2.nginx.tld]
ok: [server1.nginx.tld]

TASK [Copy default Nginx config] ***********************************************
changed: [server1.nginx.tld]
changed: [server2.nginx.tld]

PLAY RECAP *********************************************************************
server1.nginx.tld          : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
server2.nginx.tld          : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

And to verify what this generated, a little more ad-hoc Ansible:

$ ansible -a 'cat /etc/nginx/sites-enabled/default' all --inventory=inventory.yaml

server1.nginx.tld | CHANGED | rc=0 >>
server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;


        error_page 404 /custom_404.html;
        location = /custom_404.html {
                root /usr/share/nginx/html;
                internal;
        }
}

server2.nginx.tld | CHANGED | rc=0 >>
server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;


}
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
1

You can use Jinja conditions that generate dynamic content, like:

{% if group == 'dev' %}
404 = something
501 = something
{% else %}
404 = something different
501 = something different
{% endif %}

https://jinja.palletsprojects.com/en/2.10.x/

chenchuk
  • 5,324
  • 4
  • 34
  • 41