diff --git a/.github/BOTMETA.yml b/.github/BOTMETA.yml index 9d5cda41ae..e908c67cff 100644 --- a/.github/BOTMETA.yml +++ b/.github/BOTMETA.yml @@ -159,6 +159,10 @@ files: maintainers: Ajpantuso $filters/from_ini.py: maintainers: sscheib + $filters/from_toml.py: + maintainers: spike77453 + $filters/from_toml.yml: + maintainers: spike77453 $filters/groupby_as_dict.py: maintainers: felixfontein $filters/hashids.py: diff --git a/plugins/filter/from_toml.py b/plugins/filter/from_toml.py new file mode 100644 index 0000000000..fc9c301f5d --- /dev/null +++ b/plugins/filter/from_toml.py @@ -0,0 +1,24 @@ +# Copyright (c) Contributors to the Ansible project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +from __future__ import annotations + +import typing as t +from collections.abc import Mapping + +import tomllib +from ansible.errors import AnsibleFilterError + + +def from_toml(value: t.Any) -> Mapping: + if not isinstance(value, str): + raise AnsibleFilterError("from_toml only accepts strings.") + return tomllib.loads(value) + + +class FilterModule: + def filters(self): + return { + "from_toml": from_toml, + } diff --git a/plugins/filter/from_toml.yml b/plugins/filter/from_toml.yml new file mode 100644 index 0000000000..a2fe200430 --- /dev/null +++ b/plugins/filter/from_toml.yml @@ -0,0 +1,37 @@ +--- +# Copyright (c) Contributors to the Ansible project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +DOCUMENTATION: + name: from_toml + author: + - Christian Schürmann (@spike77453) + version_added: 13.1.0 + short_description: Convert TOML string into dictionary + description: + - Converts a string containing a TOML document into an Ansible variable + positional: _input + options: + _input: + description: + - A string that contains a TOML document. + type: str + required: true + +EXAMPLES: | + - name: Slurp TOML file + ansible.builtin.slurp: + src: config.toml + register: config + + - name: Convert data into dictionary + ansible.builtin.set_fact: + my_config: "{{ config['content'] | b64decode | community.general.from_toml }}" + + +RETURN: + _value: + description: + - Dictionary representing the data from the TOML string. + type: dict diff --git a/tests/integration/targets/filter_from_toml/tasks/main.yml b/tests/integration/targets/filter_from_toml/tasks/main.yml new file mode 100644 index 0000000000..4291dc1a40 --- /dev/null +++ b/tests/integration/targets/filter_from_toml/tasks/main.yml @@ -0,0 +1,12 @@ +--- +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +- name: Convert TOML string to dictionary + set_fact: + actual_dict: "{{ toml_document | community.general.from_toml }}" + +- assert: + that: + - expected_dict == actual_dict diff --git a/tests/integration/targets/filter_from_toml/vars/main.yml b/tests/integration/targets/filter_from_toml/vars/main.yml new file mode 100644 index 0000000000..1b5099fd1a --- /dev/null +++ b/tests/integration/targets/filter_from_toml/vars/main.yml @@ -0,0 +1,54 @@ +--- +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +toml_document: |- + # This is a TOML document + + title = "TOML Example" + + [owner] + name = "Tom Preston-Werner" + dob = 1979-05-27T07:32:00-08:00 + + [database] + enabled = true + ports = [ 8000, 8001, 8002 ] + data = [ ["delta", "phi"], [3.14] ] + temp_targets = { cpu = 79.5, case = 72.0 } + + [servers] + + [servers.alpha] + ip = "10.0.0.1" + role = "frontend" + + [servers.beta] + ip = "10.0.0.2" + role = "backend" + +expected_dict: + "title": "TOML Example" + "owner": + "name": "Tom Preston-Werner" + "dob": 1979-05-27T07:32:00-08:00 + "database": + "enabled": true + "ports": + - 8000 + - 8001 + - 8002 + "data": + - ["delta", "phi"] + - [3.14] + "temp_targets": + "case": 72.0 + "cpu": 79.5 + "servers": + "alpha": + "ip": "10.0.0.1" + "role": "frontend" + "beta": + "ip": "10.0.0.2" + "role": "backend"