Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

secretsmanager_secret: add 'overwrite' parameter #1628

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/1628-secretsmanager_secret-overwrite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- secretsmanager_secret - added the ``overwrite`` parameter to support only setting the secret if it doesn't exist (https://github.com/ansible-collections/community.aws/pull/1628).
24 changes: 22 additions & 2 deletions plugins/modules/secretsmanager_secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
default: 'present'
choices: ['present', 'absent']
type: str
overwrite:
description:
- Whether to overwrite an existing secret with the same name.
- If set to C(True), an existing secret with the same I(name) will be overwritten.
- If set to C(False), a secret with the given I(name) will only be created if none exists.
type: bool
default: True
version_added: 5.3.0
recovery_window:
description:
- Only used if state is absent.
Expand Down Expand Up @@ -130,6 +138,14 @@
state: absent
secret_type: 'string'
secret: "{{ super_secret_string }}"

- name: Only create a new secret, but do not update if alredy exists by name
community.aws.secretsmanager_secret:
name: 'random_string'
state: present
secret_type: 'string'
secret: "{{ lookup('community.general.random_string', length=16, special=false) }}"
overwrite: false
'''

RETURN = r'''
Expand Down Expand Up @@ -524,6 +540,7 @@ def main():
argument_spec={
'name': dict(required=True),
'state': dict(choices=['present', 'absent'], default='present'),
'overwrite': dict(type='bool', default=True),
'description': dict(default=""),
'replica': dict(type='list', elements='dict', options=replica_args),
'kms_key_id': dict(),
Expand Down Expand Up @@ -580,12 +597,15 @@ def main():
result = secrets_mgr.put_resource_policy(secret)
changed = True
else:
# current_secret exists; decide what to do with it
if current_secret.get("DeletedDate"):
secrets_mgr.restore_secret(secret.name)
changed = True
if not secrets_mgr.secrets_match(secret, current_secret):
result = secrets_mgr.update_secret(secret)
changed = True
overwrite = module.params.get('overwrite')
if overwrite:
result = secrets_mgr.update_secret(secret)
changed = True
if not rotation_match(secret, current_secret):
result = secrets_mgr.update_rotation(secret)
changed = True
Expand Down
69 changes: 69 additions & 0 deletions tests/integration/targets/secretsmanager_secret/tasks/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,68 @@
that:
- result is not changed

# ============================================================
# Overwrite testing
# ============================================================

- name: Create secret with overwrite = False (Check mode)
aws_secret:
name: "{{ secret_name }}-2"
state: present
secret_type: 'string'
secret: "{{ super_secret_string }}"
overwrite: False
register: result
check_mode: True

- name: assert key is changed
assert:
that:
- result is changed

- name: Create secret with overwrite = False
aws_secret:
name: "{{ secret_name }}-2"
state: present
secret_type: 'string'
secret: "{{ super_secret_string }}"
overwrite: False
register: result

- name: assert key is changed
assert:
that:
- result is changed

- name: Update secret with overwrite = False (Check mode)
aws_secret:
name: "{{ secret_name }}-2"
state: present
secret_type: 'string'
secret: "{{ super_secret_string }}-2"
overwrite: False
register: result
check_mode: True

- name: assert key is not changed
assert:
that:
- result is not changed

- name: Create secret with overwrite = False
aws_secret:
name: "{{ secret_name }}-2"
state: present
secret_type: 'string'
secret: "{{ super_secret_string }}-2"
overwrite: False
register: result

- name: assert key is not changed
assert:
that:
- result is not changed

# ============================================================
# Removal testing
# ============================================================
Expand Down Expand Up @@ -749,3 +811,10 @@
state: absent
recovery_window: 0
ignore_errors: yes

- name: remove secret 2
aws_secret:
name: "{{ secret_name }}-2"
state: absent
recovery_window: 0
ignore_errors: yes