diff --git a/changelogs/fragments/55217-aws-modules-config.yml b/changelogs/fragments/55217-aws-modules-config.yml new file mode 100644 index 00000000000..7911c9931e0 --- /dev/null +++ b/changelogs/fragments/55217-aws-modules-config.yml @@ -0,0 +1,5 @@ +minor_changes: + - AWS modules now take an "aws_config" parameter to define botocore configuration settings + (https://github.com/ansible/ansible/issues/55182). + - AWS modules using boto can use the parameter to define the user agent, while boto3 modules + allow any configuration settings listed in the botocore API config documentation. diff --git a/lib/ansible/module_utils/ec2.py b/lib/ansible/module_utils/ec2.py index 59d088191b6..d461b12687a 100644 --- a/lib/ansible/module_utils/ec2.py +++ b/lib/ansible/module_utils/ec2.py @@ -31,6 +31,7 @@ __metaclass__ = type import os import re +import sys import traceback from ansible.module_utils.ansible_release import __version__ @@ -136,13 +137,15 @@ def _boto3_conn(conn_type=None, resource=None, region=None, endpoint=None, **par 'the conn_type parameter in the boto3_conn function ' 'call') - if params.get('config'): - config = params.pop('config') - config.user_agent_extra = 'Ansible/{0}'.format(__version__) - else: - config = botocore.config.Config( - user_agent_extra='Ansible/{0}'.format(__version__), - ) + config = botocore.config.Config( + user_agent_extra='Ansible/{0}'.format(__version__), + ) + + if params.get('config') is not None: + config = config.merge(params.pop('config')) + if params.get('aws_config') is not None: + config = config.merge(params.pop('aws_config')) + session = boto3.session.Session( profile_name=profile, ) @@ -186,6 +189,7 @@ def aws_common_argument_spec(): validate_certs=dict(default=True, type='bool'), security_token=dict(aliases=['access_token'], no_log=True), profile=dict(), + aws_config=dict(type='dict'), ) @@ -211,6 +215,7 @@ def get_aws_connection_info(module, boto3=False): region = module.params.get('region') profile_name = module.params.get('profile') validate_certs = module.params.get('validate_certs') + config = module.params.get('aws_config') if not ec2_url: if 'AWS_URL' in os.environ: @@ -309,6 +314,13 @@ def get_aws_connection_info(module, boto3=False): boto_params['validate_certs'] = validate_certs + if config is not None: + if HAS_BOTO3 and boto3: + boto_params['aws_config'] = botocore.config.Config(**config) + elif HAS_BOTO and not boto3: + if 'user_agent' in config: + sys.modules["boto.connection"].UserAgent = config['user_agent'] + for param, value in boto_params.items(): if isinstance(value, binary_type): boto_params[param] = text_type(value, 'utf-8', 'strict') diff --git a/lib/ansible/plugins/doc_fragments/aws.py b/lib/ansible/plugins/doc_fragments/aws.py index 84c3a4048a0..668955196f9 100644 --- a/lib/ansible/plugins/doc_fragments/aws.py +++ b/lib/ansible/plugins/doc_fragments/aws.py @@ -50,6 +50,13 @@ options: - Uses a boto profile. Only works with boto >= 2.24.0. type: str version_added: "1.6" + aws_config: + description: + - A dictionary to modify the botocore configuration. + - Parameters can be found at U(https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#botocore.config.Config). + - Only the 'user_agent' key is used for boto modules. See U(http://boto.cloudhackers.com/en/latest/boto_config_tut.html#boto) for more boto configuration. + type: dict + version_added: "2.10" requirements: - python >= 2.6 - boto