我怎样才能修改已注册的变量?
例如,我azure_network_interface_info.networkinterfaces[0].ip_configurations
想public_ip_allocation_method
将其更改为Static
或Dynamic
。然后ip_configurations
在修改数组后在其他地方使用该数组。
- name: Get facts for one network interface
azure_rm_networkinterface_info:
resource_group: "{{ resource_group }}"
name: "{{ azure_vm_network_interface }}"
register: azure_network_interface_info
注册变量的输出:
"ip_configurations": [
{
"application_gateway_backend_address_pools": null,
"application_security_groups": null,
"load_balancer_backend_address_pools": null,
"name": "Ubuntu915",
"primary": true,
"private_ip_address": "10.0.0.5",
"private_ip_address_version": "IPv4",
"private_ip_allocation_method": "Dynamic",
"public_ip_address": "/subscriptions/123456789/resourceGroups/test-resource-group/providers/Microsoft.Network/publicIPAddresses/Ubuntu-915-test",
"public_ip_address_name": "/subscriptions/123456789/resourceGroups/test-resource-group/providers/Microsoft.Network/publicIPAddresses/Ubuntu-915-test",
"public_ip_allocation_method": null
}
],
答案1
虽然无法在某些地方更新变量,但是你可以使用update_fact
模块 – 更新当前设置的事实创建其他数据结构,例如
updated.azure_network_interface_info
并使用那个。看来其中一个文档示例恰好涵盖了您的用例。
文档
更多问答
-
Ansible 无法“更新”变量。您只能创建新变量。在许多情况下,您可以通过用新变量替换目标变量来获得所需的结果