我正在尝试在剧本中使用动态广告资源(netbox)中的一些hostvar。不知道这是否可能。动态库存如下所示:
{
"_meta": {
"hostvars": {
"switch1.lab1": {
"ansible_host": "192.168.1.6",
"device_roles": [
"TOR Switch"
],
"device_types": [
"EX4300-48T"
],
"manufacturers": [
"Juniper"
],
"primary_ip4": "192.168.1.6",
"sites": [
"LAB1"
],
"tags": [
"lab"
]
}
}
},
"all": {
"children": [
"device_roles_TOR Switch",
"ungrouped"
]
},
"device_roles_TOR Switch": {
"hosts": [
"switch1.lab1"
]
}
}
我正尝试使用main.yaml文件中的“站点”和“标签”部分:
tasks:
- include_tasks: lab-switch-update.yaml
when:
- (hostvars['sites'] == "LAB1")
- (hostvars['tags'] == "lab")
但是在进行播放时,它会不断跳过,因为:
TASK [include_tasks] ********************************************************************************************************************************************************
skipping: [switch1.lab1] => {"changed": false, "skip_reason": "Conditional result was False"}
PLAY RECAP ******************************************************************************************************************************************************************
switch1.lab1 : ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
有什么建议我在这里想念的吗?
答案 0 :(得分:0)
sites
和tags
都是列表。该剧在switch1.lab1
正确的条件是
when:
- sites.0 == 'LAB1'
- tags.0 == 'lab'
或(更好)
when:
- ('LAB1' in sites)
- ('lab' in tags)
tags
是reserved word。这些不应用作变量。