How to convert a text area to list of disks for vmware_guest

Hello guys:

There’s something I’ve been struggling with for some hours without knowing exactly how to solve it.
Through AWX, I want users to enter the size of disks for a virtual machine using a text area in a survey, like this:

100
20
130
230

When I assign this input to a variable, I see that ansible converts them to a list, like this:

mydisks: |-
100
20
130
230

By using vmware_guest module, I’d like to convert it to something like…

vmware_guest:
disk:

  • size_gb: 100
    type: None

  • size_gb: 20
    type: None

  • size_gb: 130
    type: None

  • size_gb: 230
    type: None

So I found some references on Internet that look like this jinja2 expressions:

vmware_guest:
disk: >-
[{% for size in mydisks.split() %}‘{ size_gb: {{ size }}, type: None }’, {% endfor %}]

However, when I run the playbook I got an ugly error like this:

TASK [Create virtual machine] ********************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: ‘str’ object has no attribute ‘get’
fatal: [localhost]: FAILED! => {“changed”: false, “module_stderr”: “Traceback (most recent call last):\n File "/home/arengifo/.ansible/tmp/ansible-tmp-1561181905.46-145468152234880/AnsiballZ_vmware_guest.py", line 114, in \n _ansiballz_main()\n File "/home/arengifo/.ansible/tmp/ansible-tmp-1561181905.46-145468152234880/AnsiballZ_vmware_guest.py", line 106, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File "/home/arengifo/.ansible/tmp/ansible-tmp-1561181905.46-145468152234880/AnsiballZ_vmware_guest.py", line 49, in invoke_module\n imp.load_module(‘main’, mod, module, MOD_DESC)\n File "/tmp/ansible_vmware_guest_payload_iGarsa/main.py", line 2630, in \n File "/tmp/ansible_vmware_guest_payload_iGarsa/main.py", line 2619, in main\n File "/tmp/ansible_vmware_guest_payload_iGarsa/main.py", line 2135, in deploy_vm\n File "/tmp/ansible_vmware_guest_payload_iGarsa/main.py", line 1811, in configure_disks\nAttributeError: ‘str’ object has no attribute ‘get’\n”, “module_stdout”: “”, “msg”: “MODULE FAILURE\nSee stdout/stderr for the exact error”, “rc”: 1}

Would someone, please, help me with this issue? I’m sure my jinja2 expression it’s wrong, but I can figure out what’s the right way to do it.

Thanks in advance

When I assign this input to a variable, I see that ansible converts them to
a list, like this:

mydisks: |-
   100
   20
   130
   230

By using vmware_guest module, I'd like to convert it to something like...

vmware_guest:
   disk:
     - size_gb: 100
       type: None
     - size_gb: 20
       type: None
     - size_gb: 130
       type: None
     - size_gb: 230
       type: None

So I found some references on Internet that look like this jinja2
expressions:

vmware_guest:
   disk: >-
     [{% for size in mydisks.split() %}'{ size_gb: {{ size }}, type: None
}', {% endfor %}]

However, when I run the playbook I got an ugly error like this:

<snip />

Would someone, please, help me with this issue? I'm sure my jinja2
expression it's wrong, but I can figure out what's the right way to do it.

With set_fact you can do this

- set_fact:
    disk: "{{ disk | default() + [{ 'size_gb': item, 'type': 'None' }] }}"
  with_items: '{{ mydisks.split() }}'

Thank you so much, Kai. This worked perfectly!