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

21 views
Skip to first unread message

Angel Rengifo Cancino

unread,
Jun 22, 2019, 1:41:19 AM6/22/19
to ansible...@googlegroups.com
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 <module>\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 <module>\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

Kai Stian Olstad

unread,
Jun 22, 2019, 3:12:46 AM6/22/19
to ansible...@googlegroups.com
<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() }}'


--
Kai Stian Olstad

Angel Rengifo Cancino

unread,
Jun 22, 2019, 3:43:15 PM6/22/19
to ansible...@googlegroups.com
Thank you so much, Kai. This worked perfectly!

--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/568505ab-e33f-ddef-675a-ccdfc2801d60%40olstad.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages