title: Ansible Lineinfile: Find a key and replace only the value
body: <p>I have a .properties/.conf/.xml/.csv files. The requirement is I need to match a key and replace only the value only if that key is present in that file.</p>
  
  <p>My .properties file will look like below,</p>
  
  <pre><code>name=newname
  surname=newname
  </code></pre>
  
  <p>Below is my key_value.yml file</p>
  
  <pre><code>---
  properties:
  - {  name: name, value: jon}
  - { name: surname, value: miller }
  </code></pre>
  
  <p>Below is my ansible task for doing the same.</p>
  
  <pre><code>---
  - name: Find and replace
    lineinfile:
      dest: /tmp/example/example.properties
      regexp: "{{ item.name }}"
      line: "{{ item.value }}"
      inserafter: '{{ item.name }}='
      backrefs: yes
      state: present
    with_items:
      - "{{ properties }}"
  </code></pre>
  
  <p>But this doesn't seem to be working. Its replacing the entire line. Please help!</p>