比较2json个文件,并根据第2个文件(file2.yml)将其差异复制到另一个文件中(file2.yml) |可靠的
Compare 2 json file and copy the difference of it to another file based on 2nd file(file2.yml) | ansible
我正在尝试比较 2 个 json 文件并通过 ansible 将其差异打印到另一个文件,但无法这样做。
我试过 Copy 和 ansible.utils.fact_diff 模块,但没有成功
file1.yml
"from": "abc@gmail.com",
"is_enabled": "true",
"is_local": "true",
"is_data": "true",
"isin_subject": "false",
file2.yml
"from": "xyz@gmail.com",
"is_enabled": "false",
"is_local": "true",
"is_data": "true",
"isin_subject": "false",
输出:file3.yml
"from": "xyz@gmail.com",
"is_enabled": "false",
只需循环字典并比较:
source0.json:
{
"from": "abc@gmail.com",
"is_enabled": "true",
"is_local": "true",
"is_data": "true",
"isin_subject": "false"
}
source1.json:
{
"from": "xyz@gmail.com",
"is_enabled": "false",
"is_local": "true",
"is_data": "true",
"isin_subject": "false"
}
玩书:
- hosts: localhost
gather_facts: false
vars:
json0: "{{ lookup('file', 'source0.json') | from_json }}"
json1: "{{ lookup('file', 'source1.json') | from_json }}"
tasks:
- name: diff
set_fact:
diff: "{{ diff | d({}) | combine({item.key: item.value}) }}"
loop: "{{ json1 | dict2items }}"
when: item.value != json0[item.key]
- name: display diff
debug:
msg: "{{ diff }}"
结果:
ok: [localhost] => {
"msg": {
"from": "xyz@gmail.com",
"is_enabled": "false"
}
}
我正在尝试比较 2 个 json 文件并通过 ansible 将其差异打印到另一个文件,但无法这样做。
我试过 Copy 和 ansible.utils.fact_diff 模块,但没有成功
file1.yml
"from": "abc@gmail.com",
"is_enabled": "true",
"is_local": "true",
"is_data": "true",
"isin_subject": "false",
file2.yml
"from": "xyz@gmail.com",
"is_enabled": "false",
"is_local": "true",
"is_data": "true",
"isin_subject": "false",
输出:file3.yml
"from": "xyz@gmail.com",
"is_enabled": "false",
只需循环字典并比较:
source0.json:
{
"from": "abc@gmail.com",
"is_enabled": "true",
"is_local": "true",
"is_data": "true",
"isin_subject": "false"
}
source1.json:
{
"from": "xyz@gmail.com",
"is_enabled": "false",
"is_local": "true",
"is_data": "true",
"isin_subject": "false"
}
玩书:
- hosts: localhost
gather_facts: false
vars:
json0: "{{ lookup('file', 'source0.json') | from_json }}"
json1: "{{ lookup('file', 'source1.json') | from_json }}"
tasks:
- name: diff
set_fact:
diff: "{{ diff | d({}) | combine({item.key: item.value}) }}"
loop: "{{ json1 | dict2items }}"
when: item.value != json0[item.key]
- name: display diff
debug:
msg: "{{ diff }}"
结果:
ok: [localhost] => {
"msg": {
"from": "xyz@gmail.com",
"is_enabled": "false"
}
}