Based on this example:
- lineinfile: dest=/opt/jboss-as/bin/standalone.conf regexp='^(.*)Xms(\d+)m(.*)$' line='\1Xms${xms}m\3' backrefs=yes
from this documentation, it was tried to do a regex-replace in Ansible.
Ansible version
user@server:/home$ ansible --version
ansible 2.1.1.0
/path/to/file:
helloworld
Ansible snippets:
- lineinfile:
dest: /path/to/file
regexp='^(hello)world$'
line='\1030'
attempt 2
- lineinfile:
dest: /path/to/file
regexp='^(hello)world$'
line="\1030"
Expected outcome:
hello030
Current outcome:
\1030
Questions
- Why is the outcome
\1030
instead ofhello030
? - How to solve it?
The lineinfile module default is
backrefs: false
. Yourregexp='^(hello)world$'
matches the entire contents of file. Literal fromline='\1030'
replaces contents.backrefs: true
line:
A backref followed by numbers will not function as expected. You will need a named group instead. e.g.
\g<1>
I suppose it is because it matches whole \1030 (as 1030-th backref). Maybe try \1 030 first and you will see, if it is the reason.