Created
November 7, 2017 15:20
-
-
Save s-hertel/d7efa582d1740f464da6e8a4576db06b to your computer and use it in GitHub Desktop.
remove tags by key only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+++ b/lib/ansible/modules/cloud/amazon/ec2_tag.py | |
@@ -33,6 +33,10 @@ options: | |
default: present | |
choices: ['present', 'absent', 'list'] | |
aliases: [] | |
+ remove_by_tag_key_only: | |
+ description: | |
+ - a boolean to indicate whether to remove tags with matching keys | |
+ default: False | |
tags: | |
description: | |
- a hash/dictionary of tags to add to the resource; '{"key":"value"}' and '{"key":"value","key":"value"}' | |
@@ -129,6 +133,7 @@ def main(): | |
resource = dict(required=True), | |
tags = dict(type='dict'), | |
state = dict(default='present', choices=['present', 'absent', 'list']), | |
+ remove_by_tag_key_only = dict(default=False, type='bool'), | |
) | |
) | |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) | |
@@ -139,6 +144,7 @@ def main(): | |
resource = module.params.get('resource') | |
tags = module.params.get('tags') | |
state = module.params.get('state') | |
+ remove_by_tag_key_only = module.params.get('remove_by_tag_key_only') | |
ec2 = ec2_connect(module) | |
@@ -170,14 +176,23 @@ def main(): | |
if state == 'absent': | |
if not tags: | |
module.fail_json(msg="tags argument is required when state is absent") | |
- for (key, value) in set(tags.items()): | |
- if (key, value) not in set(tagdict.items()): | |
- baddict[key] = value | |
- if set(baddict) == set(tags): | |
- module.exit_json(msg="Nothing to remove here. Move along.", changed=False) | |
- for (key, value) in set(tags.items()): | |
- if (key, value) in set(tagdict.items()): | |
- dictremove[key] = value | |
+ if remove_by_tag_key_only: | |
+ for (key, value) in set(tagdict.items()): | |
+ if key not in tags: | |
+ baddict[key] = value | |
+ if set(baddict.keys()) == set(tags.keys()): | |
+ module.exit_json(msg="Nothing to remove here.", changed=False) | |
+ if key in tags: | |
+ dictremove[key] = value | |
+ else: | |
+ for (key, value) in set(tags.items()): | |
+ if (key, value) not in set(tagdict.items()): | |
+ baddict[key] = value | |
+ if set(baddict) == set(tags): | |
+ module.exit_json(msg="Nothing to remove here. Move along.", changed=False) | |
+ for (key, value) in set(tags.items()): | |
+ if (key, value) in set(tagdict.items()): | |
+ dictremove[key] = value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment