在ansible中仅打印列表中的一个项目

问题描述 投票:0回答:3

我有一个包含列表的变量

- debug:
    var: plugin_versions

产量

ok: [localhost] => {
    "plugin_versions": [
        {
            "name": "ace-editor",
            "version": "1.1"
        },
        {
            "name": "analysis-core",
            "version": "1.95"
        },
        {
            "name": "ant",
            "version": "1.9"
        }
]

现在我想打印出name

我试过的是

- debug:
    var: plugin_versions.name

- debug:
    var: plugin_versions[name]

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

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

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

我有点无能为力,我还可以在这里打印出name

ansible
3个回答
2
投票

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

---
- 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 }}"

输出:

[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]$ 

希望能帮助到你


0
投票

您可以使用以下内容迭代变量的项目:

- debug:
      var: item.name
  with_items: "{{plugin_versions}}"

0
投票

由于您有一个列表(plugin_versions),您需要按索引访问每个元素:

---
- hosts: all

  vars:
    plugin_versions:
    - { name: "ace-editor", version: "1.1"}
    - { name: "analysis-core", version: "1.95"}
    - { name: "ant", version: "1.9"}

  tasks:
  - debug:  var=plugin_versions[0].name

另外,如果你想遍历列表中的所有项目,你可以这样做:

  - debug: "var=item['name']"
    with_items: "{{ plugin_versions }}"
© www.soinside.com 2019 - 2024. All rights reserved.