# With\_items /pass var - string.

**URL:** <https://forum.ansible.com/t/with-items-pass-var-string/33074>\
**Category:** Ansible Project\
**Created:** [June 2, 2020, 3:55pm UTC](https://forum.ansible.com/t/with-items-pass-var-string/33074 "2020-06-02T15:55:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_McGill](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@Adam\_McGill](https://forum.ansible.com/u/Adam_McGill)\
**Post date:** [June 2, 2020, 3:55pm UTC](https://forum.ansible.com/t/with-items-pass-var-string/33074/1 "2020-06-02T15:55:28Z")

</div>

I would like to take the value of the var and pass it into with\_items and I am running into an issue / error, how can i pass just the value into with\_items. If i hardcode and put the value with\_items: {{ small2019 }} in there are no issues. Using include\_vars / file which has the data, want to pass value into with items to get results.

Result:

---

<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:** [June 2, 2020, 7:06pm UTC](https://forum.ansible.com/t/with-items-pass-var-string/33074/2 "2020-06-02T19:06:00Z")

</div>

> I would like to take the value of the var and pass it into with\_items and I am running into an issue / error, how can i  
> pass just the value into with\_items. If i hardcode and put the value with\_items: {{ small2019 }} in there are no  
> issues. Using include\_vars / file which has the data, want to pass value into with items to get results.

Why did you wrap the variable in single quotes? This makes it a literal string instead of the contents of the variable ...

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

---

<div class="post-metadata">

**Author:** ![Adam\_McGill](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@Adam\_McGill](https://forum.ansible.com/u/Adam_McGill)\
**Post date:** [June 2, 2020, 7:36pm UTC](https://forum.ansible.com/t/with-items-pass-var-string/33074/3 "2020-06-02T19:36:04Z")

</div>

If i remove the single quotes, in the debug i see it pull the information correctly from the include\_vars. If I use the same with\_items: “{{ vm\_config }}” in the task i get this.

“msg”: "The task includes an option with an undefined variable. The error was: ‘ansible.utils.unsafe\_proxy.AnsibleUnsafeText object’ has no attribute ‘InstallMedia’\n\nThe error appears to be in

---

<div class="post-metadata">

**Author:** ![Adam\_McGill](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@Adam\_McGill](https://forum.ansible.com/u/Adam_McGill)\
**Post date:** [June 2, 2020, 7:39pm UTC](https://forum.ansible.com/t/with-items-pass-var-string/33074/4 "2020-06-02T19:39:41Z")

</div>

Basically i have a include\_vars file that has all my configs, want with\_items to only pull back the required config.

example / include\_vars:

---

<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:** [June 3, 2020, 1:09pm UTC](https://forum.ansible.com/t/with-items-pass-var-string/33074/5 "2020-06-03T13:09:08Z")

</div>

All the looping constructs (with\_items etc) assume you have some data  
structure that is iterable, ie. a list, a dict, etc.

> From your vars file it seems (guessing here) that you want to iterate

over multiple independent variables.  
If that is the case, I would suggest restructuring those multiple vars  
to one iterable var.  
This will give you a lot more flexibility elsewhere in your deployment as well.

For example:

&nbsp;&nbsp;&nbsp;&nbsp;allsizes:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- config: "DC2019"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: small2019  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InstallMedia:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Source: "\\\\someshare\\"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SourceVHDX: "Virtual Hard Disks\\DC2019.vhdx"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Destination: "C:\\ISOs\\MDT\\DC2019\\"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- config: "DC2019"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: large2019  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InstallMedia:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Source: "\\\\someshare\\"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SourceVHDX: "Virtual Hard Disks\\DC2019.vhdx"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Destination: "C:\\ISOs\\MDT\\DC2019\\"

(Note that apart from the name (which used to be your variable name),  
the dicts are exactly the same - I guess this is just example data and  
in reality these actually differ)

If this is now what you want, then explain better what you do want. I  
would help if you provide real world examples.

---

<div class="post-metadata">

**Author:** ![Adam\_McGill](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@Adam\_McGill](https://forum.ansible.com/u/Adam_McGill)\
**Post date:** [June 5, 2020, 7:22pm UTC](https://forum.ansible.com/t/with-items-pass-var-string/33074/6 "2020-06-05T19:22:23Z")

</div>

Thanks for the reply Dick.

Basically i want to have a include file/vars that will hold configuration items for each config. Using with\_items would like to call out to that config file/include vars and pull only the data associated to its name. The information i provided, yes was an example and I think your reply of the restructuring could help. Based on using that restructure using with\_items is going to loop in all the information in the config/include file. So i could narrow down my list in within the with\_items with a passed extra vars variable ( {{ allsizes.{{ vm\_config }} }} ? Going to test out / restructure this afternoon , sorry for the delay in response, appreciate the feedback.

Adam
