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

How can I get a comma separated list of firstnames?

这是我在 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'

map 过滤器实际上对一系列对象应用过滤器或查找属性。

但是在这里,您确实有一个字典,既不是对象序列,也不是仅具有属性的对象。

为了实现您正在寻找的东西,有两条路:

  1. 如果你坚持使用实际的 YAML 结构,你可以使用 Ansible dict2items 过滤器将你的字典返回到规范化列表中:

    firstnames: "{{ addresses | dict2items | map(attribute='value') | map(attribute='firstname') | join(',') }}"
    
  2. 您可以将您的字典转换为列表,以这种方式更改您的地址列表的结构,然后您的 Jinja 就会工作:

    addresses:
      - firstname: Maria
        lastname:  Smith
      - firstname: July
        lastname:  Weber
      - firstname: John
        lastname:  Kurt
      - firstname: Simon
        lastname:  Gates
    

    将与

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

以下是工作示例及其相应的概述。

  1. 剧本使用dict2items
    - hosts: local
      gather_facts: no
      vars:
        addresses:
          person1:
            firstname: Maria
            lastname:  Smith
          person2:
            firstname: July
            lastname:  Weber
          person3:
            firstname: John
            lastname:  Kurt
          person4:
            firstname: Simon
            lastname:  Gates
    
      tasks:
        - debug:
            msg: "{{ addresses | dict2items | map(attribute='value') | map(attribute='firstname') | join(',') }}"
    
    回顾一下:
    PLAY [local]    **************************************************************************************************************
    
    TASK [debug]    **************************************************************************************************************
    ok: [local] => {
        "msg": "Maria,July,John,Simon"
    }
    
    PLAY RECAP    ****************************************************************************************************************
    local                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0     
    
  2. 更改 YAML 地址结构的剧本:
    - hosts: local
      gather_facts: no
      vars:
        addresses:
          - firstname: Maria
            lastname:  Smith
          - firstname: July
            lastname:  Weber
          - firstname: John
            lastname:  Kurt
          - firstname: Simon
            lastname:  Gates
    
      tasks:
        - debug:
            msg: "{{ addresses | map(attribute='firstname') | join(',') }}"
    
    回顾一下:
    PLAY [local]    **************************************************************************************************************
    
    TASK [debug]    **************************************************************************************************************
    ok: [local] => {
        "msg": "Maria,July,John,Simon"
    }
    
    PLAY RECAP    ****************************************************************************************************************
    local                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
    

简单json_query filter即可实现结果

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

给予

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