在ansible中只打印列表中的一项

1 ansible

我有一个包含列表的变量

- debug:
    var: plugin_versions
Run Code Online (Sandbox Code Playgroud)

输出

ok: [localhost] => {
    "plugin_versions": [
        {
            "name": "ace-editor",
            "version": "1.1"
        },
        {
            "name": "analysis-core",
            "version": "1.95"
        },
        {
            "name": "ant",
            "version": "1.9"
        }
]
Run Code Online (Sandbox Code Playgroud)

现在我只想打印出 name

我试过的是

- debug:
    var: plugin_versions.name

- debug:
    var: plugin_versions[name]
Run Code Online (Sandbox Code Playgroud)

但在这两种情况下我都得到

TASK [plugins : debug] ***************************************************************************************************************
ok: [localhost] => {
    "plugin_versions.name": "VARIABLE IS NOT DEFINED!"
}

TASK [plugins : debug] ***************************************************************************************************************
ok: [localhost] => {
    "plugin_versions[name]": "VARIABLE IS NOT DEFINED!"
}
Run Code Online (Sandbox Code Playgroud)

我有点不知道我还能在这里做些什么来打印出name唯一的。

ili*_*-sp 7

你可以通过几种方式做到这一点。这plugin_versions是一个字典列表,您可以使用循环打印每个字典的名称属性,这里有 2 个您可以使用的循环示例:

---
- hosts: localhost
  gather_facts: false
  vars:
    plugin_versions:
    - name: ace-editor
      version: '1.1'
    - name: analysis-core
      version: '1.95'
    - name: ant
      version: '1.9'

  tasks:

  - name: print variable - with_items
    debug:
      msg: "{{ item.name }}"
    with_items: 
    - "{{ plugin_versions }}"

  - name: print variable - with map filter
    debug:
      var: item
    with_items:
    - "{{ plugin_versions | map(attribute='name') | list }}"
Run Code Online (Sandbox Code Playgroud)

输出:

[http_offline@greenhat-29 tests]$ ansible-playbook -i hosts test.yml 

PLAY [localhost] *******************************************************************************************************************************************************************************************************

TASK [print variable - with_items] *************************************************************************************************************************************************************************************
ok: [localhost] => (item={'name': 'ace-editor', 'version': '1.1'}) => {
    "msg": "ace-editor"
}
ok: [localhost] => (item={'name': 'analysis-core', 'version': '1.95'}) => {
    "msg": "analysis-core"
}
ok: [localhost] => (item={'name': 'ant', 'version': '1.9'}) => {
    "msg": "ant"
}

TASK [print variable - with map filter] ********************************************************************************************************************************************************************************
ok: [localhost] => (item=ace-editor) => {
    "item": "ace-editor"
}
ok: [localhost] => (item=analysis-core) => {
    "item": "analysis-core"
}
ok: [localhost] => (item=ant) => {
    "item": "ant"
}

PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0   

[http_offline@greenhat-29 tests]$ 
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你

  • 这真的很有帮助,谢谢!- “{{plugin_versions | 地图(属性='名称')| 列表 }}” (2认同)