With_items /pass var - string.

41 views
Skip to first unread message

Adam McGill

unread,
Jun 2, 2020, 11:55:28 AM6/2/20
to Ansible Project
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:
************************************
- debug:
    var:  "{{ 'vm_config' }}"

okL  [(servername)] => {
  "vm_config": "small2019"
}

Using with_items with value, errors
********************************************
- name: Create VMs
  win_hyperv_guest:
    vmname: "{{ vm_name }}"
    hvhostname: "{{ hv_fullname }}"
    installmedia_source: "{{ item.InstallMedia.Source }}"
    installmedia_destination: "{{ item.InstallMedia.Destination }}"
    importvmoptions_path: "{{ item.ImportVMOptions.Path }}"
    importvmoptions_destination: "{{ item.ImportVMOptions.Destination }}"
    systemosdisksize: "{{ item.SystemOSDiskSize | default (omit) }}"
    vmmemory: "{{ item.VMMemory }}"
    vmprocessor: "{{ item.VMProcessor }}"
    vmnetworkadaptervlan: "{{ item.VMNetworkAdapterVlan }}"
    vmnetworkadapter: "{{ item.VMNetworkAdapter | default (omit) }}"
    additionaldisks: "{{ item.AdditionalDisks | default (omit) }}"
    failoverclusteradd: "{{ item.FailoverClusterAdd | default (omit) }}"
    removedvdrive: "{{ item.RemoveDVDrive | default (omit) }}"    
    bootordergen2networklast: "{{ item.BootOrderGen2NetworkLast | default (omit) }}"
    state: present
  with_items: "{{ 'vm_config' }}"
  register: new_vms

Using with_items with hardcoded - works
********************************************
- name: Create VMs
  win_hyperv_guest:
    vmname: "{{ vm_name }}"
    hvhostname: "{{ hv_fullname }}"
    installmedia_source: "{{ item.InstallMedia.Source }}"
    installmedia_destination: "{{ item.InstallMedia.Destination }}"
    importvmoptions_path: "{{ item.ImportVMOptions.Path }}"
    importvmoptions_destination: "{{ item.ImportVMOptions.Destination }}"
    systemosdisksize: "{{ item.SystemOSDiskSize | default (omit) }}"
    vmmemory: "{{ item.VMMemory }}"
    vmprocessor: "{{ item.VMProcessor }}"
    vmnetworkadaptervlan: "{{ item.VMNetworkAdapterVlan }}"
    vmnetworkadapter: "{{ item.VMNetworkAdapter | default (omit) }}"
    additionaldisks: "{{ item.AdditionalDisks | default (omit) }}"
    failoverclusteradd: "{{ item.FailoverClusterAdd | default (omit) }}"
    removedvdrive: "{{ item.RemoveDVDrive | default (omit) }}"    
    bootordergen2networklast: "{{ item.BootOrderGen2NetworkLast | default (omit) }}"
    state: present
  with_items: "{{ small2019 }}"
  register: new_vms

Stefan Hornburg (Racke)

unread,
Jun 2, 2020, 3:06:39 PM6/2/20
to ansible...@googlegroups.com
On 6/2/20 5:55 PM, Adam McGill wrote:
> 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
Racke
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/03761546-e9df-4a13-85f8-5a56331e8a53%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/03761546-e9df-4a13-85f8-5a56331e8a53%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Adam McGill

unread,
Jun 2, 2020, 3:36:05 PM6/2/20
to Ansible Project
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

Adam McGill

unread,
Jun 2, 2020, 3:39:41 PM6/2/20
to Ansible Project
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:
---
small2019:
  - config: "DC2019"   
    InstallMedia:
      Source: "\\\\someshare\\"
      SourceVHDX: "Virtual Hard Disks\\DC2019.vhdx"
      Destination: "C:\\ISOs\\MDT\\DC2019\\"

large2019:
  - config: "DC2019"   
    InstallMedia:
      Source: "\\\\someshare\\"
      SourceVHDX: "Virtual Hard Disks\\DC2019.vhdx"
      Destination: "C:\\ISOs\\MDT\\DC2019\\"

Dick Visser

unread,
Jun 3, 2020, 9:09:43 AM6/3/20
to ansible...@googlegroups.com
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:

allsizes:
- config: "DC2019"
name: small2019
InstallMedia:
Source: "\\\\someshare\\"
SourceVHDX: "Virtual Hard Disks\\DC2019.vhdx"
Destination: "C:\\ISOs\\MDT\\DC2019\\"
- config: "DC2019"
name: large2019
InstallMedia:
Source: "\\\\someshare\\"
SourceVHDX: "Virtual Hard Disks\\DC2019.vhdx"
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.
>>> > ansible...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
>>> > To view this discussion on the web visit
>>> > https://groups.google.com/d/msgid/ansible-project/03761546-e9df-4a13-85f8-5a56331e8a53%40googlegroups.com
>>> > <https://groups.google.com/d/msgid/ansible-project/03761546-e9df-4a13-85f8-5a56331e8a53%40googlegroups.com?utm_medium=email&utm_source=footer>.
>>>
>>>
>>> --
>>> Ecommerce and Linux consulting + Perl and web application programming.
>>> Debian and Sympa administration. Provisioning with Ansible.
>>>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1d6bc36e-a1f0-47f8-a146-b8b8f1c6cd91%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Adam McGill

unread,
Jun 5, 2020, 3:22:23 PM6/5/20
to Ansible Project
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
>>> > To view this discussion on the web visit
>>> > https://groups.google.com/d/msgid/ansible-project/03761546-e9df-4a13-85f8-5a56331e8a53%40googlegroups.com
>>> > <https://groups.google.com/d/msgid/ansible-project/03761546-e9df-4a13-85f8-5a56331e8a53%40googlegroups.com?utm_medium=email&utm_source=footer>.
>>>
>>>
>>> --
>>> Ecommerce and Linux consulting + Perl and web application programming.
>>> Debian and Sympa administration. Provisioning with Ansible.
>>>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages