1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-07-08 19:49:09 +00:00

[PR #12081/8faf8c38 backport][stable-13] Add from_toml filter (#12081) (#12125)

Add `from_toml` filter (#12081) (#12081)

* Add `from_toml` filter (#12081)

* Use tasks/main.yml instead of runme.sh for integration tests

* Renamed filter back to to_toml.py, moved from_toml filter to its own file making use of python's native tomllib

* Remove task to install tomlkit library as it's no longer required for the from_toml filter

* Replace deprecated t.Mapping with collections.abc.Mapping (https://docs.python.org/3/library/typing.html#typing.Mapping)

* Type is not determined when function is called. Let isinstance check ensure value has string type.

(cherry picked from commit 8faf8c3838)

Co-authored-by: spike77453 <spike77453@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2026-05-30 15:11:41 +02:00 committed by GitHub
parent ab08561421
commit 06feb91f2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 131 additions and 0 deletions

View file

@ -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

View file

@ -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"