I'm trying to extract the digits from a string.
For example, if I have the string 'host2' I want only the '2' returned. If there is no digit in the string, I want '1' returned.
Some examples:
host1 -> 1
ho12st -> 12
host -> 1
host2 -> 2
host2test -> 2
host02 -> 02
host34 -> 34
I don't know if I should use a module for this, or use internal workings and just set a fact. I'm a bit of a newb.
Something like the following works in python, but not ansible.
int(''.join(filter(str.isdigit, {{ ansible_hostname }} )))
This ends up interpreted on the shell so I can't use the {{ ansible_hostname }} variable. It also doesn't default to '1' if no digits are found.
**** Answer ****
I ended up using this to get my required result:
- set_fact:
env_id: '{{ server_name | regex_search(qry) }}'
vars:
qry: '[0-9]'
- set_fact:
env_id: "1"
when: env_id == ""