Created
July 22, 2019 08:45
-
-
Save diasjorge/25a37163cbf99af49f0f43caa13c0ae7 to your computer and use it in GitHub Desktop.
Ansible module
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
#!/usr/bin/python | |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | |
ANSIBLE_METADATA = {'metadata_version': '1.1', | |
'status': ['preview'], | |
'supported_by': 'community'} | |
DOCUMENTATION = ''' | |
--- | |
module: wait_for_instance_profile | |
short_description: Wait for instance profile to exist | |
description: | |
- Wait for instance profile to exist | |
options: | |
name: | |
description: | |
- The name of the instance profile | |
required: true | |
delay: | |
description: | |
- Delay in seconds | |
default: 1 | |
max_attempts: | |
description: | |
- Maximumn number of attempts | |
default: 40 | |
requirements: [ botocore, boto3 ] | |
extends_documentation_fragment: | |
- aws | |
- iam | |
''' | |
EXAMPLES = ''' | |
# Note: These examples do not set authentication details, see the AWS Guide for details. | |
- name: Wait for instance profile to exist | |
wait_for_instance_profile: | |
name: my_instance_profile_name | |
''' | |
RETURN = ''' | |
None | |
''' | |
from ansible.module_utils._text import to_native | |
from ansible.module_utils.aws.core import AnsibleAWSModule | |
import json | |
import traceback | |
try: | |
from botocore.exceptions import ClientError, BotoCoreError | |
except ImportError: | |
pass # caught by AnsibleAWSModule | |
def wait_for_instance_profile(connection, module): | |
params = dict() | |
instance_profile_name = module.params.get('name') | |
delay = module.params.get('delay') | |
max_attempts = module.params.get('max_attempts') | |
waiter = connection.get_waiter('instance_profile_exists') | |
try: | |
waiter.wait( | |
InstanceProfileName=instance_profile_name, | |
WaiterConfig={ | |
'Delay': delay, | |
'MaxAttempts': max_attempts | |
} | |
) | |
except ClientError as e: | |
module.fail_json(msg="Instance profile '{0}' does not exist: {1}".format(instance_profile_name, to_native(e)), | |
exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) | |
except BotoCoreError as e: | |
module.fail_json(msg="Unable to wait for instance profile '{0}': {1}".format(instance_profile_name, to_native(e)), | |
exception=traceback.format_exc()) | |
module.exit_json(changed=False) | |
def main(): | |
argument_spec = dict( | |
name=dict(type='str', required=True), | |
delay=dict(type='int', default=1), | |
max_attempts=dict(type='int', default=40), | |
) | |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) | |
connection = module.client('iam') | |
wait_for_instance_profile(connection, module) | |
if __name__ == '__main__': |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment