Search code examples
jinja2salt-project

jinja2 get list of attributes


I'm using salt + jinja2 to template a context.xml file. I have the following pillar:

context:
  db_server: some_server
  resources:
    some_customer:
      name: some_name
      user: some_user
      passwd: some_passwd
    this_customer:
      name: this_name
      user: this_user
      passwd: this_passwd

I need to create a string with a list of the names for each customer. Right now I have this:

{%- set nameList = pillar['context']['resources']|list()|join(', ') %}

This gives me this list: 'some_customer, this_customer'. I'd like this list: 'some_name, this_name'.

How can I do this?


Solution

  • The following one-liner worked for me:

    {%- set nameList = pillar['context']['resources'].values()
                     |map(attribute='name')|join(', ') %}