Reputation: 2266
I'm trying to extract a substring from a multine string with Ansible regex without success.
I have this ouput from an excuted command (teleport users add
):
"stdout": "Signup token has been created and is valid for 3600 seconds. Share this URL with the user:\nhttps://main-proxy:3080/web/newuser/d32ed2bc0ebb0084a381123e3eff0bfa\n\nNOTE: make sure 'main-proxy' is accessible!"
I would like to extract juste the token. Here: d32ed2bc0ebb0084a381123e3eff0bfa
.
I registered the output in a result variable, and I'm trying to extract the token without success:
- set_fact:
signup_token: '{{ result.stdout | regex_replace("^(?s)^https:\/\/.*\/(.+).*?$", "\\1") }}'
- debug: msg={{ signup_token }}
What's the right regex and syntax?
Upvotes: 1
Views: 10498
Reputation: 2266
The regex (?s).*https://.*/([^\r\n]+).*
works right as I expected.
And also yes I could have tried getting the last 32 characters from the second line of standard output but I prefere to be agnostique from token lenght if it changes, so a regex to extract the token id from output is the right way for my case now.
Upvotes: 0
Reputation: 68339
Why do you use so complex regular expression? Take 32 chars of [0-9a-f]
after /
.
- set_fact:
signup_token: "{{ mystr | regex_search(qry) }}"
vars:
qry: '(?<=\/)[a-f0-9]{32}'
Use sites like https://regex101.com/ to test your expressions.
Upvotes: 1