使用 Ansible 的云端事实
Cloudfront facts using Ansible
我需要检索我的 Cloudfront 实例的 DNS 名称(例如 1234567890abcd.cloudfront.net),我想知道是否有一种无需借助 AWS CLI 即可在 Ansible 中获取此名称的快速方法。
从 Extra Modules 源代码来看,似乎没有用于此的模块。其他人如何获得此属性?
您可以编写自己的模块,也可以用几行代码编写一个过滤器插件来完成同样的事情。
在 Ansible 中编写过滤器的示例。让我们在 filter_plugins/aws.py
中将此文件命名为 aws.py
import boto3
import botocore
from ansible import errors
def get_cloudfront_dns(region, dist_id):
""" Return the dns name of the cloudfront distribution id.
Args:
region (str): The AWS region.
dist_id (str): distribution id
Basic Usage:
>>> get_cloudfront_dns('us-west-2', 'E123456LHXOD5FK')
'1234567890abcd.cloudfront.net'
"""
client = boto3.client('cloudfront', region)
domain_name = None
try:
domain_name = (
client
.get_distribution(Id=dist_id)['Distribution']['DomainName']
)
except Exception as e:
if isinstance(e, botocore.exceptions.ClientError):
raise e
else:
raise errors.AnsibleFilterError(
'Could not retreive the dns name for CloudFront Dist ID {0}: {1}'.format(dist_id, str(e))
)
return domain_name
class FilterModule(object):
''' Ansible core jinja2 filters '''
def filters(self):
return {'get_cloudfront_dns': get_cloudfront_dns,}
为了使用这个插件,你只需要调用它。
dns_entry: "{{ 'us-west-2' | get_cloudfront_dns('123434JHJHJH') }}"
请记住,您需要安装 boto3 和 botocore,才能使用此插件。
我的存储库中有很多示例 linuxdynasty ld-ansible-filters repo
我最终为此 (cloudfront_facts.py) 编写了一个已被 Ansible 2.3.0 接受的模块。
我需要检索我的 Cloudfront 实例的 DNS 名称(例如 1234567890abcd.cloudfront.net),我想知道是否有一种无需借助 AWS CLI 即可在 Ansible 中获取此名称的快速方法。
从 Extra Modules 源代码来看,似乎没有用于此的模块。其他人如何获得此属性?
您可以编写自己的模块,也可以用几行代码编写一个过滤器插件来完成同样的事情。
在 Ansible 中编写过滤器的示例。让我们在 filter_plugins/aws.py
中将此文件命名为 aws.pyimport boto3
import botocore
from ansible import errors
def get_cloudfront_dns(region, dist_id):
""" Return the dns name of the cloudfront distribution id.
Args:
region (str): The AWS region.
dist_id (str): distribution id
Basic Usage:
>>> get_cloudfront_dns('us-west-2', 'E123456LHXOD5FK')
'1234567890abcd.cloudfront.net'
"""
client = boto3.client('cloudfront', region)
domain_name = None
try:
domain_name = (
client
.get_distribution(Id=dist_id)['Distribution']['DomainName']
)
except Exception as e:
if isinstance(e, botocore.exceptions.ClientError):
raise e
else:
raise errors.AnsibleFilterError(
'Could not retreive the dns name for CloudFront Dist ID {0}: {1}'.format(dist_id, str(e))
)
return domain_name
class FilterModule(object):
''' Ansible core jinja2 filters '''
def filters(self):
return {'get_cloudfront_dns': get_cloudfront_dns,}
为了使用这个插件,你只需要调用它。
dns_entry: "{{ 'us-west-2' | get_cloudfront_dns('123434JHJHJH') }}"
请记住,您需要安装 boto3 和 botocore,才能使用此插件。
我的存储库中有很多示例 linuxdynasty ld-ansible-filters repo
我最终为此 (cloudfront_facts.py) 编写了一个已被 Ansible 2.3.0 接受的模块。