# working with file

**URL:** <https://forum.ansible.com/t/working-with-file/33451>\
**Category:** Ansible Project\
**Created:** [July 21, 2020, 2:08pm UTC](https://forum.ansible.com/t/working-with-file/33451 "2020-07-21T14:08:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicolas\_stevenin](https://avatars.discourse-cdn.com/v4/letter/n/50afbb/32.png) [@nicolas\_stevenin](https://forum.ansible.com/u/nicolas_stevenin)\
**Post date:** [July 21, 2020, 2:08pm UTC](https://forum.ansible.com/t/working-with-file/33451/1 "2020-07-21T14:08:02Z")

</div>

Hi,

I have a file like : [1,2,3,4,5]  
and I want to use loop for each number in this file on the playbook (I want to use the file, not write keys in the playbook). Could you help me please ?

The playbook: (and i need to loop on “variable\_file”)

- name: Gather info about all ESXi Host in given Cluster  
vmware\_host\_service\_info:  
validate\_certs: no  
hostname: xxxxx  
username: user  
password: password  
cluster\_name: “{{variable\_file}}”

Thanks for your help

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 21, 2020, 4:56pm UTC](https://forum.ansible.com/t/working-with-file/33451/2 "2020-07-21T16:56:50Z")

</div>

> Hi,
> 
> I have a file like : [1,2,3,4,5]

‘Like’ is ambiguous. What is like what?  
Is the filename like 12345? Is the content a json list of integers? Please try to be precise.

> and I want to use loop for each number in this file on the playbook (I want to use the file, not write keys in the playbook).

No idea again what ‘writing keys in the playbook’ means. So again please be precise.

> Could you help me please ?
> 
> The playbook: (and i need to loop on “variable\_file”)
> 
> - name: Gather info about all ESXi Host in given Cluster  
> vmware\_host\_service\_info:  
> validate\_certs: no  
> hostname: xxxxx  
> username: user  
> password: password  
> cluster\_name: “{{variable\_file}}”

A wild guess, if you want to read the contents of a file ? Maybe this is what you want:

[https://docs.ansible.com/ansible/latest/plugins/lookup/lines.html](https://docs.ansible.com/ansible/latest/plugins/lookup/lines.html)

---

<div class="post-metadata">

**Author:** ![nicolas\_stevenin](https://avatars.discourse-cdn.com/v4/letter/n/50afbb/32.png) [@nicolas\_stevenin](https://forum.ansible.com/u/nicolas_stevenin)\
**Post date:** [July 22, 2020, 8:18am UTC](https://forum.ansible.com/t/working-with-file/33451/3 "2020-07-22T08:18:22Z")

</div>

Hi,

I have a json file-\> cluster\_name.json :  
[“cluster1”,“cluster2”,“cluster3”,“cluster4”]

And I want to iterate on every cluster name to obtain some informations so I use the module vmware\_host\_service\_info.  
My playbook is → vmware.yml :

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [July 22, 2020, 9:00am UTC](https://forum.ansible.com/t/working-with-file/33451/4 "2020-07-22T09:00:36Z")

</div>

> Hi,
> 
> I have a json file-\> cluster\_name.json :  
> ["cluster1","cluster2","cluster3","cluster4"]
> 
> And I want to iterate on every cluster name to obtain some informations so I use the module vmware\_host\_service\_info.  
> My playbook is -\> vmware.yml :  
> ---  
> - name: test  
> hosts: localhost  
> vars\_files:  
> - cluster\_name.json  
> tasks:  
> - name: Gather info about all ESXi Host in given Cluster  
> vmware\_host\_service\_info:  
> validate\_certs: no  
> hostname: hostname  
> username: user  
> password: password  
> cluster\_name: "{{cluster}}" \<-- HERE I WANT TO ITERATE ON EVERY CLUSTER\_NAME IN cluster\_name.json  
> delegate\_to: localhost

Replace vars\_files with a lookup:

vars:  
&nbsp;&nbsp;&nbsp;&nbsp;cluster\_names: "{{ lookup( 'file', 'cluster\_name.json' ) }}"

This automatically converts the file contents from JSON to a list.

You can now loop over that:

tasks:  
&nbsp;&nbsp;&nbsp;- name: Gather info about all ESXi Host in given Cluster  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vmware\_host\_service\_info:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate\_certs: no  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostname: hostname  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: user  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password: password  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cluster\_name: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ cluster\_names }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![nicolas\_stevenin](https://avatars.discourse-cdn.com/v4/letter/n/50afbb/32.png) [@nicolas\_stevenin](https://forum.ansible.com/u/nicolas_stevenin)\
**Post date:** [July 22, 2020, 12:56pm UTC](https://forum.ansible.com/t/working-with-file/33451/5 "2020-07-22T12:56:27Z")

</div>

OK ! It works, thanks 🙂
