使用Ansible json_query,可以轻松地将JSON数据转换为Python数据类型。要使用该功能,您需要安装jmespath插件,然后使用json_query筛选器。以下是一个示例:
假设您有以下JSON数据:
{
"name": "John",
"age": 30,
"city": "New York"
}
要将其转换为Python字典,请使用以下Ansible Playbook:
- name: Convert JSON to Python dictionary using json_query
hosts: localhost
gather_facts: false
vars:
my_json: '{"name": "John", "age": 30, "city": "New York"}'
tasks:
- name: Convert JSON to Python dictionary
debug:
var: (my_json | from_json | json_query("."))
在此Playbook中,我们首先将JSON数据存储在名为my_json的变量中。然后,我们使用from_json筛选器将其转换为Python字典,并使用json_query筛选器将其转换为Python数据类型。
输出将是以下Python字典:
{'name': 'John', 'age': 30, 'city': 'New York'}