我需要检索我的Cloudfront实例(例如1234567890abcd.cloudfront.net)的DNS名称,并且想知道是否有一种快速方法可以在无需借助AWS CLI的情况下在Ansible中获得此名称。使用Ansible的Cloudfront事实
从闪闪发光的额外模块来源它会出现没有这个模块。其他人如何获得此属性?
我需要检索我的Cloudfront实例(例如1234567890abcd.cloudfront.net)的DNS名称,并且想知道是否有一种快速方法可以在无需借助AWS CLI的情况下在Ansible中获得此名称。使用Ansible的Cloudfront事实
从闪闪发光的额外模块来源它会出现没有这个模块。其他人如何获得此属性?
您可以编写自己的模块,也可以在几行中编写过滤器插件并完成相同的任务。
在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例子。
谢谢。我希望有一个更简单的方法,但创建一个模块可能不得不做。 –
在我看来,创建一个插件更容易获得这样的请求。除非你想有一堆选择。比一个模块将是最好的路线。 – linuxdynasty