mirror of https://github.com/ansible/ansible.git
remove facts and fix docs for taskdefinition module
parent
f4f52119b3
commit
f3a9a92ffe
@ -1,174 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: ecs_cluster_facts
|
||||
short_description: list or describe clusters or their instances in ecs
|
||||
description:
|
||||
- Lists or describes clusters or cluster instances in ecs.
|
||||
version_added: "2.0"
|
||||
author: Mark Chance(@Java1Guy)
|
||||
options:
|
||||
details:
|
||||
description:
|
||||
- Set this to true if you want detailed information.
|
||||
required: false
|
||||
default: false
|
||||
cluster:
|
||||
description:
|
||||
- The cluster ARNS to list.
|
||||
required: false
|
||||
default: 'default'
|
||||
instances:
|
||||
description:
|
||||
- The instance ARNS to list.
|
||||
required: false
|
||||
default: None (returns all)
|
||||
option:
|
||||
description:
|
||||
- Whether to return information about clusters or their instances
|
||||
required: false
|
||||
choices: ['clusters', 'instances']
|
||||
default: 'clusters'
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Note: These examples do not set authentication details, see the AWS Guide for details.
|
||||
|
||||
# Basic listing example
|
||||
- ecs_task:
|
||||
cluster=test-cluster
|
||||
task_list=123456789012345678901234567890123456
|
||||
|
||||
# Basic example of deregistering task
|
||||
- ecs_task:
|
||||
state: absent
|
||||
family: console-test-tdn
|
||||
revision: 1
|
||||
'''
|
||||
RETURN = '''
|
||||
clusters:
|
||||
description:
|
||||
- array of cluster ARNs when details is false
|
||||
- array of dicts when details is true
|
||||
sample: [ "arn:aws:ecs:us-west-2:172139249013:cluster/test-cluster" ]
|
||||
'''
|
||||
try:
|
||||
import json, os
|
||||
import boto
|
||||
import botocore
|
||||
HAS_BOTO = True
|
||||
except ImportError:
|
||||
HAS_BOTO = False
|
||||
|
||||
try:
|
||||
import boto3
|
||||
# import module snippets
|
||||
from ansible.module_utils.ec2 import ec2_argument_spec, get_aws_connection_info, boto3_conn
|
||||
HAS_BOTO3 = True
|
||||
except ImportError:
|
||||
HAS_BOTO3 = False
|
||||
|
||||
class EcsClusterManager:
|
||||
"""Handles ECS Clusters"""
|
||||
|
||||
def __init__(self, module):
|
||||
self.module = module
|
||||
|
||||
try:
|
||||
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
|
||||
if not region:
|
||||
module.fail_json(msg="Region must be specified as a parameter, in EC2_REGION or AWS_REGION environment variables or in boto configuration file")
|
||||
self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs)
|
||||
except boto.exception.NoAuthHandlerFound, e:
|
||||
self.module.fail_json(msg="Can't authorize connection - "+str(e))
|
||||
|
||||
def list_container_instances(self, cluster):
|
||||
response = self.ecs.list_container_instances(cluster=cluster)
|
||||
relevant_response = dict(instances = response['containerInstanceArns'])
|
||||
return relevant_response
|
||||
|
||||
def describe_container_instances(self, cluster, instances):
|
||||
response = self.ecs.describe_container_instances(
|
||||
clusters=cluster,
|
||||
containerInstances=instances.split(",") if instances else []
|
||||
)
|
||||
relevant_response = dict()
|
||||
if 'containerInstances' in response and len(response['containerInstances'])>0:
|
||||
relevant_response['instances'] = response['containerInstances']
|
||||
if 'failures' in response and len(response['failures'])>0:
|
||||
relevant_response['instances_not_running'] = response['failures']
|
||||
return relevant_response
|
||||
|
||||
def list_clusters(self):
|
||||
response = self.ecs.list_clusters()
|
||||
relevant_response = dict(clusters = response['clusterArns'])
|
||||
return relevant_response
|
||||
|
||||
def describe_clusters(self, cluster):
|
||||
response = self.ecs.describe_clusters(
|
||||
clusters=cluster.split(",") if cluster else []
|
||||
)
|
||||
relevant_response = dict()
|
||||
if 'clusters' in response and len(response['clusters'])>0:
|
||||
relevant_response['clusters'] = response['clusters']
|
||||
if 'failures' in response and len(response['failures'])>0:
|
||||
relevant_response['clusters_not_running'] = response['failures']
|
||||
return relevant_response
|
||||
|
||||
def main():
|
||||
|
||||
argument_spec = ec2_argument_spec()
|
||||
argument_spec.update(dict(
|
||||
details=dict(required=False, type='bool' ),
|
||||
cluster=dict(required=False, type='str' ),
|
||||
instances=dict(required=False, type='str' ),
|
||||
option=dict(required=False, choices=['clusters', 'instances'], default='clusters')
|
||||
))
|
||||
|
||||
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||
|
||||
if not HAS_BOTO:
|
||||
module.fail_json(msg='boto is required.')
|
||||
|
||||
if not HAS_BOTO3:
|
||||
module.fail_json(msg='boto3 is required.')
|
||||
|
||||
show_details = False
|
||||
if 'details' in module.params and module.params['details']:
|
||||
show_details = True
|
||||
|
||||
task_mgr = EcsClusterManager(module)
|
||||
if module.params['option']=='clusters':
|
||||
if show_details:
|
||||
ecs_facts = task_mgr.describe_clusters(module.params['cluster'])
|
||||
else:
|
||||
ecs_facts = task_mgr.list_clusters()
|
||||
if module.params['option']=='instances':
|
||||
if show_details:
|
||||
ecs_facts = task_mgr.describe_container_instances(module.params['cluster'], module.params['instances'])
|
||||
else:
|
||||
ecs_facts = task_mgr.list_container_instances(module.params['cluster'])
|
||||
ecs_facts_result = dict(changed=False, ansible_facts=ecs_facts)
|
||||
module.exit_json(**ecs_facts_result)
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
from ansible.module_utils.urls import *
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -1,184 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: ecs_task_facts
|
||||
short_description: return facts about tasks in ecs
|
||||
description:
|
||||
- Describes or lists tasks.
|
||||
version_added: 2.0
|
||||
author: Mark Chance(@Java1Guy)
|
||||
options:
|
||||
details:
|
||||
description:
|
||||
- Set this to true if you want detailed information about the tasks.
|
||||
required: false
|
||||
default: false
|
||||
type: bool
|
||||
cluster:
|
||||
description:
|
||||
- The cluster in which to list tasks if other than the 'default'.
|
||||
required: false
|
||||
default: 'default'
|
||||
type: str
|
||||
task_list:
|
||||
description:
|
||||
- Set this to a list of task identifiers. If 'details' is false, this is required.
|
||||
required: false
|
||||
family:
|
||||
required: False
|
||||
type: str
|
||||
|
||||
container_instance:
|
||||
required: False
|
||||
type: 'str'
|
||||
max_results:
|
||||
required: False
|
||||
type: 'int'
|
||||
started_by:
|
||||
required: False
|
||||
type: 'str'
|
||||
service_name:
|
||||
required: False
|
||||
type: 'str'
|
||||
desired_status:
|
||||
required: False
|
||||
choices=['RUNNING', 'PENDING', 'STOPPED']
|
||||
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Note: These examples do not set authentication details, see the AWS Guide for details.
|
||||
|
||||
# Basic listing example
|
||||
- ecs_task_facts:
|
||||
cluster=test-cluster
|
||||
task_list=123456789012345678901234567890123456
|
||||
|
||||
# Listing tasks with details
|
||||
- ecs_task_facts:
|
||||
details: true
|
||||
cluster=test-cluster
|
||||
task_list=123456789012345678901234567890123456
|
||||
|
||||
'''
|
||||
try:
|
||||
import json, os
|
||||
import boto
|
||||
import botocore
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
from ansible.module_utils.ec2 import *
|
||||
HAS_BOTO = True
|
||||
except ImportError:
|
||||
HAS_BOTO = False
|
||||
|
||||
try:
|
||||
import boto3
|
||||
HAS_BOTO3 = True
|
||||
except ImportError:
|
||||
HAS_BOTO3 = False
|
||||
|
||||
class EcsTaskManager:
|
||||
"""Handles ECS Tasks"""
|
||||
|
||||
def __init__(self, module):
|
||||
self.module = module
|
||||
|
||||
try:
|
||||
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
|
||||
if not region:
|
||||
module.fail_json(msg="Region must be specified as a parameter, in EC2_REGION or AWS_REGION environment variables or in boto configuration file")
|
||||
self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs)
|
||||
except boto.exception.NoAuthHandlerFound, e:
|
||||
self.module.fail_json(msg="Can't authorize connection - "+str(e))
|
||||
|
||||
def transmogrify(self, params, field, dictionary, arg_name):
|
||||
if field in params and params[field] is not None:
|
||||
dictionary[arg_name] = params[field]
|
||||
|
||||
def list_tasks(self, params):
|
||||
fn_args = dict()
|
||||
self.transmogrify(params, 'cluster', fn_args, 'cluster')
|
||||
self.transmogrify(params, 'container_instance', fn_args, 'containerInstance')
|
||||
self.transmogrify(params, 'family', fn_args, 'family')
|
||||
self.transmogrify(params, 'max_results', fn_args, 'maxResults')
|
||||
self.transmogrify(params, 'started_by', fn_args, 'startedBy')
|
||||
self.transmogrify(params, 'service_name', fn_args, 'startedBy')
|
||||
self.transmogrify(params, 'desired_status', fn_args, 'desiredStatus')
|
||||
relevant_response = dict()
|
||||
try:
|
||||
response = self.ecs.list_tasks(**fn_args)
|
||||
relevant_response['tasks'] = response['taskArns']
|
||||
except botocore.exceptions.ClientError:
|
||||
relevant_response['tasks'] = []
|
||||
return relevant_response
|
||||
|
||||
def describe_tasks(self, cluster_name, tasks):
|
||||
response = self.ecs.describe_tasks(
|
||||
cluster=cluster_name if cluster_name else '',
|
||||
tasks=tasks.split(",") if tasks else []
|
||||
)
|
||||
relevant_response = dict(
|
||||
tasks = response['tasks'],
|
||||
tasks_not_running = response['failures'])
|
||||
return relevant_response
|
||||
|
||||
def main():
|
||||
|
||||
argument_spec = ec2_argument_spec()
|
||||
argument_spec.update(dict(
|
||||
details=dict(required=False, type='bool' ),
|
||||
cluster=dict(required=False, type='str' ),
|
||||
task_list = dict(required=False, type='str'),
|
||||
family=dict(required= False, type='str' ),
|
||||
container_instance=dict(required=False, type='str' ),
|
||||
max_results=dict(required=False, type='int' ),
|
||||
started_by=dict(required=False, type='str' ),
|
||||
service_name=dict(required=False, type='str' ),
|
||||
desired_status=dict(required=False, choices=['RUNNING', 'PENDING', 'STOPPED'])
|
||||
))
|
||||
|
||||
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||
|
||||
if not HAS_BOTO:
|
||||
module.fail_json(msg='boto is required.')
|
||||
|
||||
if not HAS_BOTO3:
|
||||
module.fail_json(msg='boto3 is required.')
|
||||
|
||||
task_to_describe = module.params['family']
|
||||
show_details = False
|
||||
if 'details' in module.params and module.params['details']:
|
||||
show_details = True
|
||||
|
||||
task_mgr = EcsTaskManager(module)
|
||||
if show_details:
|
||||
if 'task_list' not in module.params or not module.params['task_list']:
|
||||
module.fail_json(msg="task_list must be specified for ecs_task_facts")
|
||||
ecs_facts = task_mgr.describe_tasks(module.params['cluster'], module.params['task_list'])
|
||||
else:
|
||||
ecs_facts = task_mgr.list_tasks(module.params)
|
||||
ecs_facts_result = dict(changed=False, ansible_facts=ecs_facts)
|
||||
module.exit_json(**ecs_facts_result)
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
from ansible.module_utils.urls import *
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -1,174 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: ecs_taskdefinition_facts
|
||||
short_description: return facts about task definitions in ecs
|
||||
description:
|
||||
- Describes or lists task definitions.
|
||||
version_added: 2.0
|
||||
author: Mark Chance(@Java1Guy)
|
||||
requirements: [ json, os, boto, botocore, boto3 ]
|
||||
options:
|
||||
details:
|
||||
description:
|
||||
- Set this to true if you want detailed information about the tasks.
|
||||
required: false
|
||||
default: false
|
||||
name:
|
||||
description:
|
||||
- When details is true, the name must be provided.
|
||||
required: false
|
||||
family:
|
||||
description:
|
||||
- the name of the family of task definitions to list.
|
||||
required: false
|
||||
max_results:
|
||||
description:
|
||||
- The maximum number of results to return.
|
||||
required: false
|
||||
status:
|
||||
description:
|
||||
- Show only task descriptions of the given status. If omitted, it shows all
|
||||
required: false
|
||||
choices: ['ACTIVE', 'INACTIVE']
|
||||
sort:
|
||||
description:
|
||||
- Sort order of returned list of task definitions
|
||||
required: false
|
||||
choices: ['ASC', 'DESC']
|
||||
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Note: These examples do not set authentication details, see the AWS Guide for details.
|
||||
|
||||
# Basic listing example
|
||||
- name: "Get task definitions with details"
|
||||
ecs_taskdefinition_facts:
|
||||
name: test-cluster-tasks
|
||||
details: true
|
||||
|
||||
- name: Get task definitions with details
|
||||
ecs_taskdefinition_facts:
|
||||
status: INACTIVE
|
||||
details: true
|
||||
family: test-cluster-rbjgjoaj-task
|
||||
name: "arn:aws:ecs:us-west-2:172139249013:task-definition/test-cluster-rbjgjoaj-task:1"
|
||||
'''
|
||||
RETURN = '''
|
||||
task_definitions:
|
||||
description: array of ARN values for the known task definitions
|
||||
type: array of string or dict if details is true
|
||||
sample: ["arn:aws:ecs:us-west-2:172139249013:task-definition/console-sample-app-static:1"]
|
||||
'''
|
||||
try:
|
||||
import json, os
|
||||
import boto
|
||||
import botocore
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
from ansible.module_utils.ec2 import *
|
||||
HAS_BOTO = True
|
||||
except ImportError:
|
||||
HAS_BOTO = False
|
||||
|
||||
try:
|
||||
import boto3
|
||||
HAS_BOTO3 = True
|
||||
except ImportError:
|
||||
HAS_BOTO3 = False
|
||||
|
||||
class EcsTaskManager:
|
||||
"""Handles ECS Tasks"""
|
||||
|
||||
def __init__(self, module):
|
||||
self.module = module
|
||||
|
||||
try:
|
||||
# self.ecs = boto3.client('ecs')
|
||||
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
|
||||
if not region:
|
||||
module.fail_json(msg="Region must be specified as a parameter, in EC2_REGION or AWS_REGION environment variables or in boto configuration file")
|
||||
self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs)
|
||||
except boto.exception.NoAuthHandlerFound, e:
|
||||
self.module.fail_json(msg="Can't authorize connection - "+str(e))
|
||||
|
||||
def transmogrify(self, params, field, dictionary, arg_name):
|
||||
if field in params and params[field] is not None:
|
||||
dictionary[arg_name] = params[field]
|
||||
|
||||
def list_taskdefinitions(self, params):
|
||||
fn_args = dict()
|
||||
self.transmogrify(params, 'family', fn_args, 'familyPrefix')
|
||||
self.transmogrify(params, 'max_results', fn_args, 'maxResults')
|
||||
self.transmogrify(params, 'status', fn_args, 'status')
|
||||
self.transmogrify(params, 'sort', fn_args, 'sort')
|
||||
response = self.ecs.list_task_definitions(**fn_args)
|
||||
return dict(task_definitions=response['taskDefinitionArns'])
|
||||
|
||||
def describe_taskdefinition(self, task_definition):
|
||||
try:
|
||||
response = self.ecs.describe_task_definition(taskDefinition=task_definition)
|
||||
except botocore.exceptions.ClientError:
|
||||
response = dict(taskDefinition=[ dict( name=task_definition, status="MISSING")])
|
||||
relevant_response = dict(
|
||||
task_definitions = response['taskDefinition']
|
||||
)
|
||||
return relevant_response
|
||||
|
||||
def main():
|
||||
|
||||
argument_spec = ec2_argument_spec()
|
||||
argument_spec.update(dict(
|
||||
details=dict(required= False, type='bool' ),
|
||||
name=dict(required=False, type='str' ),
|
||||
family=dict(required=False, type='str' ),
|
||||
max_results=dict(required=False, type='int' ),
|
||||
status=dict(required=False, choices=['ACTIVE', 'INACTIVE']),
|
||||
sort=dict(required=False, choices=['ASC', 'DESC'])
|
||||
))
|
||||
|
||||
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||
|
||||
if not HAS_BOTO:
|
||||
module.fail_json(msg='boto is required.')
|
||||
|
||||
if not HAS_BOTO3:
|
||||
module.fail_json(msg='boto3 is required.')
|
||||
|
||||
show_details = False
|
||||
if 'details' in module.params and module.params['details']:
|
||||
if 'name' not in module.params or not module.params['name']:
|
||||
module.fail_json(msg="task definition name must be specified for ecs_taskdefinition_facts")
|
||||
show_details = True
|
||||
|
||||
task_mgr = EcsTaskManager(module)
|
||||
if show_details:
|
||||
ecs_facts = task_mgr.describe_taskdefinition(module.params['name'])
|
||||
else:
|
||||
ecs_facts = task_mgr.list_taskdefinitions(module.params)
|
||||
ecs_facts_result = dict(changed=False, ansible_facts=ecs_facts)
|
||||
module.exit_json(**ecs_facts_result)
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
from ansible.module_utils.ec2 import *
|
||||
from ansible.module_utils.urls import *
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue