2015-12-29 10:48:00 +01:00
|
|
|
#!/usr/bin/python
|
2015-12-29 11:05:16 +01:00
|
|
|
# This file is part of Ansible
|
2015-12-29 10:48:00 +01:00
|
|
|
#
|
2015-12-29 11:05:16 +01:00
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2015-12-29 10:48:00 +01:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
module: ec2_vpc_peer
|
2015-12-29 11:05:16 +01:00
|
|
|
short_description: create, delete, accept, and reject VPC peering connections between two VPCs.
|
2015-12-29 10:48:00 +01:00
|
|
|
description:
|
2015-12-29 11:05:16 +01:00
|
|
|
- Read the AWS documentation for VPC Peering Connections
|
|
|
|
U(http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/vpc-peering.html)
|
2016-05-08 06:07:21 +02:00
|
|
|
version_added: "2.2"
|
2015-12-29 10:48:00 +01:00
|
|
|
options:
|
|
|
|
vpc_id:
|
|
|
|
description:
|
|
|
|
- VPC id of the requesting VPC.
|
|
|
|
required: false
|
|
|
|
peer_vpc_id:
|
|
|
|
description:
|
|
|
|
- VPC id of the accepting VPC.
|
|
|
|
required: false
|
|
|
|
peer_owner_id:
|
|
|
|
description:
|
|
|
|
- The AWS account number for cross account peering.
|
|
|
|
required: false
|
2016-03-28 07:07:39 +02:00
|
|
|
tags:
|
|
|
|
description:
|
|
|
|
- Dictionary of tags to look for and apply when creating a Peering Connection.
|
|
|
|
required: false
|
2015-12-29 10:48:00 +01:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Create, delete, accept, reject a peering connection.
|
|
|
|
required: false
|
|
|
|
default: present
|
|
|
|
choices: ['present', 'absent', 'accept', 'reject']
|
|
|
|
author: Mike Mochan(@mmochan)
|
|
|
|
extends_documentation_fragment: aws
|
2016-05-08 06:07:21 +02:00
|
|
|
requirements: [ botocore, boto3, json ]
|
2015-12-29 10:48:00 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Complete example to create and accept a local peering connection.
|
|
|
|
- name: Create local account VPC peering Connection
|
|
|
|
ec2_vpc_peer:
|
|
|
|
region: ap-southeast-2
|
|
|
|
vpc_id: vpc-12345678
|
|
|
|
peer_vpc_id: vpc-87654321
|
|
|
|
state: present
|
2016-03-28 07:07:39 +02:00
|
|
|
tags:
|
|
|
|
Name: Peering conenction for VPC 21 to VPC 22
|
|
|
|
CostCode: CC1234
|
|
|
|
Project: phoenix
|
2015-12-29 10:48:00 +01:00
|
|
|
register: vpc_peer
|
|
|
|
|
|
|
|
- name: Accept local VPC peering request
|
|
|
|
ec2_vpc_peer:
|
|
|
|
region: ap-southeast-2
|
|
|
|
peering_id: "{{ vpc_peer.peering_id }}"
|
|
|
|
state: accept
|
|
|
|
register: action_peer
|
|
|
|
|
|
|
|
# Complete example to delete a local peering connection.
|
|
|
|
- name: Create local account VPC peering Connection
|
|
|
|
ec2_vpc_peer:
|
|
|
|
region: ap-southeast-2
|
|
|
|
vpc_id: vpc-12345678
|
|
|
|
peer_vpc_id: vpc-87654321
|
|
|
|
state: present
|
2016-03-28 07:07:39 +02:00
|
|
|
tags:
|
|
|
|
Name: Peering conenction for VPC 21 to VPC 22
|
|
|
|
CostCode: CC1234
|
|
|
|
Project: phoenix
|
2015-12-29 10:48:00 +01:00
|
|
|
register: vpc_peer
|
|
|
|
|
|
|
|
- name: delete a local VPC peering Connection
|
|
|
|
ec2_vpc_peer:
|
|
|
|
region: ap-southeast-2
|
|
|
|
peering_id: "{{ vpc_peer.peering_id }}"
|
|
|
|
state: absent
|
|
|
|
register: vpc_peer
|
|
|
|
|
|
|
|
# Complete example to create and accept a cross account peering connection.
|
|
|
|
- name: Create cross account VPC peering Connection
|
|
|
|
ec2_vpc_peer:
|
|
|
|
region: ap-southeast-2
|
|
|
|
vpc_id: vpc-12345678
|
2015-12-29 12:38:14 +01:00
|
|
|
peer_vpc_id: vpc-12345678
|
2015-12-29 10:48:00 +01:00
|
|
|
peer_owner_id: 123456789102
|
|
|
|
state: present
|
2016-03-28 07:07:39 +02:00
|
|
|
tags:
|
|
|
|
Name: Peering conenction for VPC 21 to VPC 22
|
|
|
|
CostCode: CC1234
|
|
|
|
Project: phoenix
|
2015-12-29 10:48:00 +01:00
|
|
|
register: vpc_peer
|
|
|
|
|
|
|
|
- name: Accept peering connection from remote account
|
|
|
|
ec2_vpc_peer:
|
|
|
|
region: ap-southeast-2
|
|
|
|
peering_id: "{{ vpc_peer.peering_id }}"
|
|
|
|
profile: bot03_profile_for_cross_account
|
|
|
|
state: accept
|
|
|
|
register: vpc_peer
|
|
|
|
|
|
|
|
# Complete example to create and reject a local peering connection.
|
|
|
|
- name: Create local account VPC peering Connection
|
|
|
|
ec2_vpc_peer:
|
|
|
|
region: ap-southeast-2
|
|
|
|
vpc_id: vpc-12345678
|
|
|
|
peer_vpc_id: vpc-87654321
|
|
|
|
state: present
|
2016-03-28 07:07:39 +02:00
|
|
|
tags:
|
|
|
|
Name: Peering conenction for VPC 21 to VPC 22
|
|
|
|
CostCode: CC1234
|
|
|
|
Project: phoenix
|
2015-12-29 10:48:00 +01:00
|
|
|
register: vpc_peer
|
|
|
|
|
|
|
|
- name: Reject a local VPC peering Connection
|
|
|
|
ec2_vpc_peer:
|
|
|
|
region: ap-southeast-2
|
|
|
|
peering_id: "{{ vpc_peer.peering_id }}"
|
|
|
|
state: reject
|
|
|
|
|
|
|
|
# Complete example to create and accept a cross account peering connection.
|
|
|
|
- name: Create cross account VPC peering Connection
|
|
|
|
ec2_vpc_peer:
|
|
|
|
region: ap-southeast-2
|
|
|
|
vpc_id: vpc-12345678
|
2015-12-29 12:38:14 +01:00
|
|
|
peer_vpc_id: vpc-12345678
|
2015-12-29 10:48:00 +01:00
|
|
|
peer_owner_id: 123456789102
|
|
|
|
state: present
|
2016-03-28 07:07:39 +02:00
|
|
|
tags:
|
|
|
|
Name: Peering conenction for VPC 21 to VPC 22
|
|
|
|
CostCode: CC1234
|
|
|
|
Project: phoenix
|
2015-12-29 10:48:00 +01:00
|
|
|
register: vpc_peer
|
|
|
|
|
|
|
|
- name: Accept a cross account VPC peering connection request
|
|
|
|
ec2_vpc_peer:
|
|
|
|
region: ap-southeast-2
|
|
|
|
peering_id: "{{ vpc_peer.peering_id }}"
|
|
|
|
profile: bot03_profile_for_cross_account
|
|
|
|
state: accept
|
2016-05-08 06:52:31 +02:00
|
|
|
tags:
|
|
|
|
Name: Peering conenction for VPC 21 to VPC 22
|
|
|
|
CostCode: CC1234
|
|
|
|
Project: phoenix
|
2015-12-29 10:48:00 +01:00
|
|
|
|
|
|
|
# Complete example to create and reject a cross account peering connection.
|
|
|
|
- name: Create cross account VPC peering Connection
|
|
|
|
ec2_vpc_peer:
|
|
|
|
region: ap-southeast-2
|
|
|
|
vpc_id: vpc-12345678
|
2015-12-29 12:38:14 +01:00
|
|
|
peer_vpc_id: vpc-12345678
|
2015-12-29 10:48:00 +01:00
|
|
|
peer_owner_id: 123456789102
|
|
|
|
state: present
|
2016-03-28 07:07:39 +02:00
|
|
|
tags:
|
|
|
|
Name: Peering conenction for VPC 21 to VPC 22
|
|
|
|
CostCode: CC1234
|
|
|
|
Project: phoenix
|
2015-12-29 10:48:00 +01:00
|
|
|
register: vpc_peer
|
|
|
|
|
|
|
|
- name: Reject a cross account VPC peering Connection
|
|
|
|
ec2_vpc_peer:
|
|
|
|
region: ap-southeast-2
|
|
|
|
peering_id: "{{ vpc_peer.peering_id }}"
|
|
|
|
profile: bot03_profile_for_cross_account
|
|
|
|
state: reject
|
|
|
|
|
|
|
|
'''
|
|
|
|
RETURN = '''
|
|
|
|
task:
|
2016-01-09 06:38:39 +01:00
|
|
|
description: The result of the create, accept, reject or delete action.
|
|
|
|
returned: success
|
|
|
|
type: dictionary
|
2015-12-29 10:48:00 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
import botocore
|
|
|
|
import boto3
|
|
|
|
HAS_BOTO3 = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_BOTO3 = False
|
2016-01-09 06:38:39 +01:00
|
|
|
|
2015-12-29 10:48:00 +01:00
|
|
|
|
2016-03-28 07:07:39 +02:00
|
|
|
def tags_changed(pcx_id, client, module):
|
|
|
|
changed = False
|
|
|
|
tags = dict()
|
|
|
|
if module.params.get('tags'):
|
|
|
|
tags = module.params.get('tags')
|
|
|
|
pcx = find_pcx_by_id(pcx_id, client, module)
|
|
|
|
if pcx['VpcPeeringConnections']:
|
|
|
|
pcx_values = [t.values() for t in pcx['VpcPeeringConnections'][0]['Tags']]
|
|
|
|
pcx_tags = [item for sublist in pcx_values for item in sublist]
|
|
|
|
tag_values = [[key, str(value)] for key, value in tags.iteritems()]
|
|
|
|
tags = [item for sublist in tag_values for item in sublist]
|
|
|
|
if sorted(pcx_tags) == sorted(tags):
|
|
|
|
changed = False
|
|
|
|
return changed
|
|
|
|
else:
|
|
|
|
delete_tags(pcx_id, client, module)
|
|
|
|
create_tags(pcx_id, client, module)
|
|
|
|
changed = True
|
|
|
|
return changed
|
|
|
|
return changed
|
|
|
|
|
|
|
|
|
2016-01-09 11:20:32 +01:00
|
|
|
def describe_peering_connections(params, client):
|
2015-12-29 10:48:00 +01:00
|
|
|
result = client.describe_vpc_peering_connections(Filters=[
|
2016-01-09 11:20:32 +01:00
|
|
|
{'Name': 'requester-vpc-info.vpc-id', 'Values': [params['VpcId']]},
|
|
|
|
{'Name': 'accepter-vpc-info.vpc-id', 'Values': [params['PeerVpcId']]}
|
2015-12-29 10:48:00 +01:00
|
|
|
])
|
|
|
|
if result['VpcPeeringConnections'] == []:
|
|
|
|
result = client.describe_vpc_peering_connections(Filters=[
|
2016-01-09 11:26:33 +01:00
|
|
|
{'Name': 'requester-vpc-info.vpc-id', 'Values': [params['PeerVpcId']]},
|
|
|
|
{'Name': 'accepter-vpc-info.vpc-id', 'Values': [params['VpcId']]}
|
2015-12-29 10:48:00 +01:00
|
|
|
])
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def is_active(peering_conn):
|
|
|
|
return peering_conn['Status']['Code'] == 'active'
|
|
|
|
|
|
|
|
|
|
|
|
def is_pending(peering_conn):
|
|
|
|
return peering_conn['Status']['Code'] == 'pending-acceptance'
|
|
|
|
|
|
|
|
|
|
|
|
def create_peer_connection(client, module):
|
|
|
|
changed = False
|
2016-01-09 06:38:39 +01:00
|
|
|
params = dict()
|
|
|
|
params['VpcId'] = module.params.get('vpc_id')
|
|
|
|
params['PeerVpcId'] = module.params.get('peer_vpc_id')
|
|
|
|
if module.params.get('peer_owner_id'):
|
|
|
|
params['PeerOwnerId'] = str(module.params.get('peer_owner_id'))
|
|
|
|
params['DryRun'] = module.check_mode
|
2016-01-09 11:20:32 +01:00
|
|
|
peering_conns = describe_peering_connections(params, client)
|
2015-12-29 10:48:00 +01:00
|
|
|
for peering_conn in peering_conns['VpcPeeringConnections']:
|
2016-03-28 07:07:39 +02:00
|
|
|
pcx_id = peering_conn['VpcPeeringConnectionId']
|
|
|
|
if tags_changed(pcx_id, client, module):
|
|
|
|
changed = True
|
2015-12-29 10:48:00 +01:00
|
|
|
if is_active(peering_conn):
|
2016-01-09 08:09:49 +01:00
|
|
|
return (changed, peering_conn['VpcPeeringConnectionId'])
|
2015-12-29 10:48:00 +01:00
|
|
|
if is_pending(peering_conn):
|
2016-01-09 08:09:49 +01:00
|
|
|
return (changed, peering_conn['VpcPeeringConnectionId'])
|
2016-01-09 11:03:30 +01:00
|
|
|
try:
|
|
|
|
peering_conn = client.create_vpc_peering_connection(**params)
|
2016-03-28 07:07:39 +02:00
|
|
|
pcx_id = peering_conn['VpcPeeringConnection']['VpcPeeringConnectionId']
|
|
|
|
if module.params.get('tags'):
|
|
|
|
create_tags(pcx_id, client, module)
|
2016-01-09 11:03:30 +01:00
|
|
|
changed = True
|
|
|
|
return (changed, peering_conn['VpcPeeringConnection']['VpcPeeringConnectionId'])
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
def peer_status(client, module):
|
|
|
|
params = dict()
|
|
|
|
params['VpcPeeringConnectionIds'] = [module.params.get('peering_id')]
|
|
|
|
vpc_peering_connection = client.describe_vpc_peering_connections(**params)
|
|
|
|
return vpc_peering_connection['VpcPeeringConnections'][0]['Status']['Code']
|
2015-12-29 10:48:00 +01:00
|
|
|
|
|
|
|
|
2016-01-09 08:13:53 +01:00
|
|
|
def accept_reject_delete(state, client, module):
|
2015-12-29 10:48:00 +01:00
|
|
|
changed = False
|
2016-01-09 06:38:39 +01:00
|
|
|
params = dict()
|
|
|
|
params['VpcPeeringConnectionId'] = module.params.get('peering_id')
|
|
|
|
params['DryRun'] = module.check_mode
|
2016-01-09 08:09:49 +01:00
|
|
|
invocations = {
|
|
|
|
'accept': client.accept_vpc_peering_connection,
|
|
|
|
'reject': client.reject_vpc_peering_connection,
|
|
|
|
'absent': client.delete_vpc_peering_connection
|
|
|
|
}
|
2016-01-09 11:03:30 +01:00
|
|
|
if state == 'absent' or peer_status(client, module) != 'active':
|
|
|
|
try:
|
|
|
|
invocations[state](**params)
|
2016-05-08 06:52:31 +02:00
|
|
|
if module.params.get('tags'):
|
|
|
|
create_tags(params['VpcPeeringConnectionId'], client, module)
|
2016-01-09 11:03:30 +01:00
|
|
|
changed = True
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
module.fail_json(msg=str(e))
|
2016-05-09 02:28:05 +02:00
|
|
|
if tags_changed(params['VpcPeeringConnectionId'], client, module):
|
|
|
|
changed = True
|
2016-01-09 08:09:49 +01:00
|
|
|
return changed, params['VpcPeeringConnectionId']
|
2015-12-29 10:48:00 +01:00
|
|
|
|
|
|
|
|
2016-03-28 07:07:39 +02:00
|
|
|
def load_tags(module):
|
|
|
|
tags = []
|
|
|
|
if module.params.get('tags'):
|
|
|
|
for name, value in module.params.get('tags').iteritems():
|
|
|
|
tags.append({'Key': name, 'Value': str(value)})
|
|
|
|
return tags
|
|
|
|
|
|
|
|
|
|
|
|
def create_tags(pcx_id, client, module):
|
|
|
|
try:
|
|
|
|
delete_tags(pcx_id, client, module)
|
|
|
|
client.create_tags(Resources=[pcx_id], Tags=load_tags(module))
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
def delete_tags(pcx_id, client, module):
|
|
|
|
try:
|
|
|
|
client.delete_tags(Resources=[pcx_id])
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
def find_pcx_by_id(pcx_id, client, module):
|
|
|
|
try:
|
|
|
|
return client.describe_vpc_peering_connections(VpcPeeringConnectionIds=[pcx_id])
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
|
2015-12-29 10:48:00 +01:00
|
|
|
def main():
|
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
|
|
|
vpc_id=dict(),
|
|
|
|
peer_vpc_id=dict(),
|
|
|
|
peering_id=dict(),
|
2016-01-09 06:38:39 +01:00
|
|
|
peer_owner_id=dict(),
|
2016-03-28 07:07:39 +02:00
|
|
|
tags=dict(required=False, type='dict'),
|
2015-12-29 10:48:00 +01:00
|
|
|
profile=dict(),
|
|
|
|
state=dict(default='present', choices=['present', 'absent', 'accept', 'reject'])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec)
|
|
|
|
|
2016-01-09 06:38:39 +01:00
|
|
|
if not HAS_BOTO3:
|
2016-05-08 06:07:21 +02:00
|
|
|
module.fail_json(msg='json, botocore and boto3 are required.')
|
2015-12-29 10:48:00 +01:00
|
|
|
state = module.params.get('state').lower()
|
|
|
|
try:
|
|
|
|
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
|
2016-05-25 20:48:16 +02:00
|
|
|
client = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_kwargs)
|
|
|
|
except botocore.exceptions.NoCredentialsError as e:
|
2015-12-29 10:48:00 +01:00
|
|
|
module.fail_json(msg="Can't authorize connection - "+str(e))
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
(changed, results) = create_peer_connection(client, module)
|
|
|
|
module.exit_json(changed=changed, peering_id=results)
|
|
|
|
else:
|
2016-01-09 08:13:53 +01:00
|
|
|
(changed, results) = accept_reject_delete(state, client, module)
|
2015-12-29 10:48:00 +01:00
|
|
|
module.exit_json(changed=changed, peering_id=results)
|
|
|
|
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.ec2 import *
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|