[英]Looping through ansible dictionary to assign dictionary values
Is there a way to loop through an ansible dictionary to assign other ansible dictionary values?有没有办法循环通过 ansible 字典来分配其他 ansible 字典值?
I have the following in my group_vars/all:我的 group_vars/all 中有以下内容:
domain_controllers:
dc0:
hostname: "dc0"
ip_address: "192.168.1.0"
dc1:
hostname: "dc1"
ip_address: "192.168.1.1"
dc2:
hostanme: "dc2"
ip_address: "192.168.1.2"
I have a generic keepalived role that configures keepalived that a server role uses to make a VIP for these domain controllers.我有一个通用的keepalived 角色,它配置了服务器角色用来为这些域控制器创建VIP 的keepalived。
The following is my roles/servers/forward/vars.yml
which the keepalived role uses to build the keepalived.conf file.以下是我的
roles/servers/forward/vars.yml
角色用于构建 keepalived.conf 文件。
virtual_server:
- virutal_server_ip: "192.168.1.10"
virtual_server_port: 389
...
real_servers:
- real_ip: "192.168.1.0"
real_port: 389
description: "dc0"
...
- real_ip: "192.168.1.1"
real_port: 389
description: "dc1"
...
- real_ip: "192.168.1.2"
real_port: 389
description: "dc2"
...
- virtual_server_ip: "192.168.1.10"
virtual_server_port: 636
...
real_servers:
- real_ip: "192.168.1.0"
real_port: 636
description: "dc0"
...
- real_ip: "192.168.1.1"
real_port: 636
description: "dc1"
...
- real_ip: "192.168.1.2"
real_port: 636
description: "dc2"
...
I already have the jinja formatting in the keepalived role to build /etc/keepalived.conf
based on these variables.我已经在 keepalived 角色中设置了 jinja 格式,可以基于这些变量构建
/etc/keepalived.conf
。
What I want to do is assign these variables based on the contents of the domain_controllers
dictionary.我想要做的是根据
domain_controllers
字典的内容分配这些变量。 How could I use set_facts
to assign a large nested dictionary like this?我如何使用
set_facts
来分配像这样的大型嵌套字典?
I would like to just loop through domain_controllers
because I would like to just update my group_vars/all
when I upgrade and/or migrate to new DCs since I also have other playbooks that use this variable.我只想循环访问
domain_controllers
,因为我想在升级和/或迁移到新 DC 时更新我的group_vars/all
,因为我还有其他使用此变量的剧本。
Additionally is is possible to assign these variables in vars/main.yml
rather than through a set_facts
task?此外,是否可以在
vars/main.yml
中分配这些变量,而不是通过set_facts
任务?
I solved this with the following:我用以下方法解决了这个问题:
In group_vars/all:在 group_vars/all 中:
domain_controllers:
- ip_address: "192.168.1.0"
hostname: "dc0"
- ip_address: "192.168.1.1"
hostname: "dc1"
- ip_address: "192.168.1.2"
hostname: "dc2"
And in my vars file I called the list with:在我的 vars 文件中,我使用以下命令调用列表:
virtual_server:
- virutal_server_ip: "192.168.1.10"
virtual_server_port: 389
...
real_servers:
- real_ip: "{{ domain_controllers.0.ip_address }}"
real_port: 389
description: "{{ domain_controllers.0.hostname }}"
...
- real_ip: "{{ domain_controllers.1.ip_address }}"
real_port: 389
description: "{{ domain_controllers.1.hostname }}"
...
- real_ip: "{{ domain_controllers.2.ip_address }}"
real_port: 389
description: "{{ domain_controllers.2.hostname }}"
...
- virtual_server_ip: "192.168.1.10"
virtual_server_port: 636
...
real_servers:
- real_ip: "{{ domain_controllers.0.ip_address }}"
real_port: 636
description: "{{ domain_controllers.0.hostname }}"
...
- real_ip: "{{ domain_controllers.1.ip_address }}"
real_port: 636
description: "{{ domain_controllers.1.hostname }}"
...
- real_ip: "{{ domain_controllers.2.ip_address }}"
real_port: 636
description: "{{ domain_controllers.2.hostname }}"
...
The downside to this is that the playbook errors if there aren't at least 3 items in domain_controllers
.这样做的缺点是,如果
domain_controllers
中没有至少 3 个项目,则剧本会出错。 I would still like to find a way to loop over the list.我仍然想找到一种方法来遍历列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.