Search code examples
ruby-on-railsrubynginxansiblepassenger

Adding line to file in ansible task


I need add in ansible task to /etc/nginx/nginx.conf two lines(for deploy):

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/bin/passenger_free_ruby;

This line I add to end of file("include /etc/nginx/passenger.conf;"):

- name: Include passenger in nginx.conf
  become: yes
  lineinfile: dest=/etc/nginx/nginx.conf regexp="^\s*# include \/etc\/nginx\/passenger.conf;" line="        include /etc/nginx/passenger.conf;"

But this lines must be in html{...} block, not in the end of file. How I can do it?


Solution

  • There's insertafter option for lineinfile module.

    You can do something like this:

    - lineinfile:
        dest: /etc/nginx/nginx.conf
        line: "        include /etc/nginx/passenger.conf;"
        insertafter: '^\s*include /etc/nginx/conf-components.d/\*.conf;\s*$'
        # insertafter: '^http {$'
    

    To insert your include just after *.conf includes, or just after http { opening tag.

    It is not useful if you have many identical blocks. But for unique markers it's good enough.