2

I'm trying to develop a playbook were I have the following variable.

disk_vars:
 - { Unit: C, Size: 50 }
 - { Unit: D, Size: 50 }

With the variables defined on the playbook there is no problem but when I try to use a texarea survey on Ansible Tower I cannot manage to parse them as list of dictionaries.

I tried adding to the survey the following two lines which are already on yaml format.

 - { Unit: C, Size: 50 }
 - { Unit: D, Size: 50 }

And on my vars section I use test_var: "{{ test_var1.split('\n') }} which converts the output into a two line string. Without the split is just a single line string.

I could make my playbook work with a simple dictionary like

dict1: {{ Unit: C, Size: 50 }} 

but I'm having issues parsing it as well.

EDIT

Changing it to the following as suggested by mdaniels works.

- set_fact:
   test_var: "{{ test_var1 | from_yaml }}"
- name: test
 debug: msg=" hostname is {{ item.Unit }} and {{ item.Size }}"
 with_items:
  - "{{ test_var }}"   

I'm trying to figure a way to clear-up the data input as asking users to respect the format is not a very good idea.

tried changing the input date to the following but I could not figure out how to format that into a list of dictionaries.

disk_vars:
 Unit: C, Size: 50
 Unit: D, Size: 50

I tried with the following piece of code

- set_fact:
db_list: >-
  {{ test_var1.split("\n") | select | 
     map("regex_replace", "^", "- {") | 
     map("regex_replace", "$", "}") | 
     join("\n") }}

But is putting it all on a single line.

"db_list": "- {dbid: 1, dbname: abc\ndbid: 2, dbname: xyz} "

I have tried to play with it but could not manage to make it work.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Santi
  • 35
  • 1
  • 7

2 Answers2

3

I believe you were very close; instead of "{{ test_var1.split('\n') }}" I believe you can just feed it to the from_yaml filter:

- set_fact:
     test_var1: '{{ test_var1 | from_yaml }}'
  # this is just to simulate the **str** that you will receive from the textarea
  vars:
     test_var1: "- { Unit: C, Size: 50 }\n- { Unit: D, Size: 50 }\n"
- debug:
     msg: and now test_var1[0].Unit is {{ test_var1[0].Unit }}
mdaniel
  • 31,240
  • 5
  • 55
  • 58
  • It worked by changing the set_fact to ` new_var: '{{ test_var1 | from_yaml }}'` and then using new_var. I did found another similar question you replied but I tried to achieve this first as I was no able to make it work like in the following question. I would like to remove the yaml part as well from the textarea https://stackoverflow.com/questions/54814628/how-to-convert-a-string-with-key-value-pairs-into-a-list-of-dictionaries – Santi Jan 16 '20 at 17:07
0

I faced a similar dilemma, i.e. that I was bound to the survey format(s) available, and I was forced to use mdaniels suggested solution above with sending the data as text and then later parse it from YAML . Problem was however that controlling the format of the input (i.e. a YAML-string inside the text) would probably cause a lot of headache/errors, just like you describe.
Maybe you really need to use the Survey, but in my case I was more interested of calling the Job Template using the Tower REST API. For some reason I thought I then had to have a survey with all parameters defined. But it turned out I was wrong, when having a survey I was not able to provide dictionaries as input data (in the extra_vars). However, when removing the Survey, and also (not sure if required or not) enabling "Extra Variables -> prompt on launch", then things started to work!! Now I can provide lists / dictionaries as input to my Templates when calling them using REST API POST calls, see example below:

{
    "extra_vars": {
        "p_db_name": "MYSUPERDB",
        "p_appl_id": "MYD32",
        "p_admin_user": "myadmin",
        "p_admin_pass": "mysuperpwd",
        "p_db_state": "present",
        "p_tablespaces": [
        { 
            "name": "tomahawk",
            "size": "10M",
            "bigfile": true,
            "autoextend": true,
            "next": "1M",
            "maxsize": "20M",
            "content": "permanent",
            "state": "present"
        }
        ],
        "p_users": [
        {                        
            "schema": "myschema",
            "password": "Mypass123456#",
            "default_tablespace": "tomahawk",
            "state": "present",
            "grants": "'create session', 'create any table'"
        }
        ]
    }
}
stiffo
  • 1
  • 1