diff --git a/lib/ansible/plugins/inventory/toml.py b/lib/ansible/plugins/inventory/toml.py index 9bd439abdfb..31fe95fb56c 100644 --- a/lib/ansible/plugins/inventory/toml.py +++ b/lib/ansible/plugins/inventory/toml.py @@ -95,7 +95,7 @@ import os from functools import partial from ansible.errors import AnsibleFileNotFound, AnsibleParserError -from ansible.module_utils._text import to_bytes, to_native +from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.module_utils.common._collections_compat import MutableMapping, MutableSequence from ansible.module_utils.six import string_types, text_type from ansible.parsing.yaml.objects import AnsibleSequence, AnsibleUnicode @@ -211,8 +211,8 @@ class InventoryModule(BaseFileInventoryPlugin): raise AnsibleFileNotFound("Unable to retrieve file contents", file_name=file_name) try: - with open(b_file_name, 'r') as f: - return toml.load(f) + (b_data, private) = self.loader._get_file_contents(file_name) + return toml.loads(to_text(b_data, errors='surrogate_or_strict')) except toml.TomlDecodeError as e: raise AnsibleParserError( 'TOML file (%s) is invalid: %s' % (file_name, to_native(e)), diff --git a/test/integration/targets/vault/inventory.toml b/test/integration/targets/vault/inventory.toml new file mode 100644 index 00000000000..d97ed398a20 --- /dev/null +++ b/test/integration/targets/vault/inventory.toml @@ -0,0 +1,5 @@ +[vauled_group.hosts] +vaulted_host_toml={ ansible_host="localhost", ansible_connection="local" } + +[vauled_group.vars] +hello="world" diff --git a/test/integration/targets/vault/runme.sh b/test/integration/targets/vault/runme.sh index 6194aa70d2d..8bd35a8869f 100755 --- a/test/integration/targets/vault/runme.sh +++ b/test/integration/targets/vault/runme.sh @@ -1,6 +1,8 @@ #!/usr/bin/env bash set -euvx +source virtualenv.sh + MYTMPDIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir') trap 'rm -rf "${MYTMPDIR}"' EXIT @@ -404,6 +406,14 @@ ansible-playbook test_vault_embedded.yml -i ../../inventory -v "$@" --vault-pass ansible-playbook test_vaulted_inventory.yml -i vaulted.inventory -v "$@" --vault-password-file vault-password ansible-playbook test_vaulted_template.yml -i ../../inventory -v "$@" --vault-password-file vault-password + +# install TOML for parse toml inventory +# test playbooks using vaulted files(toml) +pip install toml +ansible-vault encrypt ./inventory.toml -v "$@" --vault-password-file=./vault-password +ansible-playbook test_vaulted_inventory_toml.yml -i ./inventory.toml -v "$@" --vault-password-file vault-password +ansible-vault decrypt ./inventory.toml -v "$@" --vault-password-file=./vault-password + # test a playbook with a host_var whose value is non-ascii utf8 (see https://github.com/ansible/ansible/issues/37258) ansible-playbook -i ../../inventory -v "$@" --vault-id vault-password test_vaulted_utf8_value.yml diff --git a/test/integration/targets/vault/test_vaulted_inventory_toml.yml b/test/integration/targets/vault/test_vaulted_inventory_toml.yml new file mode 100644 index 00000000000..f6e2c5d6035 --- /dev/null +++ b/test/integration/targets/vault/test_vaulted_inventory_toml.yml @@ -0,0 +1,9 @@ +- hosts: vaulted_host_toml + gather_facts: no + tasks: + - name: See if we knew vaulted_host_toml + debug: msg="Found vaulted_host from vaulted.inventory.toml" + + - assert: + that: + - 'hello=="world"'