Removing specific characters from a string on ansible
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 adding regex_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
Solution 1:
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.