diff --git a/changelogs/fragments/lineinfile-backrefs-syntax-example.yaml b/changelogs/fragments/lineinfile-backrefs-syntax-example.yaml new file mode 100644 index 00000000000..9409b24d30c --- /dev/null +++ b/changelogs/fragments/lineinfile-backrefs-syntax-example.yaml @@ -0,0 +1,2 @@ +bugfixes: + - lineinfile - add example of using alternative backrefs syntax (https://github.com/ansible/ansible/issues/42794) diff --git a/lib/ansible/modules/lineinfile.py b/lib/ansible/modules/lineinfile.py index 2ffa8d74f1b..5860f4d6e73 100644 --- a/lib/ansible/modules/lineinfile.py +++ b/lib/ansible/modules/lineinfile.py @@ -200,6 +200,14 @@ EXAMPLES = r''' regexp: '^%ADMIN ALL=' line: '%ADMIN ALL=(ALL) NOPASSWD: ALL' validate: /usr/sbin/visudo -cf %s + +# See https://docs.python.org/3/library/re.html for further details on syntax +- name: Use backrefs with alternative group syntax to avoid conflicts with variable values + lineinfile: + path: /tmp/config + regexp: ^(host=).* + line: \g<1>{{ hostname }} + backrefs: yes ''' import os