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:

To reproduce

Steps to reproduce the behavior:

- name: Install NGINX
    name: nginxinc.nginx
    nginx_install_from: os_repository
    nginx_start: false
    nginx_type: opensource
    nginx_state: present

- name: Configure NGINX
    name: nginxinc.nginx_config
    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
      template_file: nginx.conf.j2
      conf_file_name: nginx.conf
      conf_file_location: /etc/nginx/
      worker_connections: 1024
        - "/etc/nginx/sites-enabled/*.conf"
      user: www-data
      worker_processes: auto
      pid: /var/run/

      # http configuration section
      http_enable: true
            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.


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:

          enable: true
            number: 32
            size: 4k
          comp_level: 1
            - '"msie6"'
          http_version: 1.1
          min_length: 20
            - expired
            - text/html
          vary: false
fatal: []: 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:

      enable: true
        number: 32
        size: 16k
      comp_level: 6
      vary: true
      - any
      - "*"


    "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 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