How to modify a specific line after a match, Ansible

Viewed 45

I tried to modify a specific line which is between "<Directory /var/www/>" and ""and after "Options Indexes FollowSymLinks". The line is : "AllowOverride None" and i am interested in changing it to "AllowOverride All". I used marker_begin and marker_end to delete lines between them and then insert the new one again but it did not work. I used regex to find the block in file. Also I used match for "Options Indexes FollowSymLinks" and tried to delete the line "AllowOverride None" and use again match and insertafter the line "AllowOverride All", but I could not do anything. here is what I did with blockinfile:

- name: delete override line
  blockinfile:
    path: /etc/apache2/apache2.conf
    marker_begin: "<Directory /var/www/>"
    marker_end: "</Directory>"
    block: ""

and this is what I expected to see:

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

It seems, Ansible does not search lines which start with the sign "<".

1 Answers

a sample to use correctly marker_begin and marker_end

- name: delete override line
  blockinfile:
    path: /etc/apache2/apache2.conf
    marker_begin: "<Directory /var/www/>"
    marker_end: "</Directory>"
    marker: "{mark}"
    block: ""

this task removes all block (including markers) and FYI, marker doesnt use regex

becareful with this method, for the marker_end, it takes the last marker_end..so if you have more same marker_end, you have to choose another solution

Related