Nginx configuration using Ansible

Hi! im actually new to ansible and i need to change some multiple nginx files. since it kind of have a structure of server { } and location{} etc. also think there are more blocks of server{} and location{} in the etc in this same file. and how can i add any code specifically to the location inside the config file?

1 Like

Hi,

Not sure I quite understand what you’re asking; if I get you straight, you’d like to know how to inject your configuration (nginx directives and such) directly into existing Nginx configuration files. Is that it ?

If so, you can use lineinfile module, which will let you add, remove or modify lines or patterns in existing files.

Another suggestion would be to template those files out. Here is a vhost template example from geerlingguy ansible-role-nginx role, though you’d probably like to use your own templates for specific configuration. Also not only vhosts, but daemon conf, http block, streams and upstreams, …

Hope it helps !

2 Likes

Hi @Akila,
I think I did what you mean using a template: https://github.com/epfl-si/ansible-collection-rhel/blob/main/roles/nginx/templates/etc/nginx/nginx.conf

Hope that helped!

I highly recommend just templating the file out versus using lineinfile or blockinfile. You should understand every section of nginx and what you are configuring. Everytime I try to go down the path of partial config manipulation it ends up having some corner case I didn’t envision and back fires.

4 Likes

I recommend that you check out jinja2 templates and then create one or two with some variables.

Hi @Akila! It looks like the post might be solved - could you check to see if any of the responses above worked for you?

If so, it would be super helpful if you could click the :heavy_check_mark: on their post to accept the solution - it recognises the input of others, helps our volunteers find new issues to answer, and keeps the forum nice and tidy.

Thanks!
(this is template reply, do feel free to reply if I’ve misunderstood the situation!)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.