gpt4 book ai didi

ansible - 如何获得以逗号分隔的名字列表?

转载 作者:行者123 更新时间:2023-12-05 02:52:48 26 4
gpt4 key购买 nike

这是我在 YAML 文件中的地址列表:

addresses:
person1:
firstname: Maria
lastname: Smith
person2:
firstname: July
lastname: Weber
person3:
firstname: John
lastname: Kurt
person4:
firstname: Simon
lastname: Gates

我需要的是逗号分隔的字符串,如 Maria, July, John, Simon

我尝试了以下方法:

firstnames: >-
{% set lastnames_list= [] %}
{% for name in addresses %}
{{ name.firstname | join(",")}}
{% endfor %}
{{ lastnames_list }}

firstnames: >-
{% set lastnames_list= [] %}
{% for name in addresses %}
{{ name | map(attribute="firstname") | join(",") }}
{% endfor %}
{{ lastnames_list }}

firstnames: '{{ addresses | map(attribute="firstname") | join(",") }}'

但 Ansible 给出了以下输出:

The task includes an option with an undefined variable. The error was: 'unicode object' has no attribute 'firstname'

最佳答案

简单 json_query filter可用于实现结果。

- debug:
msg: '{{ addresses | json_query("@.*.firstname") | join(", ") }}'

给予

ok: [localhost] => 
msg: Maria, July, John, Simon

关于ansible - 如何获得以逗号分隔的名字列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62497040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com