Interview Prep/Ansible/Modules, Idempotency and Handlers

`template` fails with `"msg": "failed to validate"` and `nginx: [emerg] unknown directive "proxy_passs"`

IntermediateTroubleshootingModules, Idempotency and Handlers
Answer

Likely causes → validate: worked as designed: the rendered file failed the config test, so Ansible kept the old file in place. The bug is in the template or its variables.

Diagnose → The stderr in the result shows the validator output. Render the template locally to inspect it.

Fix → Fix the template. Keep the validation in place:

- ansible.builtin.template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf
    validate: nginx -t -c %s
  notify: Reload nginx

For included snippets that cannot be tested alone, validate the whole config in a separate command: nginx -t before the handler runs.

Prevent → Use validate on every config file that has a checker: sshd -t -f %s, visudo -cf %s, nginx -t, haproxy -c -f %s.

All Ansible questions Roadmap: Ansible

More on Modules, Idempotency and Handlers