Templating error
Closed this issue · 2 comments
I may be using the role the wrong way. But i get a templating error while generating the nginx.conf.
My Config
- hosts: webserver_true
collections:
- nginxinc.nginx_core
tasks:
- name: Install NGINX
ansible.builtin.include_role:
name: nginx
- name: Install NGINX Config
ansible.builtin.include_role:
name: nginx_config
vars:
nginx_config_main_template_enable: true
nginx_config_main_template:
config:
main:
user:
username: nginx
group: nginx
worker_processes: auto
error_log:
file: /var/log/nginx/error.log
level: notice
pid: /var/run/nginx.pid
events:
worker_connections: 1024
http:
include:
- /etc/nginx/mime.types
- /etc/nginx/conf.d/*.conf
Result
Produces following error:
sequence item 1: expected str instance, int found. sequence item 1: expected str instance, int found
Expected behavior
Should render fine.
Your environment
- Nginx Collection 0.7.1
- nginxinc.nginx_config 0.5.2
- Ansible 2.14.0
- Jinja2 3.0.3
- Target deployment platform: Ubuntu 22.04
Additional Information
After digging into this i found that this is caused by the following snippet:
https://github.com/nginxinc/ansible-role-nginx-config/blob/01c9e60f5e2359aed8b12b0b26bf9dfb90adac21/templates/core.j2#L111-L113
I am no jinja user but it seems that the variable substitution has to return string and not int. Switching line 112 the following version fixes the issue:
worker_connections {{ events['worker_connections']|string }};
That is quite an odd bug indeed. By all accounts, that Jinja2 code works as intended. Otherwise the code here https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/default/converge.yml#L76 would fail tests. You are using a relatively recent version of Jinja2 and there have not been any major changes in that piece of code between 0.5.2 and the latest commits on main. What version of Python are you using? (It's probably not relevant but maybe it is?)
The tl;dr is that I have no idea why this is happening or how to even reproduce it locally? I'll try to do some digging and see if I can find out what's going on here.
You might be running into a bug with Jinja2 3.0.2 (https://bugs.launchpad.net/ubuntu/+source/jinja2/+bug/1986509). Try updating to the latest version and let me know if that does the trick! I'll update the docs to bump the minimum required version.
I've updated the minimum version of Jinja2 required for the role to run to 3.1.0 as of c60afdc/release 0.6.0 😄