Note particularly that loop: expression. It’s a double-quoted string, so the what looks like a single-quoted “\” and “n” in those splits are actually literal new-line characters by the time split is invoked. Similarly, that '\\1\n\\2' in the regex_replace becomes:
a single backslash
the digit 1
a literal new-line
another single backslash
the digit 2
by the time regex_replace is invoked.
Anyway, maybe that will be of some help. Good luck.
In the short term, regular expressions are a good approach, but in the long term, tricky implementations can easily be a technical debt.
As a next step, I personally recommend you to consider changing the original text file format to make it easier to process in Ansible
For example, CSV, YAML, etc…
We currently have it working with a yaml file, but the people in charge want a text file, or json (which I haven’t tested yet) as the data is ingested to another system. I’ll try the regex example this morning.
(item=[‘username: bob password: secretpassword’] ) => {
msg: “username: "username: bob password: secretpassword", password: "username: bob password is secretpassword"”
Sorry to sound picky, @kathyl , but it’s a lot easier to fix, or suggest fixes, to code we can copy-n-paste than it is to try to pull together snippets from prior posts. Also, if you can bracket your code between tripple-backtick lines that would help a lot, too. Otherwise your yaml lists come out as markdown bullet points.
Ah yes, I see the problem now. You’ve been bitten by “smart quotes”. This:
content: “{{ lookup(‘file’, ‘data.txt’) }}”
should be:
content: "{{ lookup('file', 'data.txt') }}"
Note that both the single- and double-quotes need fixing. It’s also an issue in your debug task’s msg: line. Somehow, all the quotes in your loop: expression seem to be intact.
It is very hard to see the difference in the default size here on the forum. I have to [ctrl] + [shift] + [+] a couple of times before I can see it.
Of the two you just posted, the single-quote is "U+2019", not "U+0027". The double-quote is okay: "U+0022".
(Note: discourse is converting double-quotes outside of back-ticks to “those other marks” – but only when they are in pairs. I had not noticed that before. Probably works okay in text. Horrible for un-back-tick quoted code.)