在不知道键名的情况下循环遍历 Ansible 中的字典列表
loop over a list of dictionaries in Ansible without knowing key names
我一直在寻找解决方案,none 到目前为止似乎有效。
我有一个任务输出注册为:
"action_list.start_artifacts": [
{
"account-troubletree-v1-1-0-18-21001": "docker run -d -p 21001:8080 -v /apps/projects/logs/account-troubletree-v1-1-0-18-21001:/logs -v /apps/projects/logs/account-troubletree-v1-1-0-18-21001/.configmap:/tibco/config -v /apps/gwportal/appdynamics/:/apps/gwportal/appdynamics/ --env-file /apps/projects/logs/account-troubletree-v1-1-0-18-21001/config.env --name account-troubletree-v1-1-0-18-21001 account-troubletree-v1:1.0.18"
},
{
"account-troubletree-v1-1-0-17-21002": "docker run -d -p 21002:8080 -v /apps/projects/logs/account-troubletree-v1-1-0-17-21002:/logs -v /apps/projects/logs/account-troubletree-v1-1-0-17-21002/.configmap:/tibco/config -v /apps/gwportal/appdynamics/:/apps/gwportal/appdynamics/ --env-file /apps/projects/logs/account-troubletree-v1-1-0-17-21002/config.env --name account-tree-v1-1-0-17-21002 account-tree-v1:1.0.17"
}
]
我正在尝试使用以下任务获取密钥名称,但失败了。
- debug:
msg: "{{ item.key }}"
loop: "{{ action_list.start_artifacts }}"
您可以使用以下循环遍历字典变量,当然变量是静态的
default.yaml
具有变量
action_list.start_artifacts:
- account-troubletree-v1-1-0-18-21001: "docker run -d -p 21001:8080 -v /apps/projects/logs/account-troubletree-v1-1-0-18-21001:/logs -v /apps/projects/logs/account-troubletree-v1-1-0-18-21001/.configmap:/tibco/config -v /apps/gwportal/appdynamics/:/apps/gwportal/appdynamics/ --env-file /apps/projects/logs/account-troubletree-v1-1-0-18-21001/config.env --name account-troubletree-v1-1-0-18-21001 account-troubletree-v1:1.0.18"
account-troubletree-v1-1-0-17-21002: "docker run -d -p 21002:8080 -v /apps/projects/logs/account-troubletree-v1-1-0-17-21002:/logs -v /apps/projects/logs/account-troubletree-v1-1-0-17-21002/.configmap:/tibco/config -v /apps/gwportal/appdynamics/:/apps/gwportal/appdynamics/ --env-file /apps/projects/logs/account-troubletree-v1-1-0-17-21002/config.env --name account-tree-v1-1-0-17-21002 account-tree-v1:1.0.17"
- 使用变量的任务
- debug:
msg: "{{ item.account-troubletree-v1-1-0-18-21001 }} "
with_items: "{{ action_list.start_artifacts }}"
然而,如果它们是动态的,那么我们可以寻求解决方案,例如
regex-filter
下面的任务完成工作
- debug:
msg: "{{ item.keys()|first }}"
loop: "{{ action_list.start_artifacts }}"
下一个选项是
- debug:
msg: "{{ (item|dict2items).0.key }}"
loop: "{{ action_list.start_artifacts }}"
我一直在寻找解决方案,none 到目前为止似乎有效。
我有一个任务输出注册为:
"action_list.start_artifacts": [
{
"account-troubletree-v1-1-0-18-21001": "docker run -d -p 21001:8080 -v /apps/projects/logs/account-troubletree-v1-1-0-18-21001:/logs -v /apps/projects/logs/account-troubletree-v1-1-0-18-21001/.configmap:/tibco/config -v /apps/gwportal/appdynamics/:/apps/gwportal/appdynamics/ --env-file /apps/projects/logs/account-troubletree-v1-1-0-18-21001/config.env --name account-troubletree-v1-1-0-18-21001 account-troubletree-v1:1.0.18"
},
{
"account-troubletree-v1-1-0-17-21002": "docker run -d -p 21002:8080 -v /apps/projects/logs/account-troubletree-v1-1-0-17-21002:/logs -v /apps/projects/logs/account-troubletree-v1-1-0-17-21002/.configmap:/tibco/config -v /apps/gwportal/appdynamics/:/apps/gwportal/appdynamics/ --env-file /apps/projects/logs/account-troubletree-v1-1-0-17-21002/config.env --name account-tree-v1-1-0-17-21002 account-tree-v1:1.0.17"
}
]
我正在尝试使用以下任务获取密钥名称,但失败了。
- debug:
msg: "{{ item.key }}"
loop: "{{ action_list.start_artifacts }}"
您可以使用以下循环遍历字典变量,当然变量是静态的
default.yaml
具有变量
action_list.start_artifacts:
- account-troubletree-v1-1-0-18-21001: "docker run -d -p 21001:8080 -v /apps/projects/logs/account-troubletree-v1-1-0-18-21001:/logs -v /apps/projects/logs/account-troubletree-v1-1-0-18-21001/.configmap:/tibco/config -v /apps/gwportal/appdynamics/:/apps/gwportal/appdynamics/ --env-file /apps/projects/logs/account-troubletree-v1-1-0-18-21001/config.env --name account-troubletree-v1-1-0-18-21001 account-troubletree-v1:1.0.18"
account-troubletree-v1-1-0-17-21002: "docker run -d -p 21002:8080 -v /apps/projects/logs/account-troubletree-v1-1-0-17-21002:/logs -v /apps/projects/logs/account-troubletree-v1-1-0-17-21002/.configmap:/tibco/config -v /apps/gwportal/appdynamics/:/apps/gwportal/appdynamics/ --env-file /apps/projects/logs/account-troubletree-v1-1-0-17-21002/config.env --name account-tree-v1-1-0-17-21002 account-tree-v1:1.0.17"
- 使用变量的任务
- debug:
msg: "{{ item.account-troubletree-v1-1-0-18-21001 }} "
with_items: "{{ action_list.start_artifacts }}"
然而,如果它们是动态的,那么我们可以寻求解决方案,例如
下面的任务完成工作
- debug:
msg: "{{ item.keys()|first }}"
loop: "{{ action_list.start_artifacts }}"
下一个选项是
- debug:
msg: "{{ (item|dict2items).0.key }}"
loop: "{{ action_list.start_artifacts }}"