| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Aaron Smith <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: config_aggregator |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manage AWS Config aggregations across multiple accounts |
| 12 |
description: |
| 13 |
- Module manages AWS Config aggregator resources. |
| 14 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_config_aggregator). |
| 15 |
The usage did not change. |
| 16 |
author: |
| 17 |
- "Aaron Smith (@slapula)" |
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- The name of the AWS Config resource. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
state: |
| 25 |
description: |
| 26 |
- Whether the Config rule should be present or absent. |
| 27 |
default: present |
| 28 |
choices: ['present', 'absent'] |
| 29 |
type: str |
| 30 |
account_sources: |
| 31 |
description: |
| 32 |
- Provides a list of source accounts and regions to be aggregated. |
| 33 |
suboptions: |
| 34 |
account_ids: |
| 35 |
description: |
| 36 |
- A list of 12-digit account IDs of accounts being aggregated. |
| 37 |
type: list |
| 38 |
elements: str |
| 39 |
aws_regions: |
| 40 |
description: |
| 41 |
- A list of source regions being aggregated. |
| 42 |
type: list |
| 43 |
elements: str |
| 44 |
all_aws_regions: |
| 45 |
description: |
| 46 |
- If true, aggregate existing AWS Config regions and future regions. |
| 47 |
type: bool |
| 48 |
type: list |
| 49 |
elements: dict |
| 50 |
required: true |
| 51 |
organization_source: |
| 52 |
description: |
| 53 |
- The region authorized to collect aggregated data. |
| 54 |
suboptions: |
| 55 |
role_arn: |
| 56 |
description: |
| 57 |
- ARN of the IAM role used to retrieve AWS Organization details associated with the aggregator account. |
| 58 |
type: str |
| 59 |
aws_regions: |
| 60 |
description: |
| 61 |
- The source regions being aggregated. |
| 62 |
type: list |
| 63 |
elements: str |
| 64 |
all_aws_regions: |
| 65 |
description: |
| 66 |
- If true, aggregate existing AWS Config regions and future regions. |
| 67 |
type: bool |
| 68 |
type: dict |
| 69 |
required: true |
| 70 |
extends_documentation_fragment: |
| 71 |
- amazon.aws.common.modules |
| 72 |
- amazon.aws.region.modules |
| 73 |
- amazon.aws.boto3 |
| 74 |
""" |
| 75 |
|
| 76 |
EXAMPLES = r""" |
| 77 |
- name: Create cross-account aggregator |
| 78 |
community.aws.config_aggregator: |
| 79 |
name: test_config_rule |
| 80 |
state: present |
| 81 |
account_sources: |
| 82 |
account_ids: |
| 83 |
- 1234567890 |
| 84 |
- 0123456789 |
| 85 |
- 9012345678 |
| 86 |
all_aws_regions: true |
| 87 |
""" |
| 88 |
|
| 89 |
RETURN = r"""#""" |
| 90 |
|
| 91 |
|
| 92 |
try: |
| 93 |
import botocore |
| 94 |
except ImportError: |
| 95 |
pass # handled by AnsibleAWSModule |
| 96 |
|
| 97 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 98 |
|
| 99 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 100 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 101 |
|
| 102 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 103 |
|
| 104 |
|
| 105 |
def resource_exists(client, module, params): |
| 106 |
try: |
| 107 |
aggregator = client.describe_configuration_aggregators( |
| 108 |
ConfigurationAggregatorNames=[params["ConfigurationAggregatorName"]] |
| 109 |
) |
| 110 |
return aggregator["ConfigurationAggregators"][0] |
| 111 |
except is_boto3_error_code("NoSuchConfigurationAggregatorException"): |
| 112 |
return |
| 113 |
except ( |
| 114 |
botocore.exceptions.ClientError, |
| 115 |
botocore.exceptions.BotoCoreError, |
| 116 |
) as e: # pylint: disable=duplicate-except |
| 117 |
module.fail_json_aws(e) |
| 118 |
|
| 119 |
|
| 120 |
def create_resource(client, module, params, result): |
| 121 |
try: |
| 122 |
client.put_configuration_aggregator( |
| 123 |
ConfigurationAggregatorName=params["ConfigurationAggregatorName"], |
| 124 |
AccountAggregationSources=params["AccountAggregationSources"], |
| 125 |
OrganizationAggregationSource=params["OrganizationAggregationSource"], |
| 126 |
) |
| 127 |
result["changed"] = True |
| 128 |
result["aggregator"] = camel_dict_to_snake_dict(resource_exists(client, module, params)) |
| 129 |
return result |
| 130 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 131 |
module.fail_json_aws(e, msg="Couldn't create AWS Config configuration aggregator") |
| 132 |
|
| 133 |
|
| 134 |
def update_resource(client, module, params, result): |
| 135 |
result["changed"] = False |
| 136 |
|
| 137 |
current_params = client.describe_configuration_aggregators( |
| 138 |
ConfigurationAggregatorNames=[params["ConfigurationAggregatorName"]] |
| 139 |
)["ConfigurationAggregators"][0] |
| 140 |
|
| 141 |
if params["AccountAggregationSources"] != current_params.get("AccountAggregationSources", []): |
| 142 |
result["changed"] = True |
| 143 |
|
| 144 |
if params["OrganizationAggregationSource"] != current_params.get("OrganizationAggregationSource", {}): |
| 145 |
result["changed"] = True |
| 146 |
|
| 147 |
if result["changed"]: |
| 148 |
try: |
| 149 |
client.put_configuration_aggregator( |
| 150 |
ConfigurationAggregatorName=params["ConfigurationAggregatorName"], |
| 151 |
AccountAggregationSources=params["AccountAggregationSources"], |
| 152 |
OrganizationAggregationSource=params["OrganizationAggregationSource"], |
| 153 |
) |
| 154 |
result["aggregator"] = camel_dict_to_snake_dict(resource_exists(client, module, params)) |
| 155 |
return result |
| 156 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 157 |
module.fail_json_aws(e, msg="Couldn't create AWS Config configuration aggregator") |
| 158 |
|
| 159 |
|
| 160 |
def delete_resource(client, module, params, result): |
| 161 |
try: |
| 162 |
client.delete_configuration_aggregator(ConfigurationAggregatorName=params["ConfigurationAggregatorName"]) |
| 163 |
result["changed"] = True |
| 164 |
return result |
| 165 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 166 |
module.fail_json_aws(e, msg="Couldn't delete AWS Config configuration aggregator") |
| 167 |
|
| 168 |
|
| 169 |
def main(): |
| 170 |
module = AnsibleAWSModule( |
| 171 |
argument_spec={ |
| 172 |
"name": dict(type="str", required=True), |
| 173 |
"state": dict(type="str", choices=["present", "absent"], default="present"), |
| 174 |
"account_sources": dict(type="list", required=True, elements="dict"), |
| 175 |
"organization_source": dict(type="dict", required=True), |
| 176 |
}, |
| 177 |
supports_check_mode=False, |
| 178 |
) |
| 179 |
|
| 180 |
result = {"changed": False} |
| 181 |
|
| 182 |
name = module.params.get("name") |
| 183 |
state = module.params.get("state") |
| 184 |
|
| 185 |
params = {} |
| 186 |
if name: |
| 187 |
params["ConfigurationAggregatorName"] = name |
| 188 |
params["AccountAggregationSources"] = [] |
| 189 |
if module.params.get("account_sources"): |
| 190 |
for i in module.params.get("account_sources"): |
| 191 |
tmp_dict = {} |
| 192 |
if i.get("account_ids"): |
| 193 |
tmp_dict["AccountIds"] = i.get("account_ids") |
| 194 |
if i.get("aws_regions"): |
| 195 |
tmp_dict["AwsRegions"] = i.get("aws_regions") |
| 196 |
if i.get("all_aws_regions") is not None: |
| 197 |
tmp_dict["AllAwsRegions"] = i.get("all_aws_regions") |
| 198 |
params["AccountAggregationSources"].append(tmp_dict) |
| 199 |
if module.params.get("organization_source"): |
| 200 |
params["OrganizationAggregationSource"] = {} |
| 201 |
if module.params.get("organization_source").get("role_arn"): |
| 202 |
params["OrganizationAggregationSource"].update( |
| 203 |
{"RoleArn": module.params.get("organization_source").get("role_arn")} |
| 204 |
) |
| 205 |
if module.params.get("organization_source").get("aws_regions"): |
| 206 |
params["OrganizationAggregationSource"].update( |
| 207 |
{"AwsRegions": module.params.get("organization_source").get("aws_regions")} |
| 208 |
) |
| 209 |
if module.params.get("organization_source").get("all_aws_regions") is not None: |
| 210 |
params["OrganizationAggregationSource"].update( |
| 211 |
{"AllAwsRegions": module.params.get("organization_source").get("all_aws_regions")} |
| 212 |
) |
| 213 |
|
| 214 |
client = module.client("config", retry_decorator=AWSRetry.jittered_backoff()) |
| 215 |
|
| 216 |
resource_status = resource_exists(client, module, params) |
| 217 |
|
| 218 |
if state == "present": |
| 219 |
if not resource_status: |
| 220 |
create_resource(client, module, params, result) |
| 221 |
else: |
| 222 |
update_resource(client, module, params, result) |
| 223 |
|
| 224 |
if state == "absent": |
| 225 |
if resource_status: |
| 226 |
delete_resource(client, module, params, result) |
| 227 |
|
| 228 |
module.exit_json(changed=result["changed"]) |
| 229 |
|
| 230 |
|
| 231 |
if __name__ == "__main__": |
| 232 |
main() |