|
|
@ -17,75 +17,69 @@
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
---
|
|
|
|
module: sts_assume_role
|
|
|
|
module: sts_assume_role
|
|
|
|
short_description: assume a role in AWS account and obtain temporary credentials.
|
|
|
|
short_description: Assume a role using AWS Security Token Service and obtain temporary credentials
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- call AWS STS (Security Token Service) to assume a role in AWS account and obtain temporary credentials. This module has a dependency on python-boto.
|
|
|
|
- Assume a role using AWS Security Token Service and obtain temporary credentials
|
|
|
|
For details on base AWS API reference http://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html
|
|
|
|
version_added: "2.0"
|
|
|
|
version_added: "1.7"
|
|
|
|
author: Boris Ekelchik (@bekelchik)
|
|
|
|
options:
|
|
|
|
options:
|
|
|
|
role_arn:
|
|
|
|
role_arn:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- The Amazon Resource Name (ARN) of the role that the caller is assuming (http://docs.aws.amazon.com/IAM/latest/UserGuide/Using_Identifiers.html#Identifiers_ARNs)
|
|
|
|
- The Amazon Resource Name (ARN) of the role that the caller is assuming (http://docs.aws.amazon.com/IAM/latest/UserGuide/Using_Identifiers.html#Identifiers_ARNs)
|
|
|
|
required: true
|
|
|
|
required: true
|
|
|
|
aliases: []
|
|
|
|
|
|
|
|
role_session_name:
|
|
|
|
role_session_name:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Name of the role's session - will be used by CloudTrail
|
|
|
|
- Name of the role's session - will be used by CloudTrail
|
|
|
|
required: true
|
|
|
|
required: true
|
|
|
|
aliases: []
|
|
|
|
|
|
|
|
policy:
|
|
|
|
policy:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Supplemental policy to use in addition to assumed role's policies.
|
|
|
|
- Supplemental policy to use in addition to assumed role's policies.
|
|
|
|
required: false
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
|
|
|
|
duration_seconds:
|
|
|
|
duration_seconds:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) to 3600 seconds (1 hour). By default, the value is set to 3600 seconds.
|
|
|
|
- The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) to 3600 seconds (1 hour). By default, the value is set to 3600 seconds.
|
|
|
|
required: false
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
|
|
|
|
external_id:
|
|
|
|
external_id:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- A unique identifier that is used by third parties to assume a role in their customers' accounts.
|
|
|
|
- A unique identifier that is used by third parties to assume a role in their customers' accounts.
|
|
|
|
required: false
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
|
|
|
|
mfa_serial_number:
|
|
|
|
mfa_serial_number:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- he identification number of the MFA device that is associated with the user who is making the AssumeRole call.
|
|
|
|
- he identification number of the MFA device that is associated with the user who is making the AssumeRole call.
|
|
|
|
required: false
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
|
|
|
|
mfa_token:
|
|
|
|
mfa_token:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- The value provided by the MFA device, if the trust policy of the role being assumed requires MFA.
|
|
|
|
- The value provided by the MFA device, if the trust policy of the role being assumed requires MFA.
|
|
|
|
required: false
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
notes:
|
|
|
|
|
|
|
|
- In order to use the assumed role in a following playbook task you must pass the access_key, access_secret and access_token
|
|
|
|
author: Boris Ekelchik
|
|
|
|
|
|
|
|
extends_documentation_fragment: aws
|
|
|
|
extends_documentation_fragment: aws
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Basic example of assuming a role
|
|
|
|
# Note: These examples do not set authentication details, see the AWS Guide for details.
|
|
|
|
tasks:
|
|
|
|
|
|
|
|
- name: assume a role in account 123456789012
|
|
|
|
# Assume an existing role (more details: http://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html)
|
|
|
|
sts_assume_role: role_arn="arn:aws:iam::123456789012:role/someRole" session_name="someRoleSession"
|
|
|
|
sts_assume_role:
|
|
|
|
|
|
|
|
role_arn: "arn:aws:iam::123456789012:role/someRole"
|
|
|
|
- name: display temporary credentials
|
|
|
|
session_name: "someRoleSession"
|
|
|
|
debug: "temporary credentials for the assumed role are {{ ansible_temp_credentials }}"
|
|
|
|
register: assumed_role
|
|
|
|
|
|
|
|
|
|
|
|
- name: use temporary credentials for tagging an instance in account 123456789012
|
|
|
|
# Use the assumed role above to tag an instance in account 123456789012
|
|
|
|
ec2_tag: resource=i-xyzxyz01 region=us-west-1 state=present
|
|
|
|
ec2_tag:
|
|
|
|
args:
|
|
|
|
aws_access_key: "{{ assumed_role.sts_creds.access_key }}"
|
|
|
|
aws_access_key: "{{ ansible_temp_credentials.access_key }}"
|
|
|
|
aws_secret_key: "{{ assumed_role.sts_creds.secret_key }}"
|
|
|
|
aws_secret_key: "{{ ansible_temp_credentials.secret_key }}"
|
|
|
|
security_token: "{{ assumed_role.sts_creds.session_token }}"
|
|
|
|
security_token: "{{ ansible_temp_credentials.session_token }}"
|
|
|
|
resource: i-xyzxyz01
|
|
|
|
|
|
|
|
state: present
|
|
|
|
tags:
|
|
|
|
tags:
|
|
|
|
Test: value
|
|
|
|
MyNewTag: value
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
@ -93,71 +87,65 @@ import time
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
import boto.sts
|
|
|
|
import boto.sts
|
|
|
|
|
|
|
|
from boto.exception import BotoServerError
|
|
|
|
|
|
|
|
HAS_BOTO = True
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
print "failed=True msg='boto required for this module'"
|
|
|
|
HAS_BOTO = False
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sts_connect(module):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" Return an STS connection"""
|
|
|
|
def assume_role_policy(connection, module):
|
|
|
|
|
|
|
|
|
|
|
|
region, ec2_url, boto_params = get_aws_connection_info(module)
|
|
|
|
role_arn = module.params.get('role_arn')
|
|
|
|
|
|
|
|
role_session_name = module.params.get('role_session_name')
|
|
|
|
|
|
|
|
policy = module.params.get('policy')
|
|
|
|
|
|
|
|
duration_seconds = module.params.get('duration_seconds')
|
|
|
|
|
|
|
|
external_id = module.params.get('external_id')
|
|
|
|
|
|
|
|
mfa_serial_number = module.params.get('mfa_serial_number')
|
|
|
|
|
|
|
|
mfa_token = module.params.get('mfa_token')
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
|
|
|
|
# If we have a region specified, connect to its endpoint.
|
|
|
|
|
|
|
|
if region:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
sts = connect_to_aws(boto.sts, region, **boto_params)
|
|
|
|
|
|
|
|
except boto.exception.NoAuthHandlerFound, e:
|
|
|
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
# Otherwise, no region so we fallback to connect_sts method
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
sts = boto.connect_sts(**boto_params)
|
|
|
|
assumed_role = connection.assume_role(role_arn, role_session_name, policy, duration_seconds, external_id, mfa_serial_number, mfa_token)
|
|
|
|
except boto.exception.NoAuthHandlerFound, e:
|
|
|
|
changed = True
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
except BotoServerError, e:
|
|
|
|
|
|
|
|
module.fail_json(msg=e)
|
|
|
|
|
|
|
|
|
|
|
|
return sts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def assumeRole():
|
|
|
|
module.exit_json(changed=changed, sts_creds=assumed_role.credentials.__dict__, sts_user=assumed_role.user.__dict__)
|
|
|
|
data = sts.assume_role()
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
|
|
|
argument_spec.update(
|
|
|
|
role_arn = dict(required=True),
|
|
|
|
dict(
|
|
|
|
role_session_name = dict(required=True),
|
|
|
|
role_arn = dict(required=True, default=None),
|
|
|
|
duraction_seconds = dict(),
|
|
|
|
role_session_name = dict(required=True, default=None),
|
|
|
|
external_id = dict(),
|
|
|
|
duration_seconds = dict(required=False, default=None, type='int'),
|
|
|
|
policy = dict(),
|
|
|
|
external_id = dict(required=False, default=None),
|
|
|
|
mfa_serial_number = dict(),
|
|
|
|
policy = dict(required=False, default=None),
|
|
|
|
mfa_token = dict(),
|
|
|
|
mfa_serial_number = dict(required=False, default=None),
|
|
|
|
|
|
|
|
mfa_token = dict(required=False, default=None)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec)
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec)
|
|
|
|
|
|
|
|
|
|
|
|
role_arn = module.params.get('role_arn')
|
|
|
|
if not HAS_BOTO:
|
|
|
|
role_session_name = module.params.get('role_session_name')
|
|
|
|
module.fail_json(msg='boto required for this module')
|
|
|
|
policy = module.params.get('policy')
|
|
|
|
|
|
|
|
duraction_seconds = module.params.get('duraction_seconds')
|
|
|
|
|
|
|
|
external_id = module.params.get('external_id')
|
|
|
|
|
|
|
|
mfa_serial_number = module.params.get('mfa_serial_number')
|
|
|
|
|
|
|
|
mfa_token = module.params.get('mfa_token')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sts = sts_connect(module)
|
|
|
|
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
|
|
|
|
|
|
|
|
|
|
|
|
temp_credentials = {}
|
|
|
|
if region:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
connection = connect_to_aws(boto.sts, region, **aws_connect_params)
|
|
|
|
|
|
|
|
except (boto.exception.NoAuthHandlerFound, StandardError), e:
|
|
|
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
module.fail_json(msg="region must be specified")
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
temp_credentials = sts.assume_role(role_arn, role_session_name, policy, duraction_seconds,
|
|
|
|
assume_role_policy(connection, module)
|
|
|
|
external_id, mfa_serial_number, mfa_token).credentials.__dict__
|
|
|
|
except BotoServerError, e:
|
|
|
|
except boto.exception.BotoServerError, e:
|
|
|
|
module.fail_json(msg=e)
|
|
|
|
module.fail_json(msg='Unable to assume role {0}, error: {1}'.format(role_arn, e))
|
|
|
|
|
|
|
|
result = dict(changed=False, ansible_facts=dict(ansible_temp_credentials=temp_credentials))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|