1#!/usr/bin/python 2# Copyright (c) 2017 Ansible Project 3# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 4 5ANSIBLE_METADATA = {'metadata_version': '1.1', 6 'status': ['preview'], 7 'supported_by': 'community'} 8 9 10DOCUMENTATION = ''' 11--- 12module: ec2_eip_info 13short_description: List EC2 EIP details 14description: 15 - List details of EC2 Elastic IP addresses. 16 - This module was called C(ec2_eip_facts) before Ansible 2.9. The usage did not change. 17version_added: "2.6" 18author: "Brad Macpherson (@iiibrad)" 19options: 20 filters: 21 description: 22 - A dict of filters to apply. Each dict item consists of a filter key and filter 23 value. See U(https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-addresses.html#options) 24 for possible filters. Filter names and values are case sensitive. 25 required: false 26 default: {} 27extends_documentation_fragment: 28 - aws 29 - ec2 30''' 31 32EXAMPLES = ''' 33# Note: These examples do not set authentication details or the AWS region, 34# see the AWS Guide for details. 35 36# List all EIP addresses in the current region. 37- ec2_eip_info: 38 register: regional_eip_addresses 39 40# List all EIP addresses for a VM. 41- ec2_eip_info: 42 filters: 43 instance-id: i-123456789 44 register: my_vm_eips 45 46- debug: msg="{{ my_vm_eips.addresses | json_query(\"[?private_ip_address=='10.0.0.5']\") }}" 47 48# List all EIP addresses for several VMs. 49- ec2_eip_info: 50 filters: 51 instance-id: 52 - i-123456789 53 - i-987654321 54 register: my_vms_eips 55 56# List all EIP addresses using the 'Name' tag as a filter. 57- ec2_eip_info: 58 filters: 59 tag:Name: www.example.com 60 register: my_vms_eips 61 62# List all EIP addresses using the Allocation-id as a filter 63- ec2_eip_info: 64 filters: 65 allocation-id: eipalloc-64de1b01 66 register: my_vms_eips 67 68# Set the variable eip_alloc to the value of the first allocation_id 69# and set the variable my_pub_ip to the value of the first public_ip 70- set_fact: 71 eip_alloc: my_vms_eips.addresses[0].allocation_id 72 my_pub_ip: my_vms_eips.addresses[0].public_ip 73 74''' 75 76 77RETURN = ''' 78addresses: 79 description: Properties of all Elastic IP addresses matching the provided filters. Each element is a dict with all the information related to an EIP. 80 returned: on success 81 type: list 82 sample: [{ 83 "allocation_id": "eipalloc-64de1b01", 84 "association_id": "eipassoc-0fe9ce90d6e983e97", 85 "domain": "vpc", 86 "instance_id": "i-01020cfeb25b0c84f", 87 "network_interface_id": "eni-02fdeadfd4beef9323b", 88 "network_interface_owner_id": "0123456789", 89 "private_ip_address": "10.0.0.1", 90 "public_ip": "54.81.104.1", 91 "tags": { 92 "Name": "test-vm-54.81.104.1" 93 } 94 }] 95 96''' 97 98from ansible.module_utils.aws.core import AnsibleAWSModule 99from ansible.module_utils.ec2 import (ansible_dict_to_boto3_filter_list, 100 boto3_tag_list_to_ansible_dict, 101 camel_dict_to_snake_dict) 102try: 103 from botocore.exceptions import (BotoCoreError, ClientError) 104except ImportError: 105 pass # caught by imported AnsibleAWSModule 106 107 108def get_eips_details(module): 109 connection = module.client('ec2') 110 filters = module.params.get("filters") 111 try: 112 response = connection.describe_addresses( 113 Filters=ansible_dict_to_boto3_filter_list(filters) 114 ) 115 except (BotoCoreError, ClientError) as e: 116 module.fail_json_aws( 117 e, 118 msg="Error retrieving EIPs") 119 120 addresses = camel_dict_to_snake_dict(response)['addresses'] 121 for address in addresses: 122 if 'tags' in address: 123 address['tags'] = boto3_tag_list_to_ansible_dict(address['tags']) 124 return addresses 125 126 127def main(): 128 module = AnsibleAWSModule( 129 argument_spec=dict( 130 filters=dict(type='dict', default={}) 131 ), 132 supports_check_mode=True 133 ) 134 if module._module._name == 'ec2_eip_facts': 135 module._module.deprecate("The 'ec2_eip_facts' module has been renamed to 'ec2_eip_info'", version='2.13') 136 137 module.exit_json(changed=False, addresses=get_eips_details(module)) 138 139 140if __name__ == '__main__': 141 main() 142