another geek another geek - 3 months ago 121
YAML Question

Ansible lineinfile escape special characters

I'm trying to replace a line in a remote file using

ansible
.

It's a
php
file, containing:

'cache' => true,


I tried a lot of different solutions but no one worked. I feel I'm just too close but I can't do it. Could you please help me?
This is what I've done:

lineinfile: "dest=/home/ubuntu/www/2.5.0/app/config/preprod/app.php regexp=^\'cache\'=> line=\'cache\' => {{ var }}"


var
is either true or false, is a parameter set via the command line. The output shows me a line is added. but this not what I want. Thank you in advance.

Answer

Avoid using param=value in complex cases, use dict style instead, it's a lot easier to read:

 - lineinfile:
     dest: "/home/ubuntu/www/2.5.0/app/config/preprod/app.php"
     regexp: "^'cache' =>"
     line: "'cache' => {{ var | string | lower }},"