我正在尝试在 Ansible 中创建一个数组,其中包含我的帐户中启用的所有支持 FSx for NetApp ONTAP(FSxN)的 AWS 区域。
我知道我可以使用以下命令获取我的帐户上启用的区域列表:
- name: Get all the opted in regions.
amazon.aws.aws_region_info:
register: region_info
- name: Just get region names
set_fact:
opted_in_regions: "{{ [item.region_name] + opted_in_regions }}"
loop: "{{ region_info.regions }}"
但有时,通常是当一个新地区上线时,有些地区不支持 FSxN。
我发现了解哪些地区支持特定服务的唯一方法是从下载价格指南https://api.regional-table.region-services.aws.a2z.com/index.json
并查找将“Amazon FSx for NetApp ONTAP”作为“aws:serviceName”的地区。该文件的格式为:
{
"prices": [
{
"attributes": {
"aws:region": "ap-east-1",
"aws:serviceName": "Amazon Translate",
"aws:serviceUrl": "https://aws.amazon.com/translate/"
},
"id": "translate:ap-east-1"
},
{
"attributes": {
"aws:region": "ap-northeast-1",
"aws:serviceName": "Amazon Translate",
"aws:serviceUrl": "https://aws.amazon.com/translate/"
},
"id": "translate:ap-northeast-1"
},
因此,我所做的是使用文件内容创建变量:
- name: Get the capabilities of all regions.
set_fact:
regions_capabilities: "{{lookup('ansible.builtin.url', 'https://api.regional-table.region-services.aws.a2z.com/index.json', split_lines=false)}}"
然后下一个“任务”是循环遍历所有值并将字段中具有特定字符串的值添加到另一个数组中aws:serviceName
。
- name: Get the intersection of opted in regions and regions that support FSxN.
when: item['attributes']['aws:serviceName'] == "Amazon FSx for NetApp ONTAP" and item['attributes']['aws:region'] in opted_in_regions
set_fact:
fsxnRegions: "{{ [item['attributes']['aws:region']] + fsxnRegions }}"
loop: "{{ regions_capabilities.prices }}"
虽然有效,但该语句会在输出中when:
生成很多(数千)行。而且速度非常慢。skipping...
因此,问题是,是否有更好的方法来创建支持 FSxN 的区域数组?如果没有,我该如何隐藏该skipping
消息,但仅限于此任务?
以下是完整的 Ansible 剧本:
# Title: generate report
---
- hosts: localhost
collections:
- amazon.aws
gather_facts: false
name: Playbook to generate a report on all the FSxNs
vars:
fsxnRegions: []
opted_in_regions: []
tasks:
- name: Get all the opted in regions.
amazon.aws.aws_region_info:
register: region_info
- name: Just get region names
set_fact:
opted_in_regions: "{{ [item.region_name] + opted_in_regions }}"
loop: "{{ region_info.regions }}"
- name: Get the capabilities of all regions.
set_fact:
regions_capabilities: "{{lookup('ansible.builtin.url', 'https://api.regional-table.region-services.aws.a2z.com/index.json', split_lines=false)}}"
- name: Get the intersection of opted in regions and regions that support FSxN.
when: item['attributes']['aws:serviceName'] == "Amazon FSx for NetApp ONTAP" and item['attributes']['aws:region'] in opted_in_regions
set_fact:
fsxnRegions: "{{ [item['attributes']['aws:region']] + fsxnRegions }}"
loop: "{{ regions_capabilities.prices }}"
- name: Output
debug:
msg: "fsxnRegions={{ fsxnRegions }}"
如果我理解正确的话,你可以尝试使用
selectattr
而不使用loop