0

I need to retrieve the DNS name of my Cloudfront instance (eg. 1234567890abcd.cloudfront.net) and was wondering if there is a quick way to get this in Ansible without resorting to the AWS CLI.

From gleaming the Extra Modules source it would appear there is not a module for this. How are other people getting this attribute?

2 Answers 2

1

You can either write your own module or you can write a filter plugin in a few lines and accomplish the same thing.

Example of writing a filter in Ansible. Lets name this file aws.py in your filter_plugins/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,}

In order to use this plugin, you just need to call it.

dns_entry: "{{ 'us-west-2' | get_cloudfront_dns('123434JHJHJH') }}"

Keep in mind, you will need boto3 and botocore installed, in order to use this this plugin.

I have a bunch of examples located in my repo linuxdynasty ld-ansible-filters repo

2
  • Thanks. I was hoping there was an easier way but creating a module may have to do. Dec 24, 2016 at 3:11
  • In my opinion, creating a plugin is simpler for get requests like this. Unless you want to have a bunch of options. Than a module would be the best route. Dec 24, 2016 at 21:08
0

I ended up writing a module for this (cloudfront_facts.py) that has been accepted into Ansible 2.3.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.