# Printing items in a file using lineinfile/blockinfile

**URL:** <https://forum.ansible.com/t/printing-items-in-a-file-using-lineinfile-blockinfile/34587>\
**Category:** Ansible Project\
**Created:** [February 8, 2021, 2:53pm UTC](https://forum.ansible.com/t/printing-items-in-a-file-using-lineinfile-blockinfile/34587 "2021-02-08T14:53:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akshay\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@Akshay\_Kumar](https://forum.ansible.com/u/Akshay_Kumar)\
**Post date:** [February 8, 2021, 2:53pm UTC](https://forum.ansible.com/t/printing-items-in-a-file-using-lineinfile-blockinfile/34587/1 "2021-02-08T14:53:38Z")

</div>

Hi All,

I am currently working on a playbook where in i need to write the output of SQL statement into a file .  
I have 4 SQL statement and 4 hosts thus want the output in such a way that:  
1st host  
SQL1 output  
SQL2 Output  
SQL3 output  
SQL4 Output  
2nd host … and so on

Currently i am using this code:

- name: trying something new  
lineinfile:  
line: “{{ item }}”  
path: “{{ VlclFile }}”  
insertafter: EOF  
loop: “{{ testing }}”  
delegate\_to: localhost

which gives output in the format as i want for eg:

xxx:xxVC1:user: control\_file\_record\_keep\_time integer 365  
yyy:yyy:user: control\_file\_record\_keep\_time integer 365

but the output comes in a random order and thus statement 1 output for host 1 is followed by statement 1 for host 2 and not statement 2 of host 1.

Anyone can help me with this .

Other block of code which i tried is as followed:

#- name: Add output of task for getting content to the local

# local\_action:

# module: blockinfile

# path: “{{ VlclFile }}”

# marker: “”

# state: present

# block: “{{ hostvars[item][‘testing’]}}”

# insertafter: EOF

# run\_once: True

# with\_items:

# - “{{ play\_hosts }}”

but in this its giving me output in the form of a list and not items per se eg:

host1  
[a: value 1,b:value2]

host2  
[a: value 1,b:value2]

---

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [February 8, 2021, 5:30pm UTC](https://forum.ansible.com/t/printing-items-in-a-file-using-lineinfile-blockinfile/34587/2 "2021-02-08T17:30:04Z")

</div>

You shoul provide the output of testing . Is it variable or register . Give us some more info

//v

---

<div class="post-metadata">

**Author:** ![Akshay\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@Akshay\_Kumar](https://forum.ansible.com/u/Akshay_Kumar)\
**Post date:** [February 8, 2021, 8:16pm UTC](https://forum.ansible.com/t/printing-items-in-a-file-using-lineinfile-blockinfile/34587/3 "2021-02-08T20:16:41Z")

</div>

Hi Vinod,

Its a list variable containing SQL statement outputs for 2-3 statements .  
Thanks.
