gpt4 book ai didi

azure - 如何根据 set_facts 将多个 ansible 条件合并为一个

转载 作者:行者123 更新时间:2023-12-04 08:50:47 27 4
gpt4 key购买 nike

在经历 SO post 之一时对于 Ansible,对 set_fact 与不同条件检查的使用感兴趣。
然而,我根据我的方法回答了这篇文章,但是,我仍然认为可以改进为类似任务组合多个条件,例如确定位置资源组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 导入。

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]

最佳答案

您可以使用 loop 解决此问题和 zip过滤器,或者以更旧的方式,使用 with_together — 如果您想让您的剧本面向 future ,则不再推荐。

鉴于剧本:

- hosts: all
vars:
azure_vm_name: foo.bar.us-sea01.whatever.example.org

tasks:
- include_vars: azure_vars.yml

- set_fact:
host: "{{ azure_vm_name.split('.') }}"

- set_fact:
domain: "{{ host.2 }}"

- name: Domain
debug:
msg: "{{ domain }}"

- set_fact:
location: "{{ item.1 }}"
loop: "{{ azure_domains | zip(azure_location) | list }}"
when: 'domain == item.0'

- name: Location
debug:
msg: "{{ location }}"

变量文件azure_vars.yml:

azure_domains:
- us-sea01
- us-azrc2
- eu-azrc1

azure_location:
- westus2
- southcentralus
- westeurope

这产生了回顾:

PLAY [all] *******************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************
ok: [localhost]

TASK [include_vars] **********************************************************************************************
ok: [localhost]

TASK [set_fact] **************************************************************************************************
ok: [localhost]

TASK [set_fact] **************************************************************************************************
ok: [localhost]

TASK [Domain] ****************************************************************************************************
ok: [localhost] => {
"msg": "us-sea01"
}

TASK [set_fact] **************************************************************************************************
ok: [localhost] => (item=['us-sea01', 'westus2'])
skipping: [localhost] => (item=['us-azrc2', 'southcentralus'])
skipping: [localhost] => (item=['eu-azrc1', 'westeurope'])

TASK [Location] **************************************************************************************************
ok: [localhost] => {
"msg": "westus2"
}

PLAY RECAP *******************************************************************************************************
localhost : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
<小时/>

这表示还有另一种可能性,即使用 Python 的 Ansible 功能。您可以使用 index()列表的方法,用于将 azure_location 的元素定位为与 azure_domains 中的 domain 相同的位置。
例如如果domain位于azure_domains的位置2,您将获得中位置2的元素>azure_location

鉴于剧本:

- hosts: all
vars:
azure_vm_name: foo.bar.us-sea01.whatever.example.org

tasks:
- include_vars: azure_vars.yml

- set_fact:
host: "{{ azure_vm_name.split('.') }}"

- set_fact:
domain: "{{ host.2 }}"

- name: Domain
debug:
msg: "{{ domain }}"

- set_fact:
location: "{{ azure_location[azure_domains.index(domain)] }}"

- name: Location
debug:
msg: "{{ location }}"

以及相同的变量文件azure_vars.yml,这会产生回顾:

PLAY [all] ******************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************
ok: [localhost]

TASK [include_vars] *********************************************************************************************************
ok: [localhost]

TASK [set_fact] *************************************************************************************************************
ok: [localhost]

TASK [set_fact] *************************************************************************************************************
ok: [localhost]

TASK [Domain] ***************************************************************************************************************
ok: [localhost] => {
"msg": "us-sea01"
}

TASK [set_fact] *************************************************************************************************************
ok: [localhost]

TASK [Location] *************************************************************************************************************
ok: [localhost] => {
"msg": "westus2"
}

PLAY RECAP ******************************************************************************************************************
localhost : ok=7 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

它没有使用循环,因此更容易回顾。

关于azure - 如何根据 set_facts 将多个 ansible 条件合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64106956/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com