How do you guarantee that the line exists and can be reached without compromise with Ansible LineinFile?

How do you guarantee that a certain line exists in a file and is not commented on by the available `lineinfile '

The line I want to uncomment (in .htaccess ):

 #php_flag display_errors on 

I used the following:

  - name: Make sure PHP Errors are turned on lineinfile: dest={{ www_path }}/.htaccess line="php_flag display_errors on" 
+6
source share
1 answer

Actually your example works like this:

The contents of the .htaccess file:

 #php_flag display_errors on 

Uncomplicated game:

 - name: Make sure PHP Errors are turned on lineinfile: dest: "{{ www_path }}/.htaccess" line: "php_flag display_errors on" 

The results of a game with an indecisive play with this game:

 $ cat .htaccess #php_flag display_errors on php_flag display_errors on 

If the file starts with a commented line, you will see that the second line is uncommented. To fix this, use a regex that matches the existing string and replaces it:

 - lineinfile: dest: /Users/bwhaley/tmp/file regexp: '^#php_flag display_errors' line: 'php_flag display_errors' backrefs: yes 

Please note that with backrefs: yes , if the line you want to uncomment is no longer present and is not commented on, the game will not change at all.

+8
source

All Articles