Skip to content

Commit

Permalink
authorized_keys - consistent behavior in check_mode
Browse files Browse the repository at this point in the history
Previously check_mode would incorrectly return changed=False even when a
change would have taken place if ran without check_mode, with
integration tests that confirmed this incorrect behavior. Also the
module did not correctly populate the return values when run in
check_mode. Both of these issues are resolved in this PR.

Fixes ansible-collections#37

Signed-off-by: Adam Miller <[email protected]>
  • Loading branch information
maxamillion committed Jun 19, 2020
1 parent 0d0f821 commit 86a5950
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- authorized_keys - fix inconsistent return value for check mode (https://github.com/ansible-collections/ansible.posix/issues/37)
8 changes: 2 additions & 6 deletions plugins/modules/authorized_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,13 +635,9 @@ def enforce_state(module, params):
}
params['diff'] = diff

if module.check_mode:
module.exit_json(changed=True, diff=diff)
writefile(module, filename, new_content)
if not module.check_mode:
writefile(module, filename, new_content)
params['changed'] = True
else:
if module.check_mode:
module.exit_json(changed=False)

return params

Expand Down
17 changes: 13 additions & 4 deletions tests/integration/targets/authorized_key/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -410,26 +410,35 @@
# -------------------------------------------------------------
# check mode

- name: copy an existing file in place with comments
- name: CHECK MODE | copy an existing file in place with comments
copy:
src: existing_authorized_keys
dest: "{{ output_dir | expanduser }}/authorized_keys"

- authorized_key:
- name: CHECK MODE | add key in check mode to validate return codes
authorized_key:
user: root
key: "{{ multiple_key_different_order_2 }}"
state: present
path: "{{ output_dir | expanduser }}/authorized_keys"
check_mode: True
register: result

- name: assert that the file was not changed
- name: CHECK MODE | assert that authorized_keys return values are consistent
assert:
that:
- 'result.changed == True'
- '"user" in result'
- '"key" in result'

- name: CHECK MODE | recopy authorized_keys to ensure it was not changed
copy:
src: existing_authorized_keys
dest: "{{ output_dir | expanduser }}/authorized_keys"
register: result

- assert:
- name: CHECK MODE | assert that the authorized_keys file was not changed
assert:
that:
- 'result.changed == False'

Expand Down

0 comments on commit 86a5950

Please sign in to comment.