diff options
author | flowerysong <junk+github@flowerysong.com> | 2018-10-18 10:13:08 -0400 |
---|---|---|
committer | Ryan Brown <sb@ryansb.com> | 2018-10-18 10:13:08 -0400 |
commit | 0fc639e755632b691a07fba5e9659fc663167d69 (patch) | |
tree | 1b05c0159af969f01ef8570e0b222a41ee89144c | |
parent | 3f629c426cf4e17dde10a004101754a3dcea0630 (diff) | |
download | ansible-0fc639e755632b691a07fba5e9659fc663167d69.tar.gz |
ec2_tag: Fix removing tags without specifying a value (#47228)
-rw-r--r-- | lib/ansible/modules/cloud/amazon/ec2_tag.py | 23 | ||||
-rw-r--r-- | test/integration/targets/ec2_tag/tasks/main.yml | 34 |
2 files changed, 52 insertions, 5 deletions
diff --git a/lib/ansible/modules/cloud/amazon/ec2_tag.py b/lib/ansible/modules/cloud/amazon/ec2_tag.py index 0105b7e22c..c33fc52ba9 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_tag.py +++ b/lib/ansible/modules/cloud/amazon/ec2_tag.py @@ -32,7 +32,8 @@ options: choices: ['present', 'absent', 'list'] tags: description: - - a hash/dictionary of tags to add to the resource; '{"key":"value"}' and '{"key":"value","key":"value"}' + - A dictionary of tags to add or remove from the resource. + - If the value provided for a tag is null and C(state) is I(absent), the tag will be removed regardless of its current value. required: true purge_tags: description: @@ -77,6 +78,22 @@ EXAMPLES = ''' state: list register: ec2_tags +- name: Remove the Env tag + ec2_tag: + region: eu-west-1 + resource: i-xxxxxxxxxxxxxxxxx + tags: + Env: + state: absent + +- name: Remove the Env tag if it's currently 'development' + ec2_tag: + region: eu-west-1 + resource: i-xxxxxxxxxxxxxxxxx + tags: + Env: development + state: absent + - name: Remove all tags except for Name from an instance ec2_tag: region: eu-west-1 @@ -149,8 +166,8 @@ def main(): remove_tags = {} if state == 'absent': for key in tags: - if key in current_tags and current_tags[key] == tags[key]: - remove_tags[key] = tags[key] + if key in current_tags and (tags[key] is None or current_tags[key] == tags[key]): + remove_tags[key] = current_tags[key] for key in remove: remove_tags[key] = current_tags[key] diff --git a/test/integration/targets/ec2_tag/tasks/main.yml b/test/integration/targets/ec2_tag/tasks/main.yml index 0c71cc5d74..81a12b44a6 100644 --- a/test/integration/targets/ec2_tag/tasks/main.yml +++ b/test/integration/targets/ec2_tag/tasks/main.yml @@ -38,17 +38,47 @@ tags: foo: foo bar: baz + baz: also baz <<: *aws_connection_info register: result - assert: that: - result is changed - - result.tags | length == 3 - - result.added_tags | length == 2 + - result.tags | length == 4 + - result.added_tags | length == 3 - result.tags.Name == '{{ resource_prefix }} ec2_tag volume' - result.tags.foo == 'foo' - result.tags.bar == 'baz' + - result.tags.baz == 'also baz' + + - name: Remove a tag by name + ec2_tag: + resource: "{{ volume.volume_id }}" + state: absent + tags: + baz: + <<: *aws_connection_info + register: result + + - assert: + that: + - result is changed + - result.removed_tags | length == 1 + - "'baz' in result.removed_tags" + + - name: Don't remove a tag + ec2_tag: + resource: "{{ volume.volume_id }}" + state: absent + tags: + foo: baz + <<: *aws_connection_info + register: result + + - assert: + that: + - result is not changed - name: Remove a tag ec2_tag: |