Removing specific characters from a string on ansible
5,219
You are thinking way too difficult. regex_search()
returns an array, and you want the first item.
- set_fact:
test_result: "{{ htmlres.content | regex_search('http://website([0-9]+)', '\\1') | first }}"
The blank at the start is the result of your assignment of test_result
:
test_result: " {{ htmlres.content ...
^-- here
Just remove it.
Related videos on Youtube
Author by
Ress
Updated on September 18, 2022Comments
-
Ress over 1 year
I have the following
- set_fact: test_result: " {{ htmlres.content | regex_search('http://website([0-9]+)', '\\1') }}"
Using debug, this returns the following
" '[01]'"
Wanting only the number, I did some experimenting using the
replace()
function and was able to strip the[ ]
by adding the following:- set_fact: test_result: " {{ htmlres.content | regex_search('http://website([0-9]+)', '\\1') | replace('[','') | replace(']','') }}"
My problem now is that the output is now
" '01'
and I can't seem to remove the'
or the whitespace.Adding
| trim
to the end for some reason doesn't remove the whitespaces, and addingregex_search('\'','')
also doesn't seem to escape the character and work.Am I missing something?
Here's the output from the debug after the first removal:
"msg": [ " '01'", ...
Thanks
-
Ress over 2 yearsWow I didn't even notice the first space. Also thanks for clarifying that it returns an array. It's matching correctly now! Thanks!