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

在ansible中仅打印列表中的一项,ansible,Ansible,我有一个包含列表的变量 - debug: var: plugin_versions 输出 ok: [localhost] => { "plugin_versions": [ { "name": "ace-editor", "version": "1.1" }, { "name": "analysis-core", "version

我有一个包含列表的变量

- 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"
        }
]
现在我只想打印出名字

我试过的是

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

我有点不知所措,在这里我还能做些什么来只打印出名称。

因为你有一个列表插件版本,你需要通过索引访问每个元素:

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

由于您有一个列表插件版本,您需要按索引访问每个元素:

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

你可以用几种方法来做。plugin_版本是一个字典列表,您可以使用循环打印每个字典的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]$ 

希望对你有所帮助。你可以用几种方法来做。plugin_版本是一个字典列表,您可以使用循环打印每个字典的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]$ 

希望对您有所帮助

您可以使用以下方法迭代变量项:

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

您可以使用以下命令迭代变量项:

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

这真的很有帮助,谢谢{{plugin_versions}mapatribute='name'| list}这真的很有帮助,谢谢{{plugin_versions}mapattribute='name'| list}