gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:13:39 26 4
gpt4 key购买 nike

我有一个包含列表的变量

- 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

最佳答案

您可以通过几种方式做到这一点。 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 }}"

输出:

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

希望对你有帮助

关于ansible - 在ansible中仅打印列表中的一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55811319/

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