我的/ ansible / hosts文件中当前配置了两个主机。但是,我希望这两个主机具有两个不同的变量,但是使用相同的jinja2模板来访问它。如何应用条件语句?
我的./var/main.yaml看起来像这样:
---
- name: Variables for R1
when: "{{ ansible_host }}" == '198.51.100.4'
routers_all:
- {OSPF: 'Yes', OSPF_area: '0', OSPF_id: '1', hostname: R1}
int_details_R1:
- {int: Loopback1, ip: 10.0.0.1, mask: 255.255.255.255}
- {int: FastEthernet0/0, ip: 198.51.100.3, mask: 255.255.255.0}
- {int: FastEthernet1/0, ip: 198.51.101.3, mask: 255.255.255.0}
- {int: FastEthernet2/0, ip: 198.51.102.3, mask: 255.255.255.0}
- name: Variables for R2
when: "{{ ansible_host }}" == '198.51.100.5'
routers_all:
- {OSPF: 'Yes', OSPF_area: '0', OSPF_id: '2', hostname: R2}
int_details_R2:
- {int: Loopback1, ip: 20.0.0.1, mask: 255.255.255.255}
- {int: FastEthernet0/0, ip: 198.51.100.4, mask: 255.255.255.0}
- {int: FastEthernet1/0, ip: 198.51.101.4, mask: 255.255.255.0}
文件结构-
automation@automation:~/lab8/ANSIBLE/RTR-TEMPLATE$ tree
.
├── ospf.yaml
└── roles
└── router
├── tasks
│ └── main.yaml
├── templates
│ └── router.j2
└── vars
└── main.yaml
答案 0 :(得分:1)
使用host_vars。创建目录〜/ lab8 / ANSIBLE / RTR-TEMPLATE / host_vars并创建具有主机特定变量的文件。
〜/ lab8 / ANSIBLE / RTR-TEMPLATE / host_vars / 198.51.100.4
routers_all:
- {OSPF: 'Yes', OSPF_area: '0', OSPF_id: '1', hostname: R1}
int_details_R1:
- {int: Loopback1, ip: 10.0.0.1, mask: 255.255.255.255}
- {int: FastEthernet0/0, ip: 198.51.100.3, mask: 255.255.255.0}
- {int: FastEthernet1/0, ip: 198.51.101.3, mask: 255.255.255.0}
- {int: FastEthernet2/0, ip: 198.51.102.3, mask: 255.255.255.0}
〜/ lab8 / ANSIBLE / RTR-TEMPLATE / host_vars / 198.51.100.5
routers_all:
- {OSPF: 'Yes', OSPF_area: '0', OSPF_id: '2', hostname: R2}
int_details_R2:
- {int: Loopback1, ip: 20.0.0.1, mask: 255.255.255.255}
- {int: FastEthernet0/0, ip: 198.51.100.4, mask: 255.255.255.0}
- {int: FastEthernet1/0, ip: 198.51.101.4, mask: 255.255.255.0}
中所述的“默认vars插件”添加