mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Squashed commit of the following:
Changed ami_tags to instance_tags to better follow naming convention Add support for creating tags on the new instances
This commit is contained in:
parent
a894791767
commit
36027ddbba
2 changed files with 21 additions and 3 deletions
|
@ -623,6 +623,9 @@ class AnsibleModule(object):
|
|||
def jsonify(self, data):
|
||||
return json.dumps(data)
|
||||
|
||||
def from_json(self, data):
|
||||
return json.loads(data)
|
||||
|
||||
def exit_json(self, **kwargs):
|
||||
''' return from the module, without error '''
|
||||
self.add_path_info(kwargs)
|
||||
|
|
21
library/ec2
21
library/ec2
|
@ -95,8 +95,15 @@ options:
|
|||
required: False
|
||||
default: null
|
||||
aliases: []
|
||||
instance_tags:
|
||||
version_added: "1.0"
|
||||
description:
|
||||
- string of tags, in json format, to add to the new instance
|
||||
required: False
|
||||
default: null
|
||||
aliases: []
|
||||
examples:
|
||||
- code: "local_action: ec2 keypair=admin instance_type=m1.large image=emi-40603AD1 wait=true group=webserver count=3"
|
||||
- code: "local_action: ec2 keypair=admin instance_type=m1.large image=emi-40603AD1 wait=true group=webserver count=3 instance_tags='{"Name":"My Server"}'"
|
||||
description: "Examples from Ansible Playbooks"
|
||||
requirements: [ "boto" ]
|
||||
author: Seth Vidal, Tim Gerla, Lester Wade
|
||||
|
@ -126,6 +133,7 @@ def main():
|
|||
ec2_secret_key = dict(aliases=['EC2_SECRET_KEY']),
|
||||
ec2_access_key = dict(aliases=['EC2_ACCESS_KEY']),
|
||||
user_data = dict(),
|
||||
instance_tags = dict(),
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -141,7 +149,8 @@ def main():
|
|||
ec2_secret_key = module.params.get('ec2_secret_key')
|
||||
ec2_access_key = module.params.get('ec2_access_key')
|
||||
user_data = module.params.get('user_data')
|
||||
|
||||
instance_tags = module.params.get('instance_tags')
|
||||
|
||||
# allow eucarc environment variables to be used if ansible vars aren't set
|
||||
if not ec2_url and 'EC2_URL' in os.environ:
|
||||
ec2_url = os.environ['EC2_URL']
|
||||
|
@ -156,7 +165,7 @@ def main():
|
|||
ec2 = boto.connect_ec2(ec2_access_key, ec2_secret_key)
|
||||
|
||||
# Both min_count and max_count equal count parameter. This means the launch request is explicit (we want count, or fail) in how many instances we want.
|
||||
|
||||
|
||||
try:
|
||||
res = ec2.run_instances(image, key_name = key_name,
|
||||
min_count = count, max_count = count,
|
||||
|
@ -170,6 +179,12 @@ def main():
|
|||
|
||||
instids = [ i.id for i in res.instances ]
|
||||
|
||||
if instance_tags:
|
||||
try:
|
||||
ec2.create_tags(instids, module.from_json(instance_tags))
|
||||
except boto.exception.EC2ResponseError as e:
|
||||
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
|
||||
|
||||
res_list = res.connection.get_all_instances(instids)
|
||||
this_res = res_list[0]
|
||||
if wait:
|
||||
|
|
Loading…
Reference in a new issue