jinja2 get list of attributes

此生再无相见时 提交于 2019-12-12 01:26:12

问题


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?


回答1:


The following one-liner worked for me:

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


来源:https://stackoverflow.com/questions/35387845/jinja2-get-list-of-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!