我一直在寻找解决方案,目前似乎没有一个解决方案。
我有一个任务输出注册为:
"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 }}"
答案 0 :(得分:1)
下面的任务完成工作
- debug:
msg: "{{ item.keys()|first }}"
loop: "{{ action_list.start_artifacts }}"
下一个选项是
- debug:
msg: "{{ (item|dict2items).0.key }}"
loop: "{{ action_list.start_artifacts }}"
答案 1 :(得分:0)
您可以使用以下内容循环字典变量,当然变量是静态的
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-search regex-filter