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
From: dulhaver via Ansible Project <[email protected]>
Sent: Thursday, July 27, 2023 10:12 AM
To: [email protected]
Subject: [EXTERNAL] Re: [ansible-project] edit string in a multi-line file
curious actually how this can be achieved with sophisticated regex and the
lineinfile module
On 07/27/2023 4:08 PM CEST dulhaver via Ansible Project
<[email protected]> wrote:
would this do what you want?
- name: edit a line in a file
ansible.builtin.command:
chdir: /home/username/
cmd: 'sed -i "s/This is a file/This is a new file/" somefile'
On 07/27/2023 3:13 PM CEST Kathy L <[email protected]> wrote:
I am trying to edit a string in a multi-line file. For instance, if I had this
string:
This is a file and I am editing it
I want to add the string "new" in front of file if it does not exist already.
I've tried lineinfile, but the issue is that I don't know what else is on the
line. There could be more data after "This is a file and I am editing it" that
I don't want to change. What is the best way to do this?
--
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
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/9c8e2dcf-ab7d-4984-ac0a-83e818699135n%40googlegroups.com<https://urldefense.com/v3/__https:/groups.google.com/d/msgid/ansible-project/9c8e2dcf-ab7d-4984-ac0a-83e818699135n*40googlegroups.com?utm_medium=email&utm_source=footer__;JQ!!Fto3Xw!tYOdaIFvdvYbKLokgekHttcymnQP4yZuFPFFwUo8BdbOla4k9ptwGgdMsP6D79ZGnkdm4UorZ8I4N7f4plM-tSB_ILQ5oNw$>.
--
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
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/910865649.311112.1690466922457%40office.mailbox.org<https://urldefense.com/v3/__https:/groups.google.com/d/msgid/ansible-project/910865649.311112.1690466922457*40office.mailbox.org?utm_medium=email&utm_source=footer__;JQ!!Fto3Xw!tYOdaIFvdvYbKLokgekHttcymnQP4yZuFPFFwUo8BdbOla4k9ptwGgdMsP6D79ZGnkdm4UorZ8I4N7f4plM-tSB_Ac8tdeU$>.
--
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
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/354691782.311307.1690467142008%40office.mailbox.org<https://urldefense.com/v3/__https:/groups.google.com/d/msgid/ansible-project/354691782.311307.1690467142008*40office.mailbox.org?utm_medium=email&utm_source=footer__;JQ!!Fto3Xw!tYOdaIFvdvYbKLokgekHttcymnQP4yZuFPFFwUo8BdbOla4k9ptwGgdMsP6D79ZGnkdm4UorZ8I4N7f4plM-tSB_o2kenpE$>.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/PH0PR10MB55935C5AAE6D641F829FEDEBF001A%40PH0PR10MB5593.namprd10.prod.outlook.com.