title: How to add multiple lines in Ansible
body: <p>I am trying to add multiple lines to a config file after a match and using the lineinfile but I am finding that the resulting lines are reversed. Here is my playbook:</p>
  
  <pre><code>  - name: Line test
      lineinfile:
        path: /home/vagrant/abcd
        insertafter: '### AFTER THIS LINE'
        line: "{{ item }}"
        state: present
      with_items:
        - '# This is line 1'
        - '# This is line 2'
        - '# This is line 3' 
  </code></pre>
  
  <p>and here is the result:</p>
  
  <pre><code>### AFTER THIS LINE
  # This is line 3
  # This is line 2
  # This is line 1
  </code></pre>
  
  <p>My desired result should be:</p>
  
  <pre><code>### AFTER THIS LINE
  # This is line 1
  # This is line 2
  # This is line 3
  </code></pre>
  
  <p>I understand that the reversal is due to the loop but how can one overcome this without reversing the order of the input? I know there is the blockinfile which puts the block of text as-is but that adds the "ANSIBLE MANAGED BLOCK" markers which I don't want.</p>
  
  <p>Thanks.</p>