问题描述
在通过SO post中的一个ansible
配方时,我发现将set_fact
与不同的条件检查一起使用时很有趣。但是,我根据自己的方法在帖子中作了回答。不过,我仍然看到可以将类似条件的多个条件合并在一起,例如确定location
resource group
和vnet
。>
下面是playbook
和变量文件azure_vars.yml
播放
---
- name: create azure vm
hosts: localhost
connection: local
tasks:
- include_vars: azure_vars.yml
- set_fact:
host: "{{ azure_vm_name.split('.') }}"
- set_fact:
domain: "{{ host.1 }}.{{ host.2 }}"
- name: Domain
debug:
msg: "{{ domain }}"
- set_fact:
location: "{{ azure_location[0] }}"
when: 'domain == azure_domain[0]'
- set_fact:
location: "{{ azure_location[1] }}"
when: 'domain == azure_domain[1]'
- set_fact:
location: "{{ azure_location[2] }}"
when: 'domain == azure_domain[2]'
- name: Location
debug:
msg: "{{ location }}"
- set_fact:
res_group: "{{ azure_res_group[0] }}"
when: 'domain == azure_domain[0]'
- set_fact:
res_group: "{{ azure_res_group[1] }}"
when: 'domain == azure_domain[1]'
- set_fact:
res_group: "{{ azure_res_group[2] }}"
when: 'domain == azure_domain[2]'
- name: Resource Group
debug:
msg: "{{ res_group }}"
- set_fact:
vnet: "{{ azure_nprod_vnet }}"
when: 'domain == azure_domain[0]'
- set_fact:
vnet: "{{ azure_prod03_vnet }}"
when: 'domain == azure_domain[2]'
- set_fact:
vnet: "{{ azure_prod02_vnet }}"
when: 'domain == azure_domain[1]'
- name: Vnet
debug:
msg: "{{ vnet }}"
变量文件
此文件包含所有变量,这些变量将是playbook
的一部分,并在任务部分作为include_vars
导入。
$ cat azure_vars.yml
---
azure_nprod_vnet: "/subscriptions/XXXXXXXX-XXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/rg001/providers/Microsoft.Network/virtualNetworks/vnet"
azure_prod02_vnet: "/subscriptions/XXXXXXXX-XXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/rg003/providers/Microsoft.Network/virtualNetworks/vnet"
azure_prod03_vnet: "/subscriptions/XXXXXXXX-XXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/rg002/providers/Microsoft.Network/virtualNetworks/vnet"
# Azure domain
azure_domains:
- us-sea01
- us-azrc2
- eu-azrc1
# Resource group
azure_res_group:
- rg001
- rg002
- rg003
# Azure locations
azure_location:
- westus2
- southcentralus
- westeurope
...
期望将以下三项合并为一种条件:
- set_fact:
location: "{{ azure_location[0] }}"
when: 'domain == azure_domain[0]'
- set_fact:
location: "{{ azure_location[1] }}"
when: 'domain == azure_domain[1]'
- set_fact:
location: "{{ azure_location[2] }}"
when: 'domain == azure_domain[2]'
可能类似于:
- set_fact:
location:
- azure_location[0]
- azure_location[1]
- azure_location[2]
when:
- 'domain == azure_domain[0]
- 'domain == azure_domain[1]
- 'domain == azure_domain[2]
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)