From 1e2c8f39ff2199a886bc760c3e76475ec14bf101 Mon Sep 17 00:00:00 2001 From: Elliot Murphy Date: Sun, 30 Dec 2018 14:36:11 -0500 Subject: [PATCH 1/4] Support for associating to existing AWS Elastic IP Signed-off-by: Elliot Murphy --- requirements.txt | 2 +- roles/cloud-ec2/files/stack.yml | 17 +++++++++++++++- roles/cloud-ec2/tasks/cloudformation.yml | 1 + roles/cloud-ec2/tasks/prompts.yml | 25 ++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 38f36dac6..2dc62b068 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -ansible==2.5.2 +ansible==2.6.11 diff --git a/roles/cloud-ec2/files/stack.yml b/roles/cloud-ec2/files/stack.yml index 3660613b6..27a347938 100644 --- a/roles/cloud-ec2/files/stack.yml +++ b/roles/cloud-ec2/files/stack.yml @@ -11,6 +11,12 @@ Parameters: Type: String WireGuardPort: Type: String + UseThisElasticIP: + Type: String + Default: '' +Conditions: + AllocateNewEIP: !Equals [!Ref UseThisElasticIP, ''] + AssociateExistingEIP: !Not [!Equals [!Ref UseThisElasticIP, '']] Resources: VPC: Type: AWS::EC2::VPC @@ -187,6 +193,7 @@ Resources: ElasticIP: Type: AWS::EC2::EIP + Condition: AllocateNewEIP Properties: Domain: vpc InstanceId: !Ref EC2Instance @@ -194,6 +201,14 @@ Resources: - EC2Instance - VPCGatewayAttachment + ElasticIPAssociation: + Type: AWS::EC2::EIPAssociation + Condition: AssociateExistingEIP + Properties: + AllocationId: !Ref UseThisElasticIP + InstanceId: !Ref EC2Instance + + Outputs: ElasticIP: - Value: !Ref ElasticIP + Value: !GetAtt [EC2Instance, PublicIp] diff --git a/roles/cloud-ec2/tasks/cloudformation.yml b/roles/cloud-ec2/tasks/cloudformation.yml index 279772037..a0334ca98 100644 --- a/roles/cloud-ec2/tasks/cloudformation.yml +++ b/roles/cloud-ec2/tasks/cloudformation.yml @@ -12,6 +12,7 @@ PublicSSHKeyParameter: "{{ lookup('file', SSH_keys.public) }}" ImageIdParameter: "{{ ami_image }}" WireGuardPort: "{{ wireguard_port }}" + UseThisElasticIP: "{{ use_existing_eip }}" tags: Environment: Algo register: stack diff --git a/roles/cloud-ec2/tasks/prompts.yml b/roles/cloud-ec2/tasks/prompts.yml index 2993f6946..138209f05 100644 --- a/roles/cloud-ec2/tasks/prompts.yml +++ b/roles/cloud-ec2/tasks/prompts.yml @@ -53,3 +53,28 @@ [{{ default_region }}] register: _algo_region when: region is undefined + +- block: + - name: Get existing available Elastic IPs + ec2_eip_facts: + register: raw_eip_addresses + + - set_fact: + available_eip_addresses: "{{ raw_eip_addresses.addresses | selectattr('association_id', 'undefined') | list }}" + + - pause: + prompt: | + Do you want to use a pre-allocated Elastic IP? + (leave blank to allocate a new IP) + {% for eip in available_eip_addresses %} + {{ loop.index }}. {{ eip['public_ip'] }} + {% endfor %} + [''] + register: _use_existing_eip + + - set_fact: + use_existing_eip: >- + {% if _use_existing_eip.user_input is defined and _algo_region.user_input != "" %} + {{ available_eip_addresses[_use_existing_eip.user_input | int -1 ]['allocation_id'] }} + {%- else %}{% endif %} + when: use_existing_eip is undefined \ No newline at end of file From e64b60b41c4076a8423162554e5832e70d9b3069 Mon Sep 17 00:00:00 2001 From: Elliot Murphy Date: Tue, 8 Jan 2019 10:02:29 -0500 Subject: [PATCH 2/4] Backport ec2_eip_facts module for EIP support This means that EIP support no longer requires Ansible 2.6 The local fact module has been named ec2_elasticip_facts to avoid conflict with the ec2_eip_facts module whenever the Ansible 2.6 upgrade takes place. Signed-off-by: Elliot Murphy --- library/ec2_elasticip_facts.py | 122 ++++++++++++++++++++++++++++++ requirements.txt | 2 +- roles/cloud-ec2/tasks/prompts.yml | 2 +- 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 library/ec2_elasticip_facts.py diff --git a/library/ec2_elasticip_facts.py b/library/ec2_elasticip_facts.py new file mode 100644 index 000000000..31cb7f3c0 --- /dev/null +++ b/library/ec2_elasticip_facts.py @@ -0,0 +1,122 @@ +#!/usr/bin/python +# Copyright (c) 2017 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +# +# backported from Ansible 2.6. This can be removed and replaced with the +# ec2_eip_facts module after upgrading to Ansible 2.6 + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + + +DOCUMENTATION = ''' +--- +module: ec2_elasticip_facts +short_description: List EC2 EIP details +description: + - List details of EC2 Elastic IP addresses. +version_added: "2.6" +author: "Brad Macpherson (@iiibrad)" +options: + filters: + description: + - A set of filters to use. Each filter is a name:value pair. The value + may be a list or a single element. + required: false + default: {} +extends_documentation_fragment: + - aws + - ec2 +''' + +EXAMPLES = ''' +# Note: These examples do not set authentication details or the AWS region, +# see the AWS Guide for details. + +# List all EIP addresses in the current region. +- ec2_elasticip_facts: + register: regional_eip_addresses + +# List all EIP addresses for a VM. +- ec2_elasticip_facts: + filters: + instance-id: i-123456789 + register: my_vm_eips + +- debug: msg="{{ my_vm_eips.addresses | json_query(\"[?private_ip_address=='10.0.0.5']\") }}" + +# List all EIP addresses for several VMs. +- ec2_elasticip_facts: + filters: + instance-id: + - i-123456789 + - i-987654321 + register: my_vms_eips + +''' + + +RETURN = ''' +addresses: + description: Properties of all Elastic IP addresses matching the provided filters. Each element is a dict with all the information related to an EIP. + returned: on success + type: list + sample: [{ + "allocation_id": "eipalloc-64de1b01", + "association_id": "eipassoc-0fe9ce90d6e983e97", + "domain": "vpc", + "instance_id": "i-01020cfeb25b0c84f", + "network_interface_id": "eni-02fdeadfd4beef9323b", + "network_interface_owner_id": "0123456789", + "private_ip_address": "10.0.0.1", + "public_ip": "54.81.104.1", + "tags": { + "Name": "test-vm-54.81.104.1" + } + }] + +''' + +from ansible.module_utils.aws.core import AnsibleAWSModule +from ansible.module_utils.ec2 import (ansible_dict_to_boto3_filter_list, + boto3_tag_list_to_ansible_dict, + camel_dict_to_snake_dict) +try: + from botocore.exceptions import (BotoCoreError, ClientError) +except ImportError: + pass # caught by imported AnsibleAWSModule + + +def get_eips_details(module): + connection = module.client('ec2') + filters = module.params.get("filters") + try: + response = connection.describe_addresses( + Filters=ansible_dict_to_boto3_filter_list(filters) + ) + except (BotoCoreError, ClientError) as e: + module.fail_json_aws( + e, + msg="Error retrieving EIPs") + + addresses = camel_dict_to_snake_dict(response)['addresses'] + for address in addresses: + if 'tags' in address: + address['tags'] = boto3_tag_list_to_ansible_dict(address['tags']) + return addresses + + +def main(): + module = AnsibleAWSModule( + argument_spec=dict( + filters=dict(type='dict', default={}) + ), + supports_check_mode=True + ) + + module.exit_json(changed=False, addresses=get_eips_details(module)) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 2dc62b068..38f36dac6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -ansible==2.6.11 +ansible==2.5.2 diff --git a/roles/cloud-ec2/tasks/prompts.yml b/roles/cloud-ec2/tasks/prompts.yml index 138209f05..a0bab6e76 100644 --- a/roles/cloud-ec2/tasks/prompts.yml +++ b/roles/cloud-ec2/tasks/prompts.yml @@ -56,7 +56,7 @@ - block: - name: Get existing available Elastic IPs - ec2_eip_facts: + ec2_elasticip_facts: register: raw_eip_addresses - set_fact: From 3536bbbe7d73867ed4344556d885533324d1ef21 Mon Sep 17 00:00:00 2001 From: Elliot Murphy Date: Fri, 29 Mar 2019 11:25:17 -0400 Subject: [PATCH 3/4] Update from review feedback. Signed-off-by: Elliot Murphy --- roles/cloud-ec2/tasks/prompts.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roles/cloud-ec2/tasks/prompts.yml b/roles/cloud-ec2/tasks/prompts.yml index a0bab6e76..22390cf5b 100644 --- a/roles/cloud-ec2/tasks/prompts.yml +++ b/roles/cloud-ec2/tasks/prompts.yml @@ -77,4 +77,4 @@ {% if _use_existing_eip.user_input is defined and _algo_region.user_input != "" %} {{ available_eip_addresses[_use_existing_eip.user_input | int -1 ]['allocation_id'] }} {%- else %}{% endif %} - when: use_existing_eip is undefined \ No newline at end of file + when: use_existing_eip From fe0b29029457efcc1d4fdb0468e15a9952d94039 Mon Sep 17 00:00:00 2001 From: Jack Ivanov Date: Fri, 17 May 2019 15:59:35 +0200 Subject: [PATCH 4/4] Move to the native module. Add additional condition for existing Elastic IP --- config.cfg | 7 +- library/ec2_elasticip_facts.py | 122 ----------------------- roles/cloud-ec2/defaults/main.yml | 1 + roles/cloud-ec2/tasks/cloudformation.yml | 2 +- roles/cloud-ec2/tasks/prompts.yml | 17 ++-- 5 files changed, 14 insertions(+), 135 deletions(-) delete mode 100644 library/ec2_elasticip_facts.py diff --git a/config.cfg b/config.cfg index 181ae0227..73a5d96f9 100644 --- a/config.cfg +++ b/config.cfg @@ -131,9 +131,12 @@ cloud_providers: size: s-1vcpu-1gb image: "ubuntu-18-04-x64" ec2: - # Change the encrypted flag to "true" to enable AWS volume encryption, for encryption of data at rest. - # Warning: the Algo script will take approximately 6 minutes longer to complete. + # Change the encrypted flag to "true" to enable AWS volume encryption, for encryption of data at rest. + # Warning: the Algo script will take approximately 6 minutes longer to complete. encrypted: false + # Set use_existing_eip to "true" if you want to use a pre-allocated Elastic IP + # Additional prompt will be raised to determine which IP to use + use_existing_eip: true size: t2.micro image: name: "ubuntu-bionic-18.04" diff --git a/library/ec2_elasticip_facts.py b/library/ec2_elasticip_facts.py deleted file mode 100644 index 31cb7f3c0..000000000 --- a/library/ec2_elasticip_facts.py +++ /dev/null @@ -1,122 +0,0 @@ -#!/usr/bin/python -# Copyright (c) 2017 Ansible Project -# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -# -# backported from Ansible 2.6. This can be removed and replaced with the -# ec2_eip_facts module after upgrading to Ansible 2.6 - -ANSIBLE_METADATA = {'metadata_version': '1.1', - 'status': ['preview'], - 'supported_by': 'community'} - - -DOCUMENTATION = ''' ---- -module: ec2_elasticip_facts -short_description: List EC2 EIP details -description: - - List details of EC2 Elastic IP addresses. -version_added: "2.6" -author: "Brad Macpherson (@iiibrad)" -options: - filters: - description: - - A set of filters to use. Each filter is a name:value pair. The value - may be a list or a single element. - required: false - default: {} -extends_documentation_fragment: - - aws - - ec2 -''' - -EXAMPLES = ''' -# Note: These examples do not set authentication details or the AWS region, -# see the AWS Guide for details. - -# List all EIP addresses in the current region. -- ec2_elasticip_facts: - register: regional_eip_addresses - -# List all EIP addresses for a VM. -- ec2_elasticip_facts: - filters: - instance-id: i-123456789 - register: my_vm_eips - -- debug: msg="{{ my_vm_eips.addresses | json_query(\"[?private_ip_address=='10.0.0.5']\") }}" - -# List all EIP addresses for several VMs. -- ec2_elasticip_facts: - filters: - instance-id: - - i-123456789 - - i-987654321 - register: my_vms_eips - -''' - - -RETURN = ''' -addresses: - description: Properties of all Elastic IP addresses matching the provided filters. Each element is a dict with all the information related to an EIP. - returned: on success - type: list - sample: [{ - "allocation_id": "eipalloc-64de1b01", - "association_id": "eipassoc-0fe9ce90d6e983e97", - "domain": "vpc", - "instance_id": "i-01020cfeb25b0c84f", - "network_interface_id": "eni-02fdeadfd4beef9323b", - "network_interface_owner_id": "0123456789", - "private_ip_address": "10.0.0.1", - "public_ip": "54.81.104.1", - "tags": { - "Name": "test-vm-54.81.104.1" - } - }] - -''' - -from ansible.module_utils.aws.core import AnsibleAWSModule -from ansible.module_utils.ec2 import (ansible_dict_to_boto3_filter_list, - boto3_tag_list_to_ansible_dict, - camel_dict_to_snake_dict) -try: - from botocore.exceptions import (BotoCoreError, ClientError) -except ImportError: - pass # caught by imported AnsibleAWSModule - - -def get_eips_details(module): - connection = module.client('ec2') - filters = module.params.get("filters") - try: - response = connection.describe_addresses( - Filters=ansible_dict_to_boto3_filter_list(filters) - ) - except (BotoCoreError, ClientError) as e: - module.fail_json_aws( - e, - msg="Error retrieving EIPs") - - addresses = camel_dict_to_snake_dict(response)['addresses'] - for address in addresses: - if 'tags' in address: - address['tags'] = boto3_tag_list_to_ansible_dict(address['tags']) - return addresses - - -def main(): - module = AnsibleAWSModule( - argument_spec=dict( - filters=dict(type='dict', default={}) - ), - supports_check_mode=True - ) - - module.exit_json(changed=False, addresses=get_eips_details(module)) - - -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/roles/cloud-ec2/defaults/main.yml b/roles/cloud-ec2/defaults/main.yml index 12b3f19d0..86ae99543 100644 --- a/roles/cloud-ec2/defaults/main.yml +++ b/roles/cloud-ec2/defaults/main.yml @@ -5,3 +5,4 @@ ec2_vpc_nets: cidr_block: 172.16.0.0/16 subnet_cidr: 172.16.254.0/23 ec2_venv: "{{ playbook_dir }}/configs/.venvs/aws" +existing_eip: "" diff --git a/roles/cloud-ec2/tasks/cloudformation.yml b/roles/cloud-ec2/tasks/cloudformation.yml index 8f6551762..8aadfaa7f 100644 --- a/roles/cloud-ec2/tasks/cloudformation.yml +++ b/roles/cloud-ec2/tasks/cloudformation.yml @@ -12,7 +12,7 @@ PublicSSHKeyParameter: "{{ lookup('file', SSH_keys.public) }}" ImageIdParameter: "{{ ami_image }}" WireGuardPort: "{{ wireguard_port }}" - UseThisElasticIP: "{{ use_existing_eip }}" + UseThisElasticIP: "{{ existing_eip }}" tags: Environment: Algo register: stack diff --git a/roles/cloud-ec2/tasks/prompts.yml b/roles/cloud-ec2/tasks/prompts.yml index 22390cf5b..040af8342 100644 --- a/roles/cloud-ec2/tasks/prompts.yml +++ b/roles/cloud-ec2/tasks/prompts.yml @@ -56,25 +56,22 @@ - block: - name: Get existing available Elastic IPs - ec2_elasticip_facts: + ec2_eip_facts: register: raw_eip_addresses - set_fact: available_eip_addresses: "{{ raw_eip_addresses.addresses | selectattr('association_id', 'undefined') | list }}" - pause: - prompt: | - Do you want to use a pre-allocated Elastic IP? - (leave blank to allocate a new IP) + prompt: >- + What Elastic IP would you like to use? {% for eip in available_eip_addresses %} {{ loop.index }}. {{ eip['public_ip'] }} {% endfor %} - [''] + + Enter the number of your desired Elastic IP register: _use_existing_eip - set_fact: - use_existing_eip: >- - {% if _use_existing_eip.user_input is defined and _algo_region.user_input != "" %} - {{ available_eip_addresses[_use_existing_eip.user_input | int -1 ]['allocation_id'] }} - {%- else %}{% endif %} - when: use_existing_eip + existing_eip: "{{ available_eip_addresses[_use_existing_eip.user_input | int -1 ]['allocation_id'] }}" + when: cloud_providers.ec2.use_existing_eip