Allow botocore configuration for aws modules (#55217)

* Allow botocore configuration to be configurable for boto3 modules

* Allow modification of the boto user agent

* play nicely with modules that might be modifying config

* changelog
This commit is contained in:
Sloane Hertel 2020-02-18 13:12:10 -05:00 committed by GitHub
parent 214bf8dc0b
commit ed6a6aca25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 7 deletions

View file

@ -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.

View file

@ -31,6 +31,7 @@ __metaclass__ = type
import os import os
import re import re
import sys
import traceback import traceback
from ansible.module_utils.ansible_release import __version__ 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 ' 'the conn_type parameter in the boto3_conn function '
'call') 'call')
if params.get('config'): config = botocore.config.Config(
config = params.pop('config') user_agent_extra='Ansible/{0}'.format(__version__),
config.user_agent_extra = 'Ansible/{0}'.format(__version__) )
else:
config = botocore.config.Config( if params.get('config') is not None:
user_agent_extra='Ansible/{0}'.format(__version__), 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( session = boto3.session.Session(
profile_name=profile, profile_name=profile,
) )
@ -186,6 +189,7 @@ def aws_common_argument_spec():
validate_certs=dict(default=True, type='bool'), validate_certs=dict(default=True, type='bool'),
security_token=dict(aliases=['access_token'], no_log=True), security_token=dict(aliases=['access_token'], no_log=True),
profile=dict(), profile=dict(),
aws_config=dict(type='dict'),
) )
@ -211,6 +215,7 @@ def get_aws_connection_info(module, boto3=False):
region = module.params.get('region') region = module.params.get('region')
profile_name = module.params.get('profile') profile_name = module.params.get('profile')
validate_certs = module.params.get('validate_certs') validate_certs = module.params.get('validate_certs')
config = module.params.get('aws_config')
if not ec2_url: if not ec2_url:
if 'AWS_URL' in os.environ: if 'AWS_URL' in os.environ:
@ -309,6 +314,13 @@ def get_aws_connection_info(module, boto3=False):
boto_params['validate_certs'] = validate_certs 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(): for param, value in boto_params.items():
if isinstance(value, binary_type): if isinstance(value, binary_type):
boto_params[param] = text_type(value, 'utf-8', 'strict') boto_params[param] = text_type(value, 'utf-8', 'strict')

View file

@ -50,6 +50,13 @@ options:
- Uses a boto profile. Only works with boto >= 2.24.0. - Uses a boto profile. Only works with boto >= 2.24.0.
type: str type: str
version_added: "1.6" 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: requirements:
- python >= 2.6 - python >= 2.6
- boto - boto