Browse code

Update changelog, rename vpc module to ec2_vpc.

Michael DeHaan authored on 2013/11/19 07:59:40
Showing 3 changed files
... ...
@@ -18,7 +18,8 @@ Highlighted new features:
18 18
 * The name of each role is now shown before each task if roles are being used
19 19
 * Adds a "var=" option to the debug module for debugging variable data.  "debug: var=hostvars['hostname']" and "debug: var=foo" are all valid syntax.
20 20
 * Variables in {{ format }} can be used as references even if they are structured data
21
-* 
21
+* Can force binding of accelerate to ipv6 ports.
22
+
22 23
 New modules and plugins:
23 24
 
24 25
 * cloud:ec2_eip -- manage AWS elastic IPs
... ...
@@ -30,6 +31,7 @@ New modules and plugins:
30 30
 * system: modprobe -- manage kernel modules on systems that support modprobe/rmmod
31 31
 * system: open_iscsi -- manage targets on an initiator using open-iscsi
32 32
 * utilities: include_vars -- dynamically load variables based on conditions.
33
+* cloud: ec2_vpc -- manage ec2 virtual private clouds
33 34
 
34 35
 Plugins:
35 36
 
... ...
@@ -63,6 +65,10 @@ Misc changes:
63 63
 * Fixes for IPv6 addresses in inventory text files
64 64
 * name of executable can be passed to pip/gem etc, for installing under *different* interpreters
65 65
 * copy of ./hacking/env-setup added for fish users, ./hacking/env-setup.fish
66
+* file module more tolerant of non-absolute paths in softlinks.
67
+* miscellaneous fixes/upgrades to async polling logic.
68
+* conditions on roles now pass to dependent roles
69
+* 
66 70
 
67 71
 1.3.4 "Top of the World" (reprise) - October 29, 2013
68 72
 
69 73
new file mode 100644
... ...
@@ -0,0 +1,534 @@
0
+#!/usr/bin/python
1
+# This file is part of Ansible
2
+#
3
+# Ansible is free software: you can redistribute it and/or modify
4
+# it under the terms of the GNU General Public License as published by
5
+# the Free Software Foundation, either version 3 of the License, or
6
+# (at your option) any later version.
7
+#
8
+# Ansible is distributed in the hope that it will be useful,
9
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+# GNU General Public License for more details.
12
+#
13
+# You should have received a copy of the GNU General Public License
14
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
15
+
16
+DOCUMENTATION = '''
17
+---
18
+module: ec2_vpc 
19
+short_description: configure AWS virtual private clouds
20
+description:
21
+    - Create or terminates AWS virtual private clouds.  This module has a dependency on python-boto.
22
+version_added: "1.4"
23
+options:
24
+  cidr_block:
25
+    description:
26
+      - "The cidr block representing the VPC, e.g. 10.0.0.0/16"
27
+    required: false, unless state=present
28
+  dns_support:
29
+    description:
30
+      - toggles the "Enable DNS resolution" flag
31
+    required: false
32
+    default: "yes"
33
+    choices: [ "yes", "no" ]
34
+  dns_hostnames:
35
+    description:
36
+      - toggles the "Enable DNS hostname support for instances" flag
37
+    required: false
38
+    default: "yes"
39
+    choices: [ "yes", "no" ]
40
+  subnets:
41
+    description:
42
+      - "A dictionary array of subnets to add of the form: { cidr: ..., az: ... }. Where az is the desired availability zone of the subnet, but it is not required. All VPC subnets not in this list will be removed."
43
+    required: false
44
+    default: null
45
+    aliases: []
46
+  vpc_id:
47
+    description:
48
+      - A VPC id to terminate when state=absent
49
+    required: false
50
+    default: null
51
+    aliases: []
52
+  internet_gateway:
53
+    description:
54
+      - Toggle whether there should be an Internet gateway attached to the VPC
55
+    required: false
56
+    default: "no"
57
+    choices: [ "yes", "no" ]
58
+    aliases: []
59
+  route_tables:
60
+    description:
61
+      - "A dictionary array of route tables to add of the form: { subnets: [172.22.2.0/24, 172.22.3.0/24,], routes: [{ dest: 0.0.0.0/0, gw: igw},] }. Where the subnets list is those subnets the route table should be associated with, and the routes list is a list of routes to be in the table.  The special keyword for the gw of igw specifies that you should the route should go through the internet gateway attached to the VPC. gw also accepts instance-ids in addition igw. This module is currently unable to affect the 'main' route table due to some limitations in boto, so you must explicitly define the associated subnets or they will be attached to the main table implicitly."
62
+    required: false
63
+    default: null
64
+    aliases: []
65
+  wait:
66
+    description:
67
+      - wait for the VPC to be in state 'available' before returning
68
+    required: false
69
+    default: "no"
70
+    choices: [ "yes", "no" ]
71
+    aliases: []
72
+  wait_timeout:
73
+    description:
74
+      - how long before wait gives up, in seconds
75
+    default: 300
76
+    aliases: []
77
+  state:
78
+    description:
79
+      - Create or terminate the VPC
80
+    required: true
81
+    default: present
82
+    aliases: []
83
+  region:
84
+    description:
85
+      - region in which the resource exists. 
86
+    required: false
87
+    default: null
88
+    aliases: ['aws_region', 'ec2_region']
89
+  aws_secret_key:
90
+    description:
91
+      - AWS secret key. If not set then the value of the AWS_SECRET_KEY environment variable is used. 
92
+    required: false
93
+    default: None
94
+    aliases: ['ec2_secret_key', 'secret_key' ]
95
+  aws_access_key:
96
+    description:
97
+      - AWS access key. If not set then the value of the AWS_ACCESS_KEY environment variable is used.
98
+    required: false
99
+    default: None
100
+    aliases: ['ec2_access_key', 'access_key' ]
101
+requirements: [ "boto" ]
102
+author: Carson Gee
103
+'''
104
+
105
+EXAMPLES = '''
106
+# Note: None of these examples set aws_access_key, aws_secret_key, or region.
107
+# It is assumed that their matching environment variables are set.
108
+
109
+# Basic creation example:
110
+      local_action:
111
+        module: vpc
112
+        state: present
113
+        cidr_block: 172.23.0.0/16
114
+        region: us-west-2
115
+# Full creation example with subnets and optional availability zones.
116
+# The absence or presense of subnets deletes or creates them respectively.
117
+      local_action:
118
+        module: vpc
119
+        state: present
120
+        cidr_block: 172.22.0.0/16
121
+        subnets: 
122
+          - cidr: 172.22.1.0/24
123
+            az: us-west-2c
124
+          - cidr: 172.22.2.0/24
125
+            az: us-west-2b
126
+          - cidr: 172.22.3.0/24
127
+            az: us-west-2a
128
+        internet_gateway: True
129
+        route_tables:
130
+          - subnets: 
131
+              - 172.22.2.0/24
132
+              - 172.22.3.0/24
133
+            routes: 
134
+              - dest: 0.0.0.0/0
135
+                gw: igw
136
+          - subnets:
137
+              - 172.22.1.0/24
138
+            routes:
139
+              - dest: 0.0.0.0/0
140
+                gw: igw
141
+        region: us-west-2
142
+      register: vpc
143
+
144
+# Removal of a VPC by id
145
+      local_action:
146
+        module: vpc
147
+        state: absent
148
+        vpc_id: vpc-aaaaaaa 
149
+        region: us-west-2  
150
+If you have added elements not managed by this module, e.g. instances, NATs, etc then
151
+the delete will fail until those dependencies are removed.
152
+'''
153
+
154
+
155
+import sys
156
+import time
157
+
158
+try:
159
+    import boto.ec2
160
+    import boto.vpc
161
+    from boto.exception import EC2ResponseError
162
+except ImportError:
163
+    print "failed=True msg='boto required for this module'"
164
+    sys.exit(1)
165
+
166
+AWS_REGIONS = ['ap-northeast-1',
167
+               'ap-southeast-1',
168
+               'ap-southeast-2',
169
+               'eu-west-1',
170
+               'sa-east-1',
171
+               'us-east-1',
172
+               'us-west-1',
173
+               'us-west-2']
174
+
175
+def get_vpc_info(vpc):
176
+    """
177
+    Retrieves vpc information from an instance
178
+    ID and returns it as a dictionary
179
+    """
180
+
181
+    return({
182
+        'id': vpc.id,
183
+        'cidr_block': vpc.cidr_block,
184
+        'dhcp_options_id': vpc.dhcp_options_id,
185
+        'region': vpc.region.name,
186
+        'state': vpc.state,
187
+    })
188
+
189
+def create_vpc(module, vpc_conn):
190
+    """
191
+    Creates a new VPC
192
+
193
+    module : AnsibleModule object
194
+    vpc_conn: authenticated VPCConnection connection object
195
+
196
+    Returns:
197
+        A dictionary with information
198
+        about the VPC and subnets that were launched 
199
+    """
200
+    
201
+    id = module.params.get('id')
202
+    cidr_block = module.params.get('cidr_block')
203
+    dns_support = module.params.get('dns_support')
204
+    dns_hostnames = module.params.get('dns_hostnames')
205
+    subnets = module.params.get('subnets')
206
+    internet_gateway = module.params.get('internet_gateway')
207
+    route_tables = module.params.get('route_tables')
208
+    wait = module.params.get('wait')
209
+    wait_timeout = int(module.params.get('wait_timeout'))
210
+    changed = False
211
+
212
+    # Check for existing VPC by cidr_block or id
213
+    if id != None:
214
+        filter_dict = {'vpc-id':id, 'state': 'available',}
215
+        previous_vpcs = vpc_conn.get_all_vpcs(None, filter_dict)
216
+    else:
217
+        filter_dict = {'cidr': cidr_block, 'state': 'available'}
218
+        previous_vpcs = vpc_conn.get_all_vpcs(None, filter_dict)
219
+
220
+    if len(previous_vpcs) > 1:
221
+        module.fail_json(msg='EC2 returned more than one VPC, aborting')
222
+
223
+    if len(previous_vpcs) == 1:
224
+        changed = False
225
+        vpc = previous_vpcs[0]
226
+    else:
227
+        changed = True
228
+        try:
229
+            vpc = vpc_conn.create_vpc(cidr_block)
230
+            # wait here until the vpc is available
231
+            pending = True
232
+            wait_timeout = time.time() + wait_timeout
233
+            while wait and wait_timeout > time.time() and pending:
234
+                pvpc = vpc_conn.get_all_vpcs(vpc.id)
235
+                if pvpc.state == "available":
236
+                    pending = False
237
+                time.sleep(5)
238
+            if wait and wait_timeout <= time.time():
239
+                # waiting took too long
240
+                module.fail_json(msg = "wait for vpc availability timeout on %s" % time.asctime())
241
+
242
+        except boto.exception.BotoServerError, e:
243
+            module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
244
+
245
+    # Done with base VPC, now change to attributes and features.
246
+    
247
+
248
+    # boto doesn't appear to have a way to determine the existing
249
+    # value of the dns attributes, so we just set them.
250
+    # It also must be done one at a time.
251
+    vpc_conn.modify_vpc_attribute(vpc.id, enable_dns_support=dns_support)
252
+    vpc_conn.modify_vpc_attribute(vpc.id, enable_dns_hostnames=dns_hostnames)
253
+
254
+
255
+    # Process all subnet properties
256
+    if not isinstance(subnets, list):
257
+        module.fail_json(msg='subnets needs to be a list of cidr blocks')
258
+    
259
+    current_subnets = vpc_conn.get_all_subnets(filters={ 'vpc_id': vpc.id })
260
+    # First add all new subnets
261
+    for subnet in subnets:
262
+        add_subnet = True
263
+        for csn in current_subnets:
264
+            if subnet['cidr'] == csn.cidr_block:
265
+                add_subnet = False
266
+        if add_subnet:
267
+            try:
268
+                vpc_conn.create_subnet(vpc.id, subnet['cidr'], subnet.get('az', None))
269
+                changed = True
270
+            except EC2ResponseError as e:
271
+                module.fail_json(msg='Unable to create subnet {0}, error: {1}'.format(subnet['cidr'], e))
272
+    # Now delete all absent subnets
273
+    for csubnet in current_subnets:
274
+        delete_subnet = True
275
+        for subnet in subnets:
276
+            if csubnet.cidr_block == subnet['cidr']:
277
+                delete_subnet = False
278
+        if delete_subnet:
279
+            try:
280
+                vpc_conn.delete_subnet(csubnet.id)
281
+                changed = True
282
+            except EC2ResponseError as e:
283
+                module.fail_json(msg='Unable to delete subnet {0}, error: {1}'.format(csubnet.cidr_block, e))
284
+
285
+    # Handle Internet gateway (create/delete igw)
286
+    igw = None
287
+    igws = vpc_conn.get_all_internet_gateways(filters={'attachment.vpc-id': vpc.id})
288
+    if len(igws) > 1: 
289
+        module.fail_json(msg='EC2 returned more than one Internet Gateway for id %s, aborting' % vpc.id)
290
+    if internet_gateway:
291
+        if len(igws) != 1:
292
+            try:
293
+                igw = vpc_conn.create_internet_gateway()
294
+                vpc_conn.attach_internet_gateway(igw.id, vpc.id)
295
+                changed = True
296
+            except EC2ResponseError as e:
297
+                module.fail_json(msg='Unable to create Internet Gateway, error: {0}'.format(e))
298
+        else:
299
+            # Set igw variable to the current igw instance for use in route tables.
300
+            igw = igws[0]
301
+    else:
302
+        if len(igws) > 0:
303
+            try:
304
+                vpc_conn.detach_internet_gateway(igws[0].id, vpc.id)
305
+                vpc_conn.delete_internet_gateway(igws[0].id)
306
+                changed = True
307
+            except EC2ResponseError as e:
308
+                module.fail_json(msg='Unable to delete Internet Gateway, error: {0}'.format(e))
309
+
310
+    # Handle route tables - this may be worth splitting into a
311
+    # different module but should work fine here. The strategy to stay
312
+    # indempotent is to basically build all the route tables as
313
+    # defined, track the route table ids, and then run through the
314
+    # remote list of route tables and delete any that we didn't
315
+    # create.  This shouldn't interupt traffic in theory, but is the
316
+    # only way to really work with route tables over time that I can
317
+    # think of without using painful aws ids.  Hopefully boto will add
318
+    # the replace-route-table API to make this smoother and
319
+    # allow control of the 'main' routing table.
320
+    if not isinstance(route_tables, list):
321
+        module.fail_json(msg='route tables need to be a list of dictionaries')
322
+    
323
+    # Work through each route table and update/create to match dictionary array
324
+    all_route_tables = []
325
+    for rt in route_tables:
326
+        try:
327
+            new_rt = vpc_conn.create_route_table(vpc.id)
328
+            for route in rt['routes']:
329
+                r_gateway = route['gw']
330
+                if r_gateway == 'igw':
331
+                    if not internet_gateway:
332
+                        module.fail_json(
333
+                            msg='You asked for an Internet Gateway ' \
334
+                            '(igw) route, but you have no Internet Gateway'
335
+                        )
336
+                    r_gateway = igw.id
337
+                vpc_conn.create_route(new_rt.id, route['dest'], r_gateway)
338
+
339
+            # Associate with subnets
340
+            for sn in rt['subnets']:
341
+                rsn = vpc_conn.get_all_subnets(filters={'cidr': sn })
342
+                if len(rsn) != 1:
343
+                    module.fail_json(
344
+                        msg='The subnet {0} to associate with route_table {1} ' \
345
+                        'does not exist, aborting'.format(sn, rt)
346
+                    )
347
+                rsn = rsn[0]
348
+
349
+                # Disassociate then associate since we don't have replace
350
+                old_rt = vpc_conn.get_all_route_tables(
351
+                    filters={'association.subnet_id': rsn.id}
352
+                )
353
+                if len(old_rt) == 1:
354
+                    old_rt = old_rt[0]
355
+                    association_id = None
356
+                    for a in old_rt.associations:
357
+                        if a.subnet_id == rsn.id:
358
+                            association_id = a.id
359
+                    vpc_conn.disassociate_route_table(association_id)
360
+
361
+                vpc_conn.associate_route_table(new_rt.id, rsn.id)
362
+
363
+            all_route_tables.append(new_rt)
364
+        except EC2ResponseError as e:
365
+            module.fail_json(
366
+                msg='Unable to create and associate route table {0}, error: ' \
367
+                '{1}'.format(rt, e)
368
+            )
369
+        
370
+
371
+    # Now that we are good to go on our new route tables, delete the
372
+    # old ones except the 'main' route table as boto can't set the main
373
+    # table yet.
374
+    all_rts = vpc_conn.get_all_route_tables(filters={'vpc-id': vpc.id})
375
+    for rt in all_rts:
376
+        delete_rt = True
377
+        for newrt in all_route_tables:
378
+            if newrt.id == rt.id:
379
+                delete_rt = False
380
+        if delete_rt:
381
+            rta = rt.associations
382
+            is_main = False
383
+            for a in rta:
384
+                if a.main:
385
+                    is_main = True
386
+            try:
387
+                if not is_main:
388
+                    vpc_conn.delete_route_table(rt.id)
389
+            except EC2ResponseError as e:
390
+                module.fail_json(msg='Unable to delete old route table {0}, error: {1}'.format(rt.id, e))
391
+
392
+    vpc_dict = get_vpc_info(vpc)
393
+    created_vpc_id = vpc.id
394
+    returned_subnets = []
395
+    current_subnets = vpc_conn.get_all_subnets(filters={ 'vpc_id': vpc.id })
396
+    for sn in current_subnets:
397
+        returned_subnets.append({
398
+            'cidr': sn.cidr_block, 
399
+            'az': sn.availability_zone,
400
+            'id': sn.id,
401
+        })
402
+
403
+
404
+    return (vpc_dict, created_vpc_id, returned_subnets, changed)
405
+
406
+def terminate_vpc(module, vpc_conn, vpc_id=None, cidr=None):
407
+    """
408
+    Terminates a VPC
409
+
410
+    module: Ansible module object
411
+    vpc_conn: authenticated VPCConnection connection object
412
+    vpc_id: a vpc id to terminate
413
+    cidr: The cidr block of the VPC - can be used in lieu of an ID
414
+
415
+    Returns a dictionary of VPC information
416
+    about the VPC terminated.
417
+
418
+    If the VPC to be terminated is available
419
+    "changed" will be set to True.
420
+
421
+    """
422
+    vpc_dict = {}
423
+    terminated_vpc_id = ''
424
+    changed = False
425
+
426
+    if vpc_id == None and cidr == None:
427
+        module.fail_json(
428
+            msg='You must either specify a vpc id or a cidr '\
429
+            'block to terminate a VPC, aborting'
430
+        )
431
+    if vpc_id is not None:         
432
+        vpc_rs = vpc_conn.get_all_vpcs(vpc_id)
433
+    else:
434
+        vpc_rs = vpc_conn.get_all_vpcs(filters={'cidr': cidr})
435
+    if len(vpc_rs) > 1:
436
+        module.fail_json(
437
+            msg='EC2 returned more than one VPC for id {0} ' \
438
+            'or cidr {1}, aborting'.format(vpc_id,vidr)
439
+        )
440
+    if len(vpc_rs) == 1:
441
+        vpc = vpc_rs[0]
442
+        if vpc.state == 'available':
443
+            terminated_vpc_id=vpc.id
444
+            vpc_dict=get_vpc_info(vpc)
445
+            try:
446
+                subnets = vpc_conn.get_all_subnets(filters={'vpc_id': vpc.id})
447
+                for sn in subnets:
448
+                    vpc_conn.delete_subnet(sn.id)
449
+
450
+                igws = vpc_conn.get_all_internet_gateways(
451
+                    filters={'attachment.vpc-id': vpc.id}
452
+                )
453
+                for igw in igws:
454
+                    vpc_conn.detach_internet_gateway(igw.id, vpc.id)
455
+                    vpc_conn.delete_internet_gateway(igw.id)
456
+
457
+                rts = vpc_conn.get_all_route_tables(filters={'vpc_id': vpc.id})
458
+                for rt in rts:
459
+                    rta = rt.associations
460
+                    is_main = False
461
+                    for a in rta:
462
+                        if a.main:
463
+                            is_main = True
464
+                    if not is_main:
465
+                        vpc_conn.delete_route_table(rt.id)
466
+
467
+                vpc_conn.delete_vpc(vpc.id)
468
+            except EC2ResponseError as e:
469
+                module.fail_json(
470
+                    msg='Unable to delete VPC {0}, error: {1}'.format(vpc.id, e)
471
+                )
472
+            changed = True
473
+
474
+    return (changed, vpc_dict, terminated_vpc_id)
475
+
476
+
477
+def main():
478
+    module = AnsibleModule(
479
+        argument_spec = dict(
480
+            cidr_block = dict(),
481
+            wait = dict(choices=BOOLEANS, default=False),
482
+            wait_timeout = dict(default=300),
483
+            dns_support = dict(choices=BOOLEANS, default=True),
484
+            dns_hostnames = dict(choices=BOOLEANS, default=True),
485
+            subnets = dict(type='list'),
486
+            vpc_id = dict(),
487
+            internet_gateway = dict(choices=BOOLEANS, default=False),
488
+            route_tables = dict(type='list'),
489
+            region = dict(aliases=['aws_region', 'ec2_region'], choices=AWS_REGIONS),
490
+            state = dict(choices=['present', 'absent'], default='present'),
491
+            aws_secret_key = dict(aliases=['ec2_secret_key', 'secret_key'], no_log=True),
492
+            aws_access_key = dict(aliases=['ec2_access_key', 'access_key']),
493
+        )
494
+    )
495
+
496
+    state = module.params.get('state')
497
+
498
+    ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module)
499
+   
500
+    # If we have a region specified, connect to its endpoint.
501
+    if region: 
502
+        try:
503
+            vpc_conn = boto.vpc.connect_to_region(
504
+                region, 
505
+                aws_access_key_id=aws_access_key,
506
+                aws_secret_access_key=aws_secret_key
507
+            )
508
+        except boto.exception.NoAuthHandlerFound, e:
509
+            module.fail_json(msg = str(e))
510
+    else:
511
+        module.fail_json(msg="region must be specified")
512
+    
513
+    if module.params.get('state') == 'absent':
514
+        vpc_id = module.params.get('vpc_id')
515
+        cidr = module.params.get('cidr_block')
516
+        if vpc_id == None and cidr == None:
517
+            module.fail_json(
518
+                msg='You must either specify a vpc id or a cidr '\
519
+                'block to terminate a VPC, aborting'
520
+            )
521
+        (changed, vpc_dict, new_vpc_id) = terminate_vpc(module, vpc_conn, vpc_id, cidr)
522
+        subnets_changed = None
523
+    elif module.params.get('state') == 'present':
524
+        # Changed is always set to true when provisioning a new VPC
525
+        (vpc_dict, new_vpc_id, subnets_changed, changed) = create_vpc(module, vpc_conn)
526
+
527
+    module.exit_json(changed=changed, vpc_id=new_vpc_id, vpc=vpc_dict, subnets=subnets_changed)
528
+
529
+# import module snippets
530
+from ansible.module_utils.basic import *
531
+from ansible.module_utils.ec2 import *
532
+
533
+main()
0 534
deleted file mode 100644
... ...
@@ -1,534 +0,0 @@
1
-#!/usr/bin/python
2
-# This file is part of Ansible
3
-#
4
-# Ansible is free software: you can redistribute it and/or modify
5
-# it under the terms of the GNU General Public License as published by
6
-# the Free Software Foundation, either version 3 of the License, or
7
-# (at your option) any later version.
8
-#
9
-# Ansible is distributed in the hope that it will be useful,
10
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
-# GNU General Public License for more details.
13
-#
14
-# You should have received a copy of the GNU General Public License
15
-# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
16
-
17
-DOCUMENTATION = '''
18
-module: vpc 
19
-short_description: configure AWS virtual private clouds
20
-description:
21
-    - Create or terminates AWS virtual private clouds.  This module has a dependency on python-boto.
22
-version_added: "1.4"
23
-options:
24
-  cidr_block:
25
-    description:
26
-      - "The cidr block representing the VPC, e.g. 10.0.0.0/16"
27
-    required: false, unless state=present
28
-  dns_support:
29
-    description:
30
-      - toggles the "Enable DNS resolution" flag
31
-    required: false
32
-    default: "yes"
33
-    choices: [ "yes", "no" ]
34
-  dns_hostnames:
35
-    description:
36
-      - toggles the "Enable DNS hostname support for instances" flag
37
-    required: false
38
-    default: "yes"
39
-    choices: [ "yes", "no" ]
40
-  subnets:
41
-    description:
42
-      - "A dictionary array of subnets to add of the form: { cidr: ..., az: ... }. Where az is the desired availability zone of the subnet, but it is not required. All VPC subnets not in this list will be removed."
43
-    required: false
44
-    default: null
45
-    aliases: []
46
-  vpc_id:
47
-    description:
48
-      - A VPC id to terminate when state=absent
49
-    required: false
50
-    default: null
51
-    aliases: []
52
-  internet_gateway:
53
-    description:
54
-      - Toggle whether there should be an Internet gateway attached to the VPC
55
-    required: false
56
-    default: "no"
57
-    choices: [ "yes", "no" ]
58
-    aliases: []
59
-  route_tables:
60
-    description:
61
-      - "A dictionary array of route tables to add of the form: { subnets: [172.22.2.0/24, 172.22.3.0/24,], routes: [{ dest: 0.0.0.0/0, gw: igw},] }. Where the subnets list is those subnets the route table should be associated with, and the routes list is a list of routes to be in the table.  The special keyword for the gw of igw specifies that you should the route should go through the internet gateway attached to the VPC. gw also accepts instance-ids in addition igw. This module is currently unable to affect the 'main' route table due to some limitations in boto, so you must explicitly define the associated subnets or they will be attached to the main table implicitly."
62
-    required: false
63
-    default: null
64
-    aliases: []
65
-  wait:
66
-    description:
67
-      - wait for the VPC to be in state 'available' before returning
68
-    required: false
69
-    default: "no"
70
-    choices: [ "yes", "no" ]
71
-    aliases: []
72
-  wait_timeout:
73
-    description:
74
-      - how long before wait gives up, in seconds
75
-    default: 300
76
-    aliases: []
77
-  state:
78
-    description:
79
-      - Create or terminate the VPC
80
-    required: true
81
-    default: present
82
-    aliases: []
83
-  region:
84
-    description:
85
-      - region in which the resource exists. 
86
-    required: false
87
-    default: null
88
-    aliases: ['aws_region', 'ec2_region']
89
-  aws_secret_key:
90
-    description:
91
-      - AWS secret key. If not set then the value of the AWS_SECRET_KEY environment variable is used. 
92
-    required: false
93
-    default: None
94
-    aliases: ['ec2_secret_key', 'secret_key' ]
95
-  aws_access_key:
96
-    description:
97
-      - AWS access key. If not set then the value of the AWS_ACCESS_KEY environment variable is used.
98
-    required: false
99
-    default: None
100
-    aliases: ['ec2_access_key', 'access_key' ]
101
-requirements: [ "boto" ]
102
-author: Carson Gee
103
-'''
104
-
105
-EXAMPLES = '''
106
-# Note: None of these examples set aws_access_key, aws_secret_key, or region.
107
-# It is assumed that their matching environment variables are set.
108
-
109
-# Basic creation example:
110
-      local_action:
111
-        module: vpc
112
-        state: present
113
-        cidr_block: 172.23.0.0/16
114
-        region: us-west-2
115
-# Full creation example with subnets and optional availability zones.
116
-# The absence or presense of subnets deletes or creates them respectively.
117
-      local_action:
118
-        module: vpc
119
-        state: present
120
-        cidr_block: 172.22.0.0/16
121
-        subnets: 
122
-          - cidr: 172.22.1.0/24
123
-            az: us-west-2c
124
-          - cidr: 172.22.2.0/24
125
-            az: us-west-2b
126
-          - cidr: 172.22.3.0/24
127
-            az: us-west-2a
128
-        internet_gateway: True
129
-        route_tables:
130
-          - subnets: 
131
-              - 172.22.2.0/24
132
-              - 172.22.3.0/24
133
-            routes: 
134
-              - dest: 0.0.0.0/0
135
-                gw: igw
136
-          - subnets:
137
-              - 172.22.1.0/24
138
-            routes:
139
-              - dest: 0.0.0.0/0
140
-                gw: igw
141
-        region: us-west-2
142
-      register: vpc
143
-
144
-# Removal of a VPC by id
145
-      local_action:
146
-        module: vpc
147
-        state: absent
148
-        vpc_id: vpc-aaaaaaa 
149
-        region: us-west-2  
150
-If you have added elements not managed by this module, e.g. instances, NATs, etc then
151
-the delete will fail until those dependencies are removed.
152
-'''
153
-
154
-
155
-import sys
156
-import time
157
-
158
-try:
159
-    import boto.ec2
160
-    import boto.vpc
161
-    from boto.exception import EC2ResponseError
162
-except ImportError:
163
-    print "failed=True msg='boto required for this module'"
164
-    sys.exit(1)
165
-
166
-AWS_REGIONS = ['ap-northeast-1',
167
-               'ap-southeast-1',
168
-               'ap-southeast-2',
169
-               'eu-west-1',
170
-               'sa-east-1',
171
-               'us-east-1',
172
-               'us-west-1',
173
-               'us-west-2']
174
-
175
-def get_vpc_info(vpc):
176
-    """
177
-    Retrieves vpc information from an instance
178
-    ID and returns it as a dictionary
179
-    """
180
-
181
-    return({
182
-        'id': vpc.id,
183
-        'cidr_block': vpc.cidr_block,
184
-        'dhcp_options_id': vpc.dhcp_options_id,
185
-        'region': vpc.region.name,
186
-        'state': vpc.state,
187
-    })
188
-
189
-def create_vpc(module, vpc_conn):
190
-    """
191
-    Creates a new VPC
192
-
193
-    module : AnsibleModule object
194
-    vpc_conn: authenticated VPCConnection connection object
195
-
196
-    Returns:
197
-        A dictionary with information
198
-        about the VPC and subnets that were launched 
199
-    """
200
-    
201
-    id = module.params.get('id')
202
-    cidr_block = module.params.get('cidr_block')
203
-    dns_support = module.params.get('dns_support')
204
-    dns_hostnames = module.params.get('dns_hostnames')
205
-    subnets = module.params.get('subnets')
206
-    internet_gateway = module.params.get('internet_gateway')
207
-    route_tables = module.params.get('route_tables')
208
-    wait = module.params.get('wait')
209
-    wait_timeout = int(module.params.get('wait_timeout'))
210
-    changed = False
211
-
212
-    # Check for existing VPC by cidr_block or id
213
-    if id != None:
214
-        filter_dict = {'vpc-id':id, 'state': 'available',}
215
-        previous_vpcs = vpc_conn.get_all_vpcs(None, filter_dict)
216
-    else:
217
-        filter_dict = {'cidr': cidr_block, 'state': 'available'}
218
-        previous_vpcs = vpc_conn.get_all_vpcs(None, filter_dict)
219
-
220
-    if len(previous_vpcs) > 1:
221
-        module.fail_json(msg='EC2 returned more than one VPC, aborting')
222
-
223
-    if len(previous_vpcs) == 1:
224
-        changed = False
225
-        vpc = previous_vpcs[0]
226
-    else:
227
-        changed = True
228
-        try:
229
-            vpc = vpc_conn.create_vpc(cidr_block)
230
-            # wait here until the vpc is available
231
-            pending = True
232
-            wait_timeout = time.time() + wait_timeout
233
-            while wait and wait_timeout > time.time() and pending:
234
-                pvpc = vpc_conn.get_all_vpcs(vpc.id)
235
-                if pvpc.state == "available":
236
-                    pending = False
237
-                time.sleep(5)
238
-            if wait and wait_timeout <= time.time():
239
-                # waiting took too long
240
-                module.fail_json(msg = "wait for vpc availability timeout on %s" % time.asctime())
241
-
242
-        except boto.exception.BotoServerError, e:
243
-            module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
244
-
245
-    # Done with base VPC, now change to attributes and features.
246
-    
247
-
248
-    # boto doesn't appear to have a way to determine the existing
249
-    # value of the dns attributes, so we just set them.
250
-    # It also must be done one at a time.
251
-    vpc_conn.modify_vpc_attribute(vpc.id, enable_dns_support=dns_support)
252
-    vpc_conn.modify_vpc_attribute(vpc.id, enable_dns_hostnames=dns_hostnames)
253
-
254
-
255
-    # Process all subnet properties
256
-    if not isinstance(subnets, list):
257
-        module.fail_json(msg='subnets needs to be a list of cidr blocks')
258
-    
259
-    current_subnets = vpc_conn.get_all_subnets(filters={ 'vpc_id': vpc.id })
260
-    # First add all new subnets
261
-    for subnet in subnets:
262
-        add_subnet = True
263
-        for csn in current_subnets:
264
-            if subnet['cidr'] == csn.cidr_block:
265
-                add_subnet = False
266
-        if add_subnet:
267
-            try:
268
-                vpc_conn.create_subnet(vpc.id, subnet['cidr'], subnet.get('az', None))
269
-                changed = True
270
-            except EC2ResponseError as e:
271
-                module.fail_json(msg='Unable to create subnet {0}, error: {1}'.format(subnet['cidr'], e))
272
-    # Now delete all absent subnets
273
-    for csubnet in current_subnets:
274
-        delete_subnet = True
275
-        for subnet in subnets:
276
-            if csubnet.cidr_block == subnet['cidr']:
277
-                delete_subnet = False
278
-        if delete_subnet:
279
-            try:
280
-                vpc_conn.delete_subnet(csubnet.id)
281
-                changed = True
282
-            except EC2ResponseError as e:
283
-                module.fail_json(msg='Unable to delete subnet {0}, error: {1}'.format(csubnet.cidr_block, e))
284
-
285
-    # Handle Internet gateway (create/delete igw)
286
-    igw = None
287
-    igws = vpc_conn.get_all_internet_gateways(filters={'attachment.vpc-id': vpc.id})
288
-    if len(igws) > 1: 
289
-        module.fail_json(msg='EC2 returned more than one Internet Gateway for id %s, aborting' % vpc.id)
290
-    if internet_gateway:
291
-        if len(igws) != 1:
292
-            try:
293
-                igw = vpc_conn.create_internet_gateway()
294
-                vpc_conn.attach_internet_gateway(igw.id, vpc.id)
295
-                changed = True
296
-            except EC2ResponseError as e:
297
-                module.fail_json(msg='Unable to create Internet Gateway, error: {0}'.format(e))
298
-        else:
299
-            # Set igw variable to the current igw instance for use in route tables.
300
-            igw = igws[0]
301
-    else:
302
-        if len(igws) > 0:
303
-            try:
304
-                vpc_conn.detach_internet_gateway(igws[0].id, vpc.id)
305
-                vpc_conn.delete_internet_gateway(igws[0].id)
306
-                changed = True
307
-            except EC2ResponseError as e:
308
-                module.fail_json(msg='Unable to delete Internet Gateway, error: {0}'.format(e))
309
-
310
-    # Handle route tables - this may be worth splitting into a
311
-    # different module but should work fine here. The strategy to stay
312
-    # indempotent is to basically build all the route tables as
313
-    # defined, track the route table ids, and then run through the
314
-    # remote list of route tables and delete any that we didn't
315
-    # create.  This shouldn't interupt traffic in theory, but is the
316
-    # only way to really work with route tables over time that I can
317
-    # think of without using painful aws ids.  Hopefully boto will add
318
-    # the replace-route-table API to make this smoother and
319
-    # allow control of the 'main' routing table.
320
-    if not isinstance(route_tables, list):
321
-        module.fail_json(msg='route tables need to be a list of dictionaries')
322
-    
323
-    # Work through each route table and update/create to match dictionary array
324
-    all_route_tables = []
325
-    for rt in route_tables:
326
-        try:
327
-            new_rt = vpc_conn.create_route_table(vpc.id)
328
-            for route in rt['routes']:
329
-                r_gateway = route['gw']
330
-                if r_gateway == 'igw':
331
-                    if not internet_gateway:
332
-                        module.fail_json(
333
-                            msg='You asked for an Internet Gateway ' \
334
-                            '(igw) route, but you have no Internet Gateway'
335
-                        )
336
-                    r_gateway = igw.id
337
-                vpc_conn.create_route(new_rt.id, route['dest'], r_gateway)
338
-
339
-            # Associate with subnets
340
-            for sn in rt['subnets']:
341
-                rsn = vpc_conn.get_all_subnets(filters={'cidr': sn })
342
-                if len(rsn) != 1:
343
-                    module.fail_json(
344
-                        msg='The subnet {0} to associate with route_table {1} ' \
345
-                        'does not exist, aborting'.format(sn, rt)
346
-                    )
347
-                rsn = rsn[0]
348
-
349
-                # Disassociate then associate since we don't have replace
350
-                old_rt = vpc_conn.get_all_route_tables(
351
-                    filters={'association.subnet_id': rsn.id}
352
-                )
353
-                if len(old_rt) == 1:
354
-                    old_rt = old_rt[0]
355
-                    association_id = None
356
-                    for a in old_rt.associations:
357
-                        if a.subnet_id == rsn.id:
358
-                            association_id = a.id
359
-                    vpc_conn.disassociate_route_table(association_id)
360
-
361
-                vpc_conn.associate_route_table(new_rt.id, rsn.id)
362
-
363
-            all_route_tables.append(new_rt)
364
-        except EC2ResponseError as e:
365
-            module.fail_json(
366
-                msg='Unable to create and associate route table {0}, error: ' \
367
-                '{1}'.format(rt, e)
368
-            )
369
-        
370
-
371
-    # Now that we are good to go on our new route tables, delete the
372
-    # old ones except the 'main' route table as boto can't set the main
373
-    # table yet.
374
-    all_rts = vpc_conn.get_all_route_tables(filters={'vpc-id': vpc.id})
375
-    for rt in all_rts:
376
-        delete_rt = True
377
-        for newrt in all_route_tables:
378
-            if newrt.id == rt.id:
379
-                delete_rt = False
380
-        if delete_rt:
381
-            rta = rt.associations
382
-            is_main = False
383
-            for a in rta:
384
-                if a.main:
385
-                    is_main = True
386
-            try:
387
-                if not is_main:
388
-                    vpc_conn.delete_route_table(rt.id)
389
-            except EC2ResponseError as e:
390
-                module.fail_json(msg='Unable to delete old route table {0}, error: {1}'.format(rt.id, e))
391
-
392
-    vpc_dict = get_vpc_info(vpc)
393
-    created_vpc_id = vpc.id
394
-    returned_subnets = []
395
-    current_subnets = vpc_conn.get_all_subnets(filters={ 'vpc_id': vpc.id })
396
-    for sn in current_subnets:
397
-        returned_subnets.append({
398
-            'cidr': sn.cidr_block, 
399
-            'az': sn.availability_zone,
400
-            'id': sn.id,
401
-        })
402
-
403
-
404
-    return (vpc_dict, created_vpc_id, returned_subnets, changed)
405
-
406
-def terminate_vpc(module, vpc_conn, vpc_id=None, cidr=None):
407
-    """
408
-    Terminates a VPC
409
-
410
-    module: Ansible module object
411
-    vpc_conn: authenticated VPCConnection connection object
412
-    vpc_id: a vpc id to terminate
413
-    cidr: The cidr block of the VPC - can be used in lieu of an ID
414
-
415
-    Returns a dictionary of VPC information
416
-    about the VPC terminated.
417
-
418
-    If the VPC to be terminated is available
419
-    "changed" will be set to True.
420
-
421
-    """
422
-    vpc_dict = {}
423
-    terminated_vpc_id = ''
424
-    changed = False
425
-
426
-    if vpc_id == None and cidr == None:
427
-        module.fail_json(
428
-            msg='You must either specify a vpc id or a cidr '\
429
-            'block to terminate a VPC, aborting'
430
-        )
431
-    if vpc_id is not None:         
432
-        vpc_rs = vpc_conn.get_all_vpcs(vpc_id)
433
-    else:
434
-        vpc_rs = vpc_conn.get_all_vpcs(filters={'cidr': cidr})
435
-    if len(vpc_rs) > 1:
436
-        module.fail_json(
437
-            msg='EC2 returned more than one VPC for id {0} ' \
438
-            'or cidr {1}, aborting'.format(vpc_id,vidr)
439
-        )
440
-    if len(vpc_rs) == 1:
441
-        vpc = vpc_rs[0]
442
-        if vpc.state == 'available':
443
-            terminated_vpc_id=vpc.id
444
-            vpc_dict=get_vpc_info(vpc)
445
-            try:
446
-                subnets = vpc_conn.get_all_subnets(filters={'vpc_id': vpc.id})
447
-                for sn in subnets:
448
-                    vpc_conn.delete_subnet(sn.id)
449
-
450
-                igws = vpc_conn.get_all_internet_gateways(
451
-                    filters={'attachment.vpc-id': vpc.id}
452
-                )
453
-                for igw in igws:
454
-                    vpc_conn.detach_internet_gateway(igw.id, vpc.id)
455
-                    vpc_conn.delete_internet_gateway(igw.id)
456
-
457
-                rts = vpc_conn.get_all_route_tables(filters={'vpc_id': vpc.id})
458
-                for rt in rts:
459
-                    rta = rt.associations
460
-                    is_main = False
461
-                    for a in rta:
462
-                        if a.main:
463
-                            is_main = True
464
-                    if not is_main:
465
-                        vpc_conn.delete_route_table(rt.id)
466
-
467
-                vpc_conn.delete_vpc(vpc.id)
468
-            except EC2ResponseError as e:
469
-                module.fail_json(
470
-                    msg='Unable to delete VPC {0}, error: {1}'.format(vpc.id, e)
471
-                )
472
-            changed = True
473
-
474
-    return (changed, vpc_dict, terminated_vpc_id)
475
-
476
-
477
-def main():
478
-    module = AnsibleModule(
479
-        argument_spec = dict(
480
-            cidr_block = dict(),
481
-            wait = dict(choices=BOOLEANS, default=False),
482
-            wait_timeout = dict(default=300),
483
-            dns_support = dict(choices=BOOLEANS, default=True),
484
-            dns_hostnames = dict(choices=BOOLEANS, default=True),
485
-            subnets = dict(type='list'),
486
-            vpc_id = dict(),
487
-            internet_gateway = dict(choices=BOOLEANS, default=False),
488
-            route_tables = dict(type='list'),
489
-            region = dict(aliases=['aws_region', 'ec2_region'], choices=AWS_REGIONS),
490
-            state = dict(choices=['present', 'absent'], default='present'),
491
-            aws_secret_key = dict(aliases=['ec2_secret_key', 'secret_key'], no_log=True),
492
-            aws_access_key = dict(aliases=['ec2_access_key', 'access_key']),
493
-        )
494
-    )
495
-
496
-    state = module.params.get('state')
497
-
498
-    ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module)
499
-   
500
-    # If we have a region specified, connect to its endpoint.
501
-    if region: 
502
-        try:
503
-            vpc_conn = boto.vpc.connect_to_region(
504
-                region, 
505
-                aws_access_key_id=aws_access_key,
506
-                aws_secret_access_key=aws_secret_key
507
-            )
508
-        except boto.exception.NoAuthHandlerFound, e:
509
-            module.fail_json(msg = str(e))
510
-    else:
511
-        module.fail_json(msg="region must be specified")
512
-    
513
-    if module.params.get('state') == 'absent':
514
-        vpc_id = module.params.get('vpc_id')
515
-        cidr = module.params.get('cidr_block')
516
-        if vpc_id == None and cidr == None:
517
-            module.fail_json(
518
-                msg='You must either specify a vpc id or a cidr '\
519
-                'block to terminate a VPC, aborting'
520
-            )
521
-        (changed, vpc_dict, new_vpc_id) = terminate_vpc(module, vpc_conn, vpc_id, cidr)
522
-        subnets_changed = None
523
-    elif module.params.get('state') == 'present':
524
-        # Changed is always set to true when provisioning a new VPC
525
-        (vpc_dict, new_vpc_id, subnets_changed, changed) = create_vpc(module, vpc_conn)
526
-
527
-    module.exit_json(changed=changed, vpc_id=new_vpc_id, vpc=vpc_dict, subnets=subnets_changed)
528
-
529
-# import module snippets
530
-from ansible.module_utils.basic import *
531
-from ansible.module_utils.ec2 import *
532
-
533
-main()