Response Variable from shell command to set options in input_select

Hello I'm trying to figure out the right format to use a template to set input_select options from at text file thru shell_command.

my shell command is:

return_cities_list: cat /config/www/acg-tracker/options/storages/cities.list

The content of cities.list is :

- MLV
- New York City
- Paris
- Quito
- Other
- N/A

And finally, my script is:

sequence:
  - action: shell_command.return_cities_list
    data: {}
    response_variable: cities_list
  - action: input_select.set_options
    metadata: {}
    target:
      entity_id: input_select.city
    data:
      options: >
        - {{ cities_list['stdout'] }}

And here is THE option that got set in input_select.cities_list :

- - MLV - New York City - Paris - Quito - Other - N/A 

No other option, just this one. I have spent time to try to figure the right format. I'm desperate and out ideas. Thanks for you help !

That's what I would expect it to produce.

This means set the value of option to be a single list item containing the contents of cities_list['stdout'].

    data:
      options: >
        - {{ cities_list['stdout'] }}

That looks like a YAML list but it will not be handled as a list because this {{ cities_list['stdout'] }} is interpreted by the Jinja2 processor, not the YAML processor. In other words, you cannot pass a list in YAML format and expect the Jinja2 processor to handle it as a list.

I suggest you restructure the contents of the cities.list file into JSON format.

["MLV","New York City","Paris","Quito","Other","N/A"]

Then you can use the from_json filter in your template to convert it to a true list.

    data:
      options: "{{ cities_list['stdout'] | from_json }}"

EDIT

If you don't like the idea of reformatting the contents of cities.list into JSON format, you can simply use comma-separated format.

MLV,New York City,Paris,Quito,Other,N/A

Template is:

    data:
      options: "{{ cities_list['stdout'].split(',') }}"

Easy-peasy.

However, if you have no control over the format of the file's contents, then you're obligated to use slimak's suggestion which does more processing to convert it into a list.

Maybe this will work:

data:
  options: "{{ cities_list['stdout'].split('-')|select|map('trim')|list }}"

split turns the string into a list, select discards empty strings, trim removes spaces around items.

Yep, that worked.
Thank you so much !