如何在 Ansible 中更新嵌套变量 [英] How to update nested variables in Ansible
问题描述
我在/etc/ansible/facts.d/environment.fact 中存储了一些额外信息,例如数据库连接详细信息等.
I have some extra information like db connection details etc. stored in /etc/ansible/facts.d/environment.fact.
这些可以作为变量使用,例如 ansible_local.environment.database.name
.更新数据库名称的最佳方法是什么?
These are made available as variables like ansible_local.environment.database.name
. What is the best way to update the database name?
我尝试了 set_fact 模块,但无法正确更新嵌套变量.它只是覆盖整个 ansible_local
哈希.
I tried the set_fact module but could not get it to update the nested variable correctly. It just overwrites the whole ansible_local
hash.
- name: Update database name
set_fact:
args:
ansible_local:
environment:
database:
name: "{{ db_name }}"
推荐答案
这应该会有所帮助,假设您使用的是 Ansible 2.0 或更早版本.
This should help, assuming you are using Ansible 2.0 or older.
- set_fact:
test:
app:
in: 1
out: 2
- set_fact:
test_new:
app:
transform: 3
- set_fact:
test: "{{test|combine(test_new,recursive=True)}}"
- debug: var=test
A combine
是 Ansible 中包含的 Jinja2 过滤器.确保在这种情况下使用 recursive
参数.
这篇关于如何在 Ansible 中更新嵌套变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!