关于Rails上的ruby:在ansible任务中向文件添加行

Adding line to file in ansible task

我需要在/etc/nginx/nginx.conf中添加ansible任务两行(用于部署):

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

我添加到文件末尾的行(" include /etc/nginx/passenger.conf;"):

1
2
3
- 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;"

但是此行必须在html {...}块中,而不是在文件末尾。
我该怎么办?


lineinfile模块具有insertafter选项。

您可以执行以下操作:

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

要在*.conf包含之后或http {开头标记之后插入包含。

如果您有许多相同的块,则没有用。但是对于唯一的标记来说就足够了。