Debian Codegraph @

ansible_collections/amazon/aws/plugins/callback/aws_resource_actions.py

raw
1 # -*- coding: utf-8 -*-
2
3 # (C) 2018 Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 DOCUMENTATION = r"""
7 name: aws_resource_actions
8 type: aggregate
9 short_description: summarizes all "resource:actions" completed
10 description:
11 - Ansible callback plugin for collecting the AWS actions completed by all boto3 modules using
12 AnsibleAWSModule in a playbook. Botocore endpoint logs need to be enabled for those modules, which can
13 be done easily by setting debug_botocore_endpoint_logs to True for group/aws using module_defaults.
14 requirements:
15 - whitelisting in configuration - see examples section below for details.
16 """
17
18 EXAMPLES = r"""
19 example: >
20 To enable, add this to your ansible.cfg file in the defaults block
21 [defaults]
22 callback_whitelist = aws_resource_actions
23 sample output: >
24 #
25 # AWS ACTIONS: ['s3:PutBucketAcl', 's3:HeadObject', 's3:DeleteObject', 's3:PutObjectAcl', 's3:CreateMultipartUpload',
26 # 's3:DeleteBucket', 's3:GetObject', 's3:DeleteObjects', 's3:CreateBucket', 's3:CompleteMultipartUpload',
27 # 's3:ListObjectsV2', 's3:HeadBucket', 's3:UploadPart', 's3:PutObject']
28 """
29
30 from ansible.module_utils.common.text.converters import to_native
31 from ansible.plugins.callback import CallbackBase
32
33
34 class CallbackModule(CallbackBase):
35 """Callback plugin to collect and display AWS resource actions.
36
37 This callback aggregates resource actions from task results. The resource_actions
38 are captured by AnsibleAWSModule when debug_botocore_endpoint_logs is enabled.
39 AnsibleAWSModule parses botocore.endpoint debug logs to extract API calls and
40 adds them to the task result as resource_actions. This callback then collects
41 those actions across all tasks and displays a summary.
42 """
43
44 CALLBACK_VERSION = 2.8
45 CALLBACK_TYPE = "aggregate"
46 CALLBACK_NAME = "amazon.aws.aws_resource_actions"
47 CALLBACK_NEEDS_WHITELIST = True
48
49 def __init__(self):
50 self.aws_resource_actions = []
51 super().__init__()
52
53 def extend_aws_resource_actions(self, result):
54 if result.get("resource_actions"):
55 self.aws_resource_actions.extend(result["resource_actions"])
56
57 def runner_on_ok(self, host, res):
58 self.extend_aws_resource_actions(res)
59
60 def runner_on_failed(self, host, res, ignore_errors=False):
61 self.extend_aws_resource_actions(res)
62
63 def v2_runner_item_on_ok(self, result):
64 self.extend_aws_resource_actions(result._result)
65
66 def v2_runner_item_on_failed(self, result):
67 self.extend_aws_resource_actions(result._result)
68
69 def playbook_on_stats(self, stats):
70 if self.aws_resource_actions:
71 self.aws_resource_actions = sorted(to_native(action) for action in set(self.aws_resource_actions))
72 self._display.display(f"AWS ACTIONS: {self.aws_resource_actions}")