Ansible-role-nginx-config: "AnsibleUndefinedVariable: the inline if-expression on line 9 in 'http/grpc.j2' evaluated to false and no else section was defined."}

Created on 22 Mar 2021  ·  10Comments  ·  Source: nginxinc/ansible-role-nginx-config

Describe the bug

Can't apply configuration when the http section is enabled. I get the following error:

"AnsibleUndefinedVariable: the inline if-expression on line 9 in 'http/grpc.j2' evaluated to false and no else section was defined."}

The configuration has been copied from this example:

https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/default/converge.yml

To reproduce

Steps to reproduce the behavior:

# https://github.com/nginxinc/ansible-role-nginx
- name: Install NGINX
  include_role:
    name: nginxinc.nginx
  vars:
    nginx_install_from: os_repository
    nginx_start: false
    nginx_type: opensource
    nginx_state: present

# https://github.com/nginxinc/ansible-role-nginx-config
- name: Configure NGINX
  include_role:
    name: nginxinc.nginx_config
  vars:
    nginx_config_start: false
    nginx_config_cleanup: true
    nginx_config_debug_output: true
    nginx_config_debug_tasks: true

    # top level nginx configuration
    nginx_config_main_template_enable: true
    nginx_config_main_template:
      template_file: nginx.conf.j2
      conf_file_name: nginx.conf
      conf_file_location: /etc/nginx/
      worker_connections: 1024
      http_custom_includes:
        - "/etc/nginx/sites-enabled/*.conf"
      user: www-data
      worker_processes: auto
      pid: /var/run/nginx.pid

      # http configuration section
      http_enable: true
      http_settings:
        grpc_global:
          bind:
            address: $remote_addr
            transparent: false
          buffer_size: 4k
          connect_timeout: 60s
[...]

Expected behavior

I expected the configuration to apply without errors

Your environment:

  • nginxinc.nginx_config, 0.3.3
  • nginxinc.nginx, 0.19.1
  • ansible: 2.9.6+dfsg-1
  • ubuntu 20.04

Additional context

Add any other context about the problem here.

bug

All 10 comments

Found the issue. PR coming shortly.

thank you for the quick response, I will follow up the development of this issue.

Can you pull from main and check if the latest PR fixed your issue? You might need to do some further tweaks to your template since there's been a few breaking changes since 0.3.3, but hopefully the updated examples in the molecule tests should help you get there.

I can, but I am still testing other configuration situations. I just found a similar situation with gzip:

        gzip:
          enable: true
          buffers:
            number: 32
            size: 4k
          comp_level: 1
          disable:
            - '"msie6"'
          http_version: 1.1
          min_length: 20
          proxied:
            - expired
          types:
            - text/html
          vary: false
fatal: [testing-snap05.snapview.net]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: the inline if-expression on line 27 in 'http/gzip.j2' evaluated to false and no else section was defined."}

Seems to be related to the "proxied" line.

That's a weirder error. Can you check which version of Jinja2 you are using? Tests for both the previous and this error pass just fine, and I've seen templates fail for other people when using older versions of Jinja2.

Here is the version:

python3-jinja2 2.10.1-2

I am also having trouble in applying a proxy: section under http_settings. It keeps getting ignored and nothing gets written to nginx.conf.

proxy:  # Optional -- Configure Proxy
  buffer_size: 4k  # Optional
  buffering: true  # Optional boolean
  buffers:  # Optional
    number: 8  # Required
    size: 4k  # Required
  busy_buffers_size: 8k  # Optional
  cache: false  # Optional -- Set to 'false' or specify cache zone

You might want to update to Jinja2 2.11.x and see if that addresses any of your errors. Re proxy, there's a typo under defaults/main/template.yml. You should include your proxy details under nginx_config_http_template. I'll create a separate PR to address that asap. When in doubt, the molecule tests are always a good source of truth and should always be fully functional.

I opened a separate issue for the proxy: problem above to not spam this one.

Having the same issue in regards of http/gzip.j2. Tried upgrading to Jinja2==2.11.3, but that hasn't helped either.
This is the config:

---
nginx_config_main_template:
  http_settings:
    gzip:
      enable: true
      buffers:
        number: 32
        size: 16k
      comp_level: 6
      vary: true
      proxied:
      - any
      types:
      - "*"

Error:

    "msg": "AnsibleUndefinedVariable: the inline if-expression on line 27 in 'http/gzip.j2' evaluated to false and no else section was defined.",

Line 27 includes the proxied template. Can you try running the example here https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/default/converge.yml#L62-L76 and seeing if you still run into any issues? That will help determine whether it's an issue with the role or perchance it's still a Jinja2 issue (maybe Jinja2 didn't update correctly?).

Was this page helpful?
0 / 5 - 0 ratings