# create file using ansible variable

**URL:** <https://forum.ansible.com/t/create-file-using-ansible-variable/33693>\
**Category:** Ansible Developer\
**Tags:** windows\
**Created:** [August 28, 2020, 6:04am UTC](https://forum.ansible.com/t/create-file-using-ansible-variable/33693 "2020-08-28T06:04:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pradeep\_Ghorpade](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@Pradeep\_Ghorpade](https://forum.ansible.com/u/Pradeep_Ghorpade)\
**Post date:** [August 28, 2020, 6:04am UTC](https://forum.ansible.com/t/create-file-using-ansible-variable/33693/1 "2020-08-28T06:04:48Z")

</div>

Hello,

i am trying to create file which will be use to create partition in windows.  
I am using variables in below format.

additional\_disks:

- drive\_letter: E  
mount\_point: TMP1  
disk\_size: 7  
label: DBInstall  
block\_size: 4096k  
datastore: vsanDatastoreCMP001
- drive\_letter: F  
mount\_point: TMP2  
disk\_size: 9  
label: DBInstall1  
block\_size: 4096k  
datastore: vsanDatastoreCMP001
- drive\_letter: G  
mount\_point: TMP3  
disk\_size: 10  
label: DBInstall2  
block\_size: 4096k  
datastore: vsanDatastoreCMP001

I am using below code:

- name: Testing Iteration  
copy:  
dest: /tmp/testtemp  
content: |  
{% for var in additional\_disks %}  
select disk {{ my\_idx | int +2 }}  
create partition primary  
assign letter “{{ item.drive\_letter }}”  
format fs=ntfs label={{ item.label }} quick  
{% endfor %}  
with\_items:
- “{{ additional\_disks }}”  
loop\_control:  
index\_var: my\_idx  
label: " "  
delegate\_to: localhost

Gettin output:

select disk 4  
create partition primary  
assign letter “G”  
format fs=ntfs label=DBInstall2 quick  
select disk 4  
create partition primary  
assign letter “G”  
format fs=ntfs label=DBInstall2 quick  
select disk 4  
create partition primary  
assign letter “G”  
format fs=ntfs label=DBInstall2 quick

expected output:

select disk 2  
create partition primary  
assign letter E  
format fs=ntfs label=DBInstall quick

select disk 3  
create partition primary  
assign letter F  
format fs=ntfs label=DBInstall quick

select disk 4  
create partition primary  
assign letter G  
format fs=ntfs label=DBInstall2 quick

Can you please help to get correct outpt.

---

<div class="post-metadata">

**Author:** ![Chriztoffer\_Hansen](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@Chriztoffer\_Hansen](https://forum.ansible.com/u/Chriztoffer_Hansen)\
**Post date:** [August 28, 2020, 6:00pm UTC](https://forum.ansible.com/t/create-file-using-ansible-variable/33693/2 "2020-08-28T18:00:53Z")

</div>

Have you tried using loop instead of var\_items?

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_loops.html#iterating-over-a-simple-list](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#iterating-over-a-simple-list)

---

<div class="post-metadata">

**Author:** ![Chriztoffer\_Hansen](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@Chriztoffer\_Hansen](https://forum.ansible.com/u/Chriztoffer_Hansen)\
**Post date:** [August 28, 2020, 6:02pm UTC](https://forum.ansible.com/t/create-file-using-ansible-variable/33693/3 "2020-08-28T18:02:05Z")

</div>

> Have you tried using loop instead of var\_items?

loop instead of with\_items, of course.
