我想更改介面內的地址型別,但地址型別在串列中。
這是 json 格式的示例。
{
"IPcontrol_static_dns": {
"addressType": "static",
"aliases": [],
"hostname": "dc",
"id": 872084447,
"interfaces": [
{
"addressType": [
"Reserved"
],
"excludeFromDiscovery": "false",
"id": 872084447,
"ipAddress": [
"2.82.50.34"
]
}
],
"resourceRecordFlag": "true"
}
}
這是我的劇本
- name: change dns record to reserve and add value
set_fact:
IPcontrol_static_dns: "{{ check_domain_name.json | combine({'addressType' : 'static' }, { 'resourceRecordFlag': 'true' }, recursive=True) }} "
- name: change dns record to reserve and add value continue
set_fact:
IPcontrol_static_dns: "{{ IPcontrol_static_dns | combine( { 'interfaces': [{ 'addressType': ['static'] }] }, recursive=True) }} "
問題是界面內的所有內容都被洗掉了,只剩下“addresType”了。
我只想修改這個值
結果的一部分
"id": 872084447,
"interfaces": [
{
"addressType": [
"static"
]
}
],
"ipAddress": "2.82.50.34",
"resourceRecordFlag": "true"
uj5u.com熱心網友回復:
這個劇本可以完成這項作業:
- name: test
hosts: localhost
vars:
IP: "{{ lookup('file','filetest.json') | from_json }}"
tasks:
- name: set my variable
debug:
msg: "{{ IP }}"
- name: change json
set_fact:
IP: "{{ IP | combine(dict1, recursive=true) }}"
vars:
dict1: "{{ IP | combine(interface) }}"
ipadr: "{{ IP.IPcontrol_static_dns.interfaces.0.ipAddress.0}}"
interface: { "IPcontrol_static_dns": {"interfaces": [{ "addressType": ["static"]}], 'ipAddress': "{{ipadr}}" }}
- name: result
debug:
msg: "{{ IP }}"
結果:
起始IP:
{
"IPcontrol_static_dns": {
"addressType": "static",
"aliases": [],
"hostname": "dc",
"id": 872084447,
"interfaces": [
{
"addressType": [
"Reserved"
],
"excludeFromDiscovery": "false",
"id": 872084447,
"ipAddress": [
"2.82.50.34"
]
}
],
"resourceRecordFlag": "true"
}
}
最終IP:
ok: [localhost] => {
"msg": {
"IPcontrol_static_dns": {
"addressType": "static",
"aliases": [],
"hostname": "dc",
"id": 872084447,
"interfaces": [
{
"addressType": [
"static"
]
}
],
"ipAddress": "2.82.50.34",
"resourceRecordFlag": "true"
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/367033.html
