【发布时间】:2021-02-16 17:42:13
【问题描述】:
是否可以用新值更新 ansible 中的字典?
这是我所拥有的:
vars:
LIST_OF_CERTS:
- url: "supercoolhost.org"
port: 443
expiry_date: ""
- url: "anotherHost.org"
port: 443
expiry_date: ""
然后在其中一个剧本中,我发生了这种情况,我们循环遍历 LIST_OF_CERTS 并对它们执行各种任务。其中包括:
- name: Set Variables
set_fact:
CERT_NAME : "{{ item.url }}"
CERT_PORT : "{{ item.port }}"
哪个有效,CERT_NAME 和 CERT_PORT 用于其他一些任务。除此之外,我正在尝试使用以下内容更新 expiry_date 位:
- name: Set expiry date
set_fact:
"{{ item.expiry_date }}" : "{{ EXPIRY_DATE.stdout }}"
虽然我认为这是一个错误:
fatal: [localhost]: FAILED! => {"changed": false, "msg": "The variable name '' is not valid. Variables must start with a letter or underscore character, and contain only letters, numbers and underscores."}
我做错了什么?看起来“{{ item.expiry_date }}”会输出我拥有的空字符串,这是不正确的语法,这对我来说很有意义,但是如何更新项目字典中的 expiry_date 键?
UPDATE 将设置到期日期设置为新的,并尝试使用 combine 更新 dict:
- name: Set expiry date
set_fact:
CERT_EXPIRY : "{{ EXPIRY_DATE.stdout }}"
- name: Add in expiry date to list of certs and update dict.
set_fact:
LIST_OF_CERTS: "{ LIST_OF_CERTS|combine({{'url': CERT_NAME, 'port': CERT_PORT, 'expiry_date': CERT_EXPIRY }}) }"
这似乎使我的字典空白,因为我发送通知的电子邮件来自
The following certs were checked:
* {'url': 'supercoolhost.org', 'port': 443, 'expiry_date': ''}
* {'url': 'anotherHost.org', 'port': 443, 'expiry_date': ''}
只是:
The following certs were checked:
* url
* port
* expiry_date
【问题讨论】:
-
现在您正在尝试将字典合并到一个列表中,所以是的,您的逻辑在这里肯定是一个问题:
LIST_OF_CERTS: "{ LIST_OF_CERTS|combine({{'url': CERT_NAME, 'port': CERT_PORT, 'expiry_date': CERT_EXPIRY }}) }"
标签: loops dictionary ssl ansible certificate