Ansible win_lineinfile Assistance

30 views
Skip to first unread message

Joefrey

unread,
Apr 24, 2023, 12:37:19 PM4/24/23
to Ansible Project
Good day all,

Not sure If I'm doing something wrong here or hitting a bug.

Goal is to update the below line.

  <key="Token" value="some_token" />

community.windows.win_lineinfile:
  path: '{{ some_path }}'
  regex: '(.+"Token".+").+(" />)
  line: '$1{{ some_new_token }}$2'
  backrefs: true

I end up with:

$1new_token" />

I cannot get $1 to expand when placed directly next to "{{" in line.

line: '$1 {{ some_new_token }}$2'  # notice space after $1

Results in:

  <key="Token" value=" new_token" />

Changing my regex and line as below is a workaround, but I'd like to know what I'm missing above.

regex: '(.+"Token" value=).+'
line: '$1"{{ some_new_token}}" />'

Thank you for any insight.

Atul Nasir

unread,
Aug 20, 2023, 6:10:01 PM8/20/23
to Ansible Project
- name: Update XML line with new token
  community.windows.win_lineinfile:
    path: '{{ some_path }}'
    regex: '(<key="Token" value=").+(" />)'
    line: '{{ line | regex_replace("(<key=\\"Token\\" value=).+( \\" />)", "\\1{{ some_new_token }}\\2") }}'
  vars:
    line: '{{ lookup("file", some_path) | regex_replace("(\\r|\\n)", "") }}'
Reply all
Reply to author
Forward
0 new messages