更新事实模块ansible
Update facts module ansible
我用 set_fact 创建了一个字典。现在我只想用 update_fact 模块更新值“dnu”。
"PortRCR": [
{
"desc": null,
"dnu": "0",
"lp": "down",
"name": "GigabitEthernet0/0",
"os": "down"
},
{
"desc": "Router",
"dnu": "0",
"lp": "down",
"name": "GigabitEthernet1/0/1",
"os": "up"
},
{
"desc": null,
"dnu": "0",
"lp": "down",
"name": "GigabitEthernet1/0/10",
"os": "down"
我的剧本是这样的:
set_fact:
PortRCR: "{{ PortRCR + [{'name': item.key, 'desc': item.value.description, 'lp': item.value.lineprotocol | default('down', true), 'os': item.value.operstatus, 'dnu': '' | default('0', true) }] }}"
PortRCR_length: "{{ PortRCR | length }}"
cacheable: yes
loop: "{{ ansible_net_interfaces | dict2items }}"
vars:
PortRCR: []
- name: Update the fact
ansible.utils.update_fact:
updates:
- path: PortRCR{{ item }}['dnu']
value: "1"
loop: "{{ range(0, PortRCR_length|int)|list }}"
Ansible 告诉我事实已更改但值未受影响。
如果您查看 the documentation for the update_fact module,您会注意到上面写着:
Variables are not modified in place, instead they are returned by the module.
并且所有示例都显示了使用 register
来捕获更新的值,例如:
- name: Update the fact
ansible.utils.update_fact:
updates:
- path: a.b.c.0
value: 10
- path: "a['b']['c'][1]"
value: 20
register: updated
您需要向 update_fact
块添加 register
指令,但我们需要 post-process 返回值:因为您在循环,你将在 results
键中有多个结果(每个循环迭代一个)。
这似乎对我有用:
- hosts: localhost
gather_facts: false
tasks:
# Here's the original fact as presented in your question
- name: set portrcr fact
set_fact:
"PortRCR": [
{
"desc": null,
"dnu": "0",
"lp": "down",
"name": "GigabitEthernet0/0",
"os": "down"
},
{
"desc": "Router",
"dnu": "0",
"lp": "down",
"name": "GigabitEthernet1/0/1",
"os": "up"
},
{
"desc": null,
"dnu": "0",
"lp": "down",
"name": "GigabitEthernet1/0/10",
"os": "down"
}]
# We use update_fact to update the values and register
# the results in the "updated" variable.
- name: update portrcr fact
ansible.utils.update_fact:
updates:
- path: "PortRCR[{{item}}].dnu"
value: 1
loop: "{{ range(PortRCR|length) }}"
register: updated
# The final iteration of the loop will contain all the changes,
# so we want to replace the value of PortRCR with the value
# from the final loop iteration.
- set_fact:
PortRCR: "{{ updated.results[-1].PortRCR }}"
- debug:
var: PortRCR
运行 上面的剧本产生:
TASK [debug] ********************************************************************************************
ok: [localhost] => {
"PortRCR": [
{
"desc": null,
"dnu": 1,
"lp": "down",
"name": "GigabitEthernet0/0",
"os": "down"
},
{
"desc": "Router",
"dnu": 1,
"lp": "down",
"name": "GigabitEthernet1/0/1",
"os": "up"
},
{
"desc": null,
"dnu": 1,
"lp": "down",
"name": "GigabitEthernet1/0/10",
"os": "down"
}
]
}
我用 set_fact 创建了一个字典。现在我只想用 update_fact 模块更新值“dnu”。
"PortRCR": [
{
"desc": null,
"dnu": "0",
"lp": "down",
"name": "GigabitEthernet0/0",
"os": "down"
},
{
"desc": "Router",
"dnu": "0",
"lp": "down",
"name": "GigabitEthernet1/0/1",
"os": "up"
},
{
"desc": null,
"dnu": "0",
"lp": "down",
"name": "GigabitEthernet1/0/10",
"os": "down"
我的剧本是这样的:
set_fact:
PortRCR: "{{ PortRCR + [{'name': item.key, 'desc': item.value.description, 'lp': item.value.lineprotocol | default('down', true), 'os': item.value.operstatus, 'dnu': '' | default('0', true) }] }}"
PortRCR_length: "{{ PortRCR | length }}"
cacheable: yes
loop: "{{ ansible_net_interfaces | dict2items }}"
vars:
PortRCR: []
- name: Update the fact
ansible.utils.update_fact:
updates:
- path: PortRCR{{ item }}['dnu']
value: "1"
loop: "{{ range(0, PortRCR_length|int)|list }}"
Ansible 告诉我事实已更改但值未受影响。
如果您查看 the documentation for the update_fact module,您会注意到上面写着:
Variables are not modified in place, instead they are returned by the module.
并且所有示例都显示了使用 register
来捕获更新的值,例如:
- name: Update the fact
ansible.utils.update_fact:
updates:
- path: a.b.c.0
value: 10
- path: "a['b']['c'][1]"
value: 20
register: updated
您需要向 update_fact
块添加 register
指令,但我们需要 post-process 返回值:因为您在循环,你将在 results
键中有多个结果(每个循环迭代一个)。
这似乎对我有用:
- hosts: localhost
gather_facts: false
tasks:
# Here's the original fact as presented in your question
- name: set portrcr fact
set_fact:
"PortRCR": [
{
"desc": null,
"dnu": "0",
"lp": "down",
"name": "GigabitEthernet0/0",
"os": "down"
},
{
"desc": "Router",
"dnu": "0",
"lp": "down",
"name": "GigabitEthernet1/0/1",
"os": "up"
},
{
"desc": null,
"dnu": "0",
"lp": "down",
"name": "GigabitEthernet1/0/10",
"os": "down"
}]
# We use update_fact to update the values and register
# the results in the "updated" variable.
- name: update portrcr fact
ansible.utils.update_fact:
updates:
- path: "PortRCR[{{item}}].dnu"
value: 1
loop: "{{ range(PortRCR|length) }}"
register: updated
# The final iteration of the loop will contain all the changes,
# so we want to replace the value of PortRCR with the value
# from the final loop iteration.
- set_fact:
PortRCR: "{{ updated.results[-1].PortRCR }}"
- debug:
var: PortRCR
运行 上面的剧本产生:
TASK [debug] ********************************************************************************************
ok: [localhost] => {
"PortRCR": [
{
"desc": null,
"dnu": 1,
"lp": "down",
"name": "GigabitEthernet0/0",
"os": "down"
},
{
"desc": "Router",
"dnu": 1,
"lp": "down",
"name": "GigabitEthernet1/0/1",
"os": "up"
},
{
"desc": null,
"dnu": 1,
"lp": "down",
"name": "GigabitEthernet1/0/10",
"os": "down"
}
]
}