Allow ec2_tag module to list the tags of an instance
Use the list argument to state to just collect the tags of a resource through the AWS API.
This commit is contained in:
parent
d063cfaf39
commit
f568140ea5
1 changed files with 12 additions and 9 deletions
|
@ -19,7 +19,7 @@ DOCUMENTATION = '''
|
||||||
module: ec2_tag
|
module: ec2_tag
|
||||||
short_description: create and remove tag(s) to ec2 resources.
|
short_description: create and remove tag(s) to ec2 resources.
|
||||||
description:
|
description:
|
||||||
- Creates and removes tags from any EC2 resource. The resource is referenced by its resource id (e.g. an instance being i-XXXXXXX). It is designed to be used with complex args (tags), see the examples. This module has a dependency on python-boto.
|
- Creates, removes and lists tags from any EC2 resource. The resource is referenced by its resource id (e.g. an instance being i-XXXXXXX). It is designed to be used with complex args (tags), see the examples. This module has a dependency on python-boto.
|
||||||
version_added: "1.3"
|
version_added: "1.3"
|
||||||
options:
|
options:
|
||||||
resource:
|
resource:
|
||||||
|
@ -30,7 +30,7 @@ options:
|
||||||
aliases: []
|
aliases: []
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- Whether the tags should be present or absent on the resource.
|
- Whether the tags should be present or absent on the resource. Use list to interrogate the tags of an instance.
|
||||||
required: false
|
required: false
|
||||||
default: present
|
default: present
|
||||||
choices: ['present', 'absent']
|
choices: ['present', 'absent']
|
||||||
|
@ -115,14 +115,14 @@ def main():
|
||||||
argument_spec = ec2_argument_spec()
|
argument_spec = ec2_argument_spec()
|
||||||
argument_spec.update(dict(
|
argument_spec.update(dict(
|
||||||
resource = dict(required=True),
|
resource = dict(required=True),
|
||||||
tags = dict(required=True),
|
tags = dict(),
|
||||||
state = dict(default='present', choices=['present', 'absent']),
|
state = dict(default='present', choices=['present', 'absent', 'list']),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
module = AnsibleModule(argument_spec=argument_spec)
|
module = AnsibleModule(argument_spec=argument_spec)
|
||||||
|
|
||||||
resource = module.params.get('resource')
|
resource = module.params.get('resource')
|
||||||
tags = module.params['tags']
|
tags = module.params.get('tags')
|
||||||
state = module.params.get('state')
|
state = module.params.get('state')
|
||||||
|
|
||||||
ec2 = ec2_connect(module)
|
ec2 = ec2_connect(module)
|
||||||
|
@ -140,6 +140,8 @@ def main():
|
||||||
tagdict[tag.name] = tag.value
|
tagdict[tag.name] = tag.value
|
||||||
|
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
|
if not tags:
|
||||||
|
module.fail_json(msg="tags argument is required when state is present")
|
||||||
if set(tags.items()).issubset(set(tagdict.items())):
|
if set(tags.items()).issubset(set(tagdict.items())):
|
||||||
module.exit_json(msg="Tags already exists in %s." %resource, changed=False)
|
module.exit_json(msg="Tags already exists in %s." %resource, changed=False)
|
||||||
else:
|
else:
|
||||||
|
@ -151,6 +153,8 @@ def main():
|
||||||
module.exit_json(msg="Tags %s created for resource %s." % (dictadd,resource), changed=True)
|
module.exit_json(msg="Tags %s created for resource %s." % (dictadd,resource), changed=True)
|
||||||
|
|
||||||
if state == 'absent':
|
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()):
|
for (key, value) in set(tags.items()):
|
||||||
if (key, value) not in set(tagdict.items()):
|
if (key, value) not in set(tagdict.items()):
|
||||||
baddict[key] = value
|
baddict[key] = value
|
||||||
|
@ -162,10 +166,9 @@ def main():
|
||||||
tagger = ec2.delete_tags(resource, dictremove)
|
tagger = ec2.delete_tags(resource, dictremove)
|
||||||
gettags = ec2.get_all_tags(filters=filters)
|
gettags = ec2.get_all_tags(filters=filters)
|
||||||
module.exit_json(msg="Tags %s removed for resource %s." % (dictremove,resource), changed=True)
|
module.exit_json(msg="Tags %s removed for resource %s." % (dictremove,resource), changed=True)
|
||||||
|
|
||||||
# print json.dumps({
|
if state == 'list':
|
||||||
# "current_resource_tags": gettags,
|
module.exit_json(changed=False, **tagdict)
|
||||||
# })
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# import module snippets
|
# import module snippets
|
||||||
|
|
Loading…
Reference in a new issue