--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9c8e2dcf-ab7d-4984-ac0a-83e818699135n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/910865649.311112.1690466922457%40office.mailbox.org.
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/yZgBhYf9B7Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/910865649.311112.1690466922457%40office.mailbox.org.
What you want to use is backrefs.
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html
An Example from the document:
# NOTE: Yaml requires escaping backslashes in double quotes but not in single quotes
- name: Ensure the JBoss memory settings are exactly as needed
ansible.builtin.lineinfile:
path: /opt/jboss-as/bin/standalone.conf
regexp: '^(.*)Xms(\d+)m(.*)$'
line: '\1Xms${xms}m\3'
backrefs: yes
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/354691782.311307.1690467142008%40office.mailbox.org.
- name: Test lineinfile
hosts: localhost
gather_facts: false
tasks:
- name: Create a file to work on
ansible.builtin.copy:
content: |
First line of a file
Second line of the same file
This is a file and I am editing it. Extra Stuff!
Fourth line of this file
dest: /tmp/textfile.txt
- name: Show the file content
ansible.builtin.debug:
msg: "{{ lookup('file', '/tmp/textfile.txt') }}"
- name: Insert "new" before "file" in the 3rd line
ansible.builtin.lineinfile:
path: /tmp/textfile.txt
regexp: '(This is a )(?!>new)(file and I am editing it\..*)'
backrefs: true
line: '\1new \2'
- name: Show the file content a 2nd time
ansible.builtin.debug:
msg: "{{ lookup('file', '/tmp/textfile.txt') }}"
- name: Second identical lineinfile should not make changes
ansible.builtin.lineinfile:
path: /tmp/textfile.txt
regexp: '(This is a )(?!>new)(file and I am editing it\..*)'
backrefs: true
line: '\1new \2'
- name: Show the file content a 3nd time
ansible.builtin.debug:
msg: "{{ lookup('file', '/tmp/textfile.txt') }}"
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9c8e2dcf-ab7d-4984-ac0a-83e818699135n%40googlegroups.com.
-- Todd