Ansible,行模块未将JSON数据正确保存到文件中



我有一个剧本,它设置变量"certData";它是一组字典。这就是";certData";看起来像:

ok: [localhost] => {
"msg": [
{
"cn": "node1.corp.com",
"expires": "2020-11-05T15:20:18+00:00",
"id": 705,
"serial": "111"
},
{
"cn": "node2.corp.com",
"expires": "2020-11-05T15:20:28+00:00",
"id": 728,
"serial": "222"
},
{
"cn": "node3.corp.com",
"expires": "2020-10-28T19:37:29+00:00",
"id": 670,
"serial": "333"
},
{
"cn": "node4.corp.com",
"expires": "2021-04-04T09:55:08+00:00",
"id": 1163,
"serial": "444"
},
{
"cn": "node5.corp.com",
"expires": "2020-11-05T15:20:22+00:00",
"id": 715,
"serial": "555"
}
]
}

我有一个剧本,它从";certData";并保存一个名为cert_expiring.json的文件,如下所示:

- name: Testing jinja
lineinfile:
dest: "./cert_expiring.json"
line: |-
{
"devices": [{
"items": {
{%- for i in certData -%}
{{ "" if loop.first else "," }}
"{{ i.id }}": "{{ i.cn }}"
{%- endfor %}

}
}]
}
state: present
create: yes

剧本运行成功,但给出以下警告:

[WARNING]: The value {'devices': [{'items': {'705': 'node1.corp.com', '715': 'node5.corp.com', '670': 'node3.corp.com', '728': 'node2.corp.com', '1163':
'node4.corp.com'}}]} (type dict) in a string field was converted to u"{'devices': [{'items': {'705': 'node1.corp.com', '715': 'node5.corp.com', '670':
'node3.corp.com', '728': 'node2.corp.com', '1163': 'node4.corp.com'}}]}" (type string). If this does not look like what you expect, quote the entire value to ensure it does not
change.

来自警告的数据正是它如何被打印到";cert_expiring.json";带有单引号的文件。

certs_expiring.json:

{'devices': [{'items': {'705': 'node1.corp.com', '715': 'node5.corp.com', '670': 'node3.corp.com', '728': 'node2.corp.com', '1163': 'node4.corp.com'}}]}

但是,当我加载";cert_expiring.json";返回到一个变量(expired_certs2(并打印它,它会正确打印:

- name: Store file contents into variable
include_vars:
file: cert_expiring.json
name: expired_certs2

- name: Print expired_certs2 variable.
debug:
msg: "{{ expired_certs2 | json_query('devices[*].items') }}"
TASK [Print expired_certs2 variable.] **********************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
{
"1163": "DUKEISEC12.corp.cox.com",
"670": "sage.stg.cox.com",
"705": "btnrssmoloch01.corp.cox.com",
"715": "elmwssmoloch01.corp.cox.com",
"728": "rnkessmoloch01.corp.cox.com"
}
]
}

我试过这样单独引用Jinja代码,但没有帮助:

- name: Testing jinja
lineinfile:
dest: "./cert_expiring.json"
line: |-
'{
"devices": [{
"items": {
{%- for i in certData -%}
{{ "" if loop.first else "," }}
"{{ i.id }}": "{{ i.cn }}"
{%- endfor %}

}
}]
}'
state: present
create: yes

此外,尝试使用双引号,但没有成功。

如何将数据打印到";cert_expiring.json";文件是否为正确的json格式?

Q:">如何将数据打印到";cert_expiring.json";文件是否为正确的JSON格式">

A: 使用过滤器to_json,例如

- include_vars:
file: cert_expiring.json
name: expired_certs2
- debug:
msg: "{{ expired_certs2 | to_json }}"

给出

msg: '{"devices": [{"item": {"705": "node1.corp.com", "728": "node2.corp.com", "670": "node3.corp.com", "1163": "node4.corp.com", "715": "node5.corp.com"}}]}'

查询

- debug:
msg: "{{ expired_certs2.devices | json_query('[].item') | to_json }}"

给出

msg: '[{"705": "node1.corp.com", "728": "node2.corp.com", "670": "node3.corp.com", "1163": "node4.corp.com", "715": "node5.corp.com"}]'

可以简化内容,例如

- set_fact:
devices: "{{ devices | default([]) +
[{'item': certData | items2dict(key_name='id', value_name='cn')}] }}"
- copy:
dest: cert_expiring.json
content: >
{"devices": {{ devices | to_json }} }

给出

shell> cat cert_expiring.json
{"devices": [{"item": {"705": "node1.corp.com", "728": "node2.corp.com", "670": "node3.corp.com", "1163": "node4.corp.com", "715": "node5.corp.com"}}] }

相关内容

  • 没有找到相关文章

最新更新