title: extract digits from string with ansible
body: <p>I'm trying to extract the digits from a string.</p>
  
  <p>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.</p>
  
  <p><strong>Some examples:</strong></p>
  
  <pre><code>host1 -&gt; 1
  ho12st -&gt; 12
  host -&gt; 1
  host2 -&gt; 2
  host2test -&gt; 2
  host02 -&gt; 02
  host34 -&gt; 34
  </code></pre>
  
  <p>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.</p>
  
  <p>Something like the following works in python, but not ansible.</p>
  
  <pre><code>int(''.join(filter(str.isdigit, {{ ansible_hostname }} )))
  </code></pre>
  
  <p>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.</p>
  
  <p>**** Answer ****</p>
  
  <p>I ended up using this to get my required result:</p>
  
  <pre><code>- set_fact:
          env_id: '{{ server_name | regex_search(qry) }}'
        vars:
          qry: '[0-9]'
      - set_fact:
          env_id: "1"
        when: env_id == ""
  </code></pre>