From f501b579e51c05565d852feb676fa38cddf3f2ed Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Fri, 28 Jan 2022 09:34:59 +0100 Subject: [PATCH] ansible-vault encrypt_string: fix --output to write into file (#76856) Fixes #75101 Fixes #59590 --- .../75101-ansible-vault-encrypt_string-output-file.yml | 2 ++ lib/ansible/cli/vault.py | 5 ++++- test/integration/targets/ansible-vault/runme.sh | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/75101-ansible-vault-encrypt_string-output-file.yml diff --git a/changelogs/fragments/75101-ansible-vault-encrypt_string-output-file.yml b/changelogs/fragments/75101-ansible-vault-encrypt_string-output-file.yml new file mode 100644 index 00000000000..7c9ded2398b --- /dev/null +++ b/changelogs/fragments/75101-ansible-vault-encrypt_string-output-file.yml @@ -0,0 +1,2 @@ +bugfixes: + - "ansible-vault encrypt_string - fix ``--output`` option to correctly write encrypted string into given file (https://github.com/ansible/ansible/issues/75101)" diff --git a/lib/ansible/cli/vault.py b/lib/ansible/cli/vault.py index 830d22395d6..3491b22e428 100755 --- a/lib/ansible/cli/vault.py +++ b/lib/ansible/cli/vault.py @@ -376,12 +376,15 @@ class VaultCLI(CLI): # Format the encrypted strings and any corresponding stderr output outputs = self._format_output_vault_strings(b_plaintext_list, vault_id=self.encrypt_vault_id) + b_outs = [] for output in outputs: err = output.get('err', None) out = output.get('out', '') if err: sys.stderr.write(err) - print(out) + b_outs.append(to_bytes(out)) + + self.editor.write_data(b'\n'.join(b_outs), context.CLIARGS['output_file'] or '-') if sys.stdout.isatty(): display.display("Encryption successful", stderr=True) diff --git a/test/integration/targets/ansible-vault/runme.sh b/test/integration/targets/ansible-vault/runme.sh index 09de6bd2685..bb893433f08 100755 --- a/test/integration/targets/ansible-vault/runme.sh +++ b/test/integration/targets/ansible-vault/runme.sh @@ -344,6 +344,8 @@ ansible-vault encrypt_string "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" # write to file ansible-vault encrypt_string "$@" --vault-password-file "${NEW_VAULT_PASSWORD}" --name "blippy" "a test string names blippy" --output "${MYTMPDIR}/enc_string_test_file" +[ -f "${MYTMPDIR}/enc_string_test_file" ]; + # test ansible-vault edit with a faux editor ansible-vault encrypt "$@" --vault-password-file vault-password "${TEST_FILE_EDIT}"