|
|
@ -1,18 +1,11 @@
|
|
|
|
#!/usr/bin/python
|
|
|
|
#!/usr/bin/python
|
|
|
|
# This file is part of Ansible
|
|
|
|
# Copyright: Ansible Project
|
|
|
|
#
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
__metaclass__ = type
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
|
|
|
ANSIBLE_METADATA = {'status': ['stableinterface'],
|
|
|
|
ANSIBLE_METADATA = {'status': ['stableinterface'],
|
|
|
|
'supported_by': 'community',
|
|
|
|
'supported_by': 'community',
|
|
|
|
'metadata_version': '1.0'}
|
|
|
|
'metadata_version': '1.0'}
|
|
|
@ -114,9 +107,6 @@ policy:
|
|
|
|
}'
|
|
|
|
}'
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
from ansible.module_utils.ec2 import boto3_conn, get_aws_connection_info, ec2_argument_spec, AWSRetry
|
|
|
|
|
|
|
|
from ansible.module_utils.ec2 import sort_json_policy_dict, camel_dict_to_snake_dict, HAS_BOTO3
|
|
|
|
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
import traceback
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
|
|
|
@ -125,6 +115,11 @@ try:
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
pass # caught by imported HAS_BOTO3
|
|
|
|
pass # caught by imported HAS_BOTO3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
from ansible.module_utils.ec2 import (boto3_conn, get_aws_connection_info, ec2_argument_spec, AWSRetry,
|
|
|
|
|
|
|
|
sort_json_policy_dict, camel_dict_to_snake_dict, HAS_BOTO3)
|
|
|
|
|
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@AWSRetry.backoff(tries=5, delay=5, backoff=2.0)
|
|
|
|
@AWSRetry.backoff(tries=5, delay=5, backoff=2.0)
|
|
|
|
def list_policies_with_backoff(iam):
|
|
|
|
def list_policies_with_backoff(iam):
|
|
|
@ -318,8 +313,8 @@ def main():
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
rvalue = iam.create_policy(PolicyName=name, Path='/',
|
|
|
|
rvalue = iam.create_policy(PolicyName=name, Path='/',
|
|
|
|
PolicyDocument=policy, Description=description)
|
|
|
|
PolicyDocument=policy, Description=description)
|
|
|
|
except:
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="Couldn't create policy %s: %s" % (name, str(e)),
|
|
|
|
module.fail_json(msg="Couldn't create policy %s: %s" % (name, to_native(e)),
|
|
|
|
exception=traceback.format_exc(),
|
|
|
|
exception=traceback.format_exc(),
|
|
|
|
**camel_dict_to_snake_dict(e.response))
|
|
|
|
**camel_dict_to_snake_dict(e.response))
|
|
|
|
|
|
|
|
|
|
|
@ -332,8 +327,8 @@ def main():
|
|
|
|
if changed:
|
|
|
|
if changed:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
p = iam.get_policy(PolicyArn=p['Arn'])['Policy']
|
|
|
|
p = iam.get_policy(PolicyArn=p['Arn'])['Policy']
|
|
|
|
except:
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="Couldn't get policy: %s" % str(e),
|
|
|
|
module.fail_json(msg="Couldn't get policy: %s" % to_native(e),
|
|
|
|
exception=traceback.format_exc(),
|
|
|
|
exception=traceback.format_exc(),
|
|
|
|
**camel_dict_to_snake_dict(e.response))
|
|
|
|
**camel_dict_to_snake_dict(e.response))
|
|
|
|
|
|
|
|
|
|
|
@ -347,7 +342,7 @@ def main():
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
versions = iam.list_policy_versions(PolicyArn=p['Arn'])['Versions']
|
|
|
|
versions = iam.list_policy_versions(PolicyArn=p['Arn'])['Versions']
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
module.fail_json(msg="Couldn't list policy versions: %s" % str(e),
|
|
|
|
module.fail_json(msg="Couldn't list policy versions: %s" % to_native(e),
|
|
|
|
exception=traceback.format_exc(),
|
|
|
|
exception=traceback.format_exc(),
|
|
|
|
**camel_dict_to_snake_dict(e.response))
|
|
|
|
**camel_dict_to_snake_dict(e.response))
|
|
|
|
for v in versions:
|
|
|
|
for v in versions:
|
|
|
@ -355,14 +350,15 @@ def main():
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
iam.delete_policy_version(PolicyArn=p['Arn'], VersionId=v['VersionId'])
|
|
|
|
iam.delete_policy_version(PolicyArn=p['Arn'], VersionId=v['VersionId'])
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
module.fail_json(msg="Couldn't delete policy version %s: %s" % (v['VersionId'], str(e)),
|
|
|
|
module.fail_json(msg="Couldn't delete policy version %s: %s" %
|
|
|
|
|
|
|
|
(v['VersionId'], to_native(e)),
|
|
|
|
exception=traceback.format_exc(),
|
|
|
|
exception=traceback.format_exc(),
|
|
|
|
**camel_dict_to_snake_dict(e.response))
|
|
|
|
**camel_dict_to_snake_dict(e.response))
|
|
|
|
# Delete policy
|
|
|
|
# Delete policy
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
iam.delete_policy(PolicyArn=p['Arn'])
|
|
|
|
iam.delete_policy(PolicyArn=p['Arn'])
|
|
|
|
except:
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="Couldn't delete policy %s: %s" % (p['PolicyName'], str(e)),
|
|
|
|
module.fail_json(msg="Couldn't delete policy %s: %s" % (p['PolicyName'], to_native(e)),
|
|
|
|
exception=traceback.format_exc(),
|
|
|
|
exception=traceback.format_exc(),
|
|
|
|
**camel_dict_to_snake_dict(e.response))
|
|
|
|
**camel_dict_to_snake_dict(e.response))
|
|
|
|
# This is the one case where we will return the old policy
|
|
|
|
# This is the one case where we will return the old policy
|
|
|
@ -372,6 +368,5 @@ def main():
|
|
|
|
# end main
|
|
|
|
# end main
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
main()
|
|
|
|