Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert line in nginx configuration file ansible inside http {}

I am writing an ansible script to deploy django in centos 7. My problem is with nginx.

I have created /etc/nginx/sites-available and /etc/nginx/sites-enabled directories and have to include sites-available in /etc/nginx/nginx.conf file at the bottom if http { } block.

How do I insert the line include /etc/nginx/sites-available/*.conf inside the http {} block in my ansible script after creating the respective directories and copying the configuration files?

The output should be

http {
    .....
    .....

    server {
        .......
    }

    include /etc/nginx/sites-available/*.conf;
}
like image 485
Eutychus Avatar asked Oct 22 '25 19:10

Eutychus


1 Answers

You can use the lineinfile module. Something like this should work:

- lineinfile: dest=/etc/nginx/nginx.conf regexp="^include /etc/nginx/sites-available/*.conf;" insertbefore="server {" line="include /etc/nginx/sites-available/*.conf;"

This will insert the line before the server block. You can play around with insertbefore and insert after and different regular expressions.

like image 196
Károly Nagy Avatar answered Oct 24 '25 20:10

Károly Nagy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!