问题
I'm trying to modify URL value in config file using Ansible
$CONSOLE_URI = "http://172.18.18.103/controller/";
I'm using lineinfile module, but it doesn't work, I have tried to escape $ with double back slashes, but it hasn't worked either.
- lineinfile: dest=/etc/log.conf regexp='^\\$CONSOLE_URI' line='$CONSOLE_URI=http://google.com';
回答1:
I think your regex is correct. I just tested this and the line written to the file actually has the quotes inside. This is the content of /etc/log.conf
:
'$CONSOLE_URI=http://google.com';
If that was your intention, which I do not believe, you of course need to add the quotes to the regex.
Don't ask me why the single quotes do sometimes work and sometimes not. In this case you need to use double quotes for line while for regexp the single quotes work...
- lineinfile: dest=/etc/log.conf regexp='^\\$CONSOLE_URI' line="$CONSOLE_URI=http://google.com"
Anyway, I highly suggest to use YAML syntax instead of key=value syntax. I find the latter extremely hard to read and Ansible is all about readability. In proper YAML syntax you also can trash quotes almost completely, which helps a lot if you use more complex commands which include both single and double quotes, which you then would have to escape again. So ideally (by my opinion) your task would look like this:
- lineinfile:
dest: /etc/log.conf
regexp: ^\$CONSOLE_URI
line: $CONSOLE_URI=http://google.com
In that case you only need to escape the $
once.
This is tested with Ansible 2.0.0.2.
PS: Not sure about the ;
of your line, if that was supposed to be inside the file or not.
来源:https://stackoverflow.com/questions/35717778/ansible-regex-escape-dollar-character