Skip to content

Instantly share code, notes, and snippets.

@joelthompson
Last active February 24, 2016 22:33
Show Gist options
  • Save joelthompson/fefc5a158ad1abc7c606 to your computer and use it in GitHub Desktop.
Save joelthompson/fefc5a158ad1abc7c606 to your computer and use it in GitHub Desktop.
diff --git a/cloud/amazon/ec2_vpc_dhcp_options.py b/cloud/amazon/ec2_vpc_dhcp_options.py
index 7068b05..00f56ff 100644
--- a/cloud/amazon/ec2_vpc_dhcp_options.py
+++ b/cloud/amazon/ec2_vpc_dhcp_options.py
@@ -233,7 +233,7 @@ def fetch_dhcp_options_for_vpc(vpc_conn, vpc_id):
connection variable.
"""
vpcs = vpc_conn.get_all_vpcs(vpc_ids=[vpc_id])
- if len(vpcs) != 1:
+ if len(vpcs) != 1 or vpcs[0].dhcp_options_id == "default":
return None
dhcp_options = vpc_conn.get_all_dhcp_options(dhcp_options_ids=[vpcs[0].dhcp_options_id])
if len(dhcp_options) != 1:
@@ -286,6 +286,8 @@ def main():
region, ec2_url, boto_params = get_aws_connection_info(module)
connection = connect_to_aws(boto.vpc, region, **boto_params)
+ existing_options = None
+
# First check if we were given a dhcp_options_id
if not params['dhcp_options_id']:
# No, so create new_options from the parameters
@@ -306,12 +308,13 @@ def main():
existing_options = fetch_dhcp_options_for_vpc(connection, params['vpc_id'])
# if we've been asked to inherit existing options, do that now
if params['inherit_existing']:
- if not existing_options:
- module.fail_json(msg="a vpc_id was provided, but that vpc does not exist")
- for option in [ 'domain-name-servers', 'netbios-name-servers', 'ntp-servers', 'domain-name', 'netbios-node-type']:
- if existing_options.options.get(option) and (not new_options[option] or [''] == new_options[option]):
- new_options[option] = existing_options.options.get(option)
-
+ if existing_options:
+ for option in [ 'domain-name-servers', 'netbios-name-servers', 'ntp-servers', 'domain-name', 'netbios-node-type']:
+ if existing_options.options.get(option) and \
+ new_options[option] != [] and (not new_options[option] or [''] == new_options[option]):
+ new_options[option] = existing_options.options.get(option)
+
# Do the vpc's dhcp options already match what we're asked for? if so we are done
if existing_options and new_options == existing_options.options:
module.exit_json(changed=changed, new_options=new_options, dhcp_options_id=existing_options.id)
@@ -329,8 +332,8 @@ def main():
else:
found = True
dhcp_option = supplied_options[0]
- if params['state'] != 'absent' and options['tags']:
- ensure_tags(connection, options['tags'], False, module.check_mode)
+ if params['state'] != 'absent' and params['tags']:
+ ensure_tags(connection, dhcp_option.id, params['tags'], False, module.check_mode)
# Now we have the dhcp options set, let's do the necessary
@@ -365,6 +368,7 @@ def main():
# If we were given a vpc_id, then attach the options we now have to that before we finish
if params['vpc_id'] and not module.check_mode:
+ changed = True
connection.associate_dhcp_options(dhcp_option.id, params['vpc_id'])
# and remove old ones if that was requested
if params['delete_old'] and existing_options:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment