使用Ansible更新Azure网络接口时遇ip_configurations类型错误求助
Ansible更新Azure网络接口时的ip_configurations错误排查
错误现象
使用Ansible更新Azure网络接口时,遇到两个关联错误:
- 传递处理后的IP配置时,报错:
ip_configurations must by a list, not 'list' - 手动写成列表形式后,又出现:
AttributeError:'str' object has no attribute 'get'
涉及的Playbook代码
- json_modify: data: "{{ azure_network_interface_info }}" pointer: "/networkinterfaces/0/ip_configurations/0" action: update update: { "public_ip_allocation_method": "Dynamic" } register: azure_network_interface_info_modified - debug: var: azure_network_interface_info_modified.result.networkinterfaces[0].ip_configurations - name: Applying NSG to target NIC azure_rm_networkinterface: name: "{{ azure_vm_network_interface }}" resource_group: "{{ resource_group }}" subnet_name: "{{ azure_network_interface_info.networkinterfaces[0].subnet }}" virtual_network: "{{ azure_network_interface_info.networkinterfaces[0].virtual_network.name }}" ip_configurations: "{{ [ azure_network_interface_info_modified.result.networkinterfaces[0].ip_configurations ] }}" security_group: "/subscriptions/123456/resourceGroups/cloud-shell-storage/providers/Microsoft.Network/networkSecurityGroups/testing_temp_8"
收到的错误信息
"msg": "Value '[{'name': 'Ubuntu915', 'private_ip_address': '10.0.0.5',
'private_ip_allocation_method': 'Dynamic', 'primary': True, 'load_balancer_backend_address_pools': None,
'application_gateway_backend_address_pools': None, 'public_ip_address': '/subscriptions/123456/resourceGroups/cloud-shell-
storage/providers/Microsoft.Network/publicIPAddresses/Ubuntu-915-test', 'public_ip_allocation_method': 'Dynamic',
'application_security_groups': None}]' in the sub parameter field 'ip_configurations' must by a list, not 'list'"
问题分析
- 第一个错误根源:
azure_network_interface_info_modified.result.networkinterfaces[0].ip_configurations本身已经是列表类型,用[]再次包裹后变成「列表嵌套列表」,模块无法识别该格式,因此报错。 - 第二个错误原因:手动配置时,
primary: "{{ test2 }}"会将布尔值转为字符串类型(比如"True"而非原生布尔值True),模块处理时尝试调用字符串的get()方法,触发AttributeError。
解决方案
1. 修复列表嵌套问题
直接传递原列表,去掉外层多余的[]:
- name: Applying NSG to target NIC azure_rm_networkinterface: name: "{{ azure_vm_network_interface }}" resource_group: "{{ resource_group }}" subnet_name: "{{ azure_network_interface_info.networkinterfaces[0].subnet }}" virtual_network: "{{ azure_network_interface_info.networkinterfaces[0].virtual_network.name }}" ip_configurations: "{{ azure_network_interface_info_modified.result.networkinterfaces[0].ip_configurations }}" security_group: "/subscriptions/123456/resourceGroups/cloud-shell-storage/providers/Microsoft.Network/networkSecurityGroups/testing_temp_8"
2. 确保字段类型正确(手动配置场景)
如果需要手动指定ip_configurations,用bool过滤器将字符串转为原生布尔值:
ip_configurations: - name: "{{ test1 }}" primary: "{{ test2 | bool }}"
内容的提问来源于stack exchange,提问作者user630702
相关产品推荐
相关产品推荐

