title: How to conditionally replace text in a list with regex_replace?
body: <p>I have a variable in my playbook that's derived from a list. In some instances this variable contains a "-" to separate two values. For example,</p>
  
  <pre><code>Numbers:
    - 2211
    - 2211-2212
  </code></pre>
  
  <p>When this is the case I would like to replace the "-" with a "_" based on a conditional: If the number is 4 characters long, do this. Else, replace the "-" with a " _ " and do that. </p>
  
  <p>I've already tried to fiddle around with jinja2 ans regex in my playbooks but so far no luck. Here's what I tried,</p>
  
  <pre><code>number: {% if length(item) == 4 %} {{ item | regex_replace("^(.*)$", "Number_\1") | string }} {% else %} {{ item | regex_replace("^(.*)$", "Number_\1") |replace("-", "_") | string }}
  
  </code></pre>
  
  <p>The result that I would like to have,</p>
  
  <pre><code>Number is four characters long:
      number: Number_2211
  Number is more then 4 characters long:
      number: Number_2211_2212 
  </code></pre>
  
  <p>Some of the Error messages I have received are,</p>
  
  <pre><code>ERROR! Syntax Error while loading YAML.
    did not find expected key
  
  </code></pre>
  
  <pre><code>ERROR! Syntax Error while loading YAML.
    found character that cannot start any token
  </code></pre>
  
  <p>Is there a way to achieve this within the Ansible playbook?
  Thanks in advance!</p>