我有一个 Ansible 角色,它使用以下变量:connection_name、connection_ip 等来创建连接。
在我的主机文件中,我可以有许多“连接”并将其定义为列表字典:
host_file:
hosts:
host_one:
domain: one
ip: 172.18.1.1
connection:
- connection_name: two
connection_ip: 172.18.1.2
- connection_index: three
local_hub_ip: 172.18.1.3
host_two:
domain: two
ip: 172.18.1.2
host_three:
domain: three
ip: 172.18.1.3
如何将每个“连接”对象传递给我的 Ansible 角色,以便它可以使用嵌套变量“connection_name”和“connection_ip”?我曾尝试像这样使用“with_items”和“with_dict”:
---
- name: Creating new connection
hosts: "{{ host_name }}"
gather_facts: no
become: yes
remote_user: "{{ user | default('root')}}"
tasks:
- include_role:
name: connection-create
with_items: "{{connection}}"
但是我得到一个“FAILED:connection_name is undefined”,因为它以 {“connection_name”:二,“connection_ip”:172.18.1.2} 的形式传入对象,而不是内部部分,“connection_name”:二,“connection_ip”:没有 {} 的 172.18.1.2。