title: Replace Specific String in YAML File using Ansible
body: <p>I have a web application that uses a YAML file for configuration. This is an except from the file:</p>
  
  <pre><code>---
  settings:
    domain: 127.0.0.1
  </code></pre>
  
  <p>I have an Ansible playbook that uses the lineinfile module to replace the IP address in the YAML file above with the server's public IP address. </p>
  
  <pre><code>- name: Discovering Public Internet Protocol Address
    ipify_facts:
    register: public_ip
  
  - name: Configuring Application with discovered Public IP
    lineinfile:
      dest: /application/path/settings.yml
      regexp: '^(.*)domain: (.*)$'
      line: 'domain: {{ ipify_public_ip }}'
  </code></pre>
  
  <p>This finds and replaces the 127.0.0.1 IP with the public server's IP but it breaks the YAML indentation as follows:</p>
  
  <pre><code>---
  settings:
  domain: 54.12.33.3
  </code></pre>
  
  <p>Problem: "domain" is moved to the same line with "settings" and my ruby app fails to run migrations because it identifies a YAML syntax error.</p>
  
  <p>I do not mind replacing lineinfile with another module, but I'd like to keep it if possible. I've been struggling with this for hours and will appreciate any assistance. </p>