title: lineinfile module of ansible with delegate_to localhost doesn't write all data to localhost, it writes only 1 random entry on localhost
body: <p>I have 3 remote VMs and 1 ansible node.</p>
  
  <p>I am getting the hostname of some VMs by running hostname command on those remote VMs through ansible shell module and registering that output in <strong>hostname_output</strong> variable.</p>
  
  <p>Then I want to add those VM's IP <strong>(collected using gather_facts: True, {{ ansible_default_ipv4.address }} )</strong> with their hostname and append it to a file <strong>temp_hostname</strong> on localhost, hence I am delegating the task to localhost.</p>
  
  <p>But the issue is, when I see on console, the lineinfile module says that line has been added when the module executed for each node and delegated to localhost, but when I check the file on the localhost, only 1 entry is shown on localhost instead of 3.</p>
  
  <pre><code>---
  - name: get hostnames of dynamically created VMs
    hosts: all
    remote_user: "{{ remote_user }}"
    gather_facts: True
  
    tasks:
     - name: save hostname in variable, as this command is executed remotely, and we want the value on the ansible node
       shell: hostname
       register: hostname_output
  
     - name: writing hostname_output in ansible node in file on ansible node
       lineinfile:
        line: "{{ ansible_default_ipv4.address }} {{ hostname_output.stdout }}"
        dest: temp_hostname
        state: present
       delegate_to: 127.0.0.1
  </code></pre>
  
  <p>I even tried with copy module as specified in <a href="https://stackoverflow.com/questions/38363385/ansible-writing-output-from-multiple-task-to-a-single-file">Ansible writing output from multiple task to a single file</a> , but that also gave same result i.e 1 entry only.</p>
  
  <pre><code>---
  - name: get hostnames of dynamically created VMs
    hosts: all
    remote_user: "{{ remote_user }}"
    gather_facts: True
  
    tasks:
     - name: save hostname in variable, as this command is executed remotely, and we want the value on the ansible node
       shell: hostname
       register: hostname_output
  
     - name: writing hostname_output in ansible node in file on ansible node
       copy:
          content: "{{ ansible_default_ipv4.address }} {{ hostname_output.stdout }}"
          dest: /volume200gb/sushil/test/code_hostname/temp_hostname
       delegate_to: 127.0.0.1
  </code></pre>
  
  <p>Finally when I used shell module with redirection operator, it worked as I wanted i.e 3 entries in file on localhost. </p>
  
  <pre><code>---
  - name: get hostnames of dynamically created VMs
    hosts: all
    remote_user: "{{ remote_user }}"
    gather_facts: True
  
    tasks:
     - name: save hostname in variable, as this command is executed remotely, and we want the value on the ansible node
       shell: hostname
       register: hostname_output
  
     - name: writing hostname_output in ansible node in file on ansible node
       shell: echo -e "{{ ansible_default_ipv4.address }} {{ hostname_output.stdout }}" &gt;&gt; temp_hostname
       delegate_to: 127.0.0.1
  </code></pre>
  
  <p>I am calling this ansible-playbook get_hostname.yml using command:</p>
  
  <p><strong>ansible-playbook -i  hosts get_hostname.yml --ssh-extra-args="-o StrictHostKeyChecking=no" --extra-vars "remote_user=cloud-user" -vvv</strong></p>
  
  <p>My hosts file is:</p>
  
  <pre><code>10.194.11.86 private_key_file=/root/.ssh/id_rsa
  10.194.11.87 private_key_file=/root/.ssh/id_rsa
  10.194.11.88 private_key_file=/root/.ssh/id_rsa
  </code></pre>
  
  <p>I am using ansible 2.1.0.0</p>
  
  <p>I am using default ansible.cfg only, no modications</p>
  
  <p>My question is why lineinfile and copy module didn't work? Did I miss anything or wrote something wrongly</p>