From 99ce712259c96c01de590c0f11e5ff7ac192ea06 Mon Sep 17 00:00:00 2001 From: Ondra Machacek Date: Wed, 7 Mar 2018 16:25:32 +0100 Subject: [PATCH] ovirt_host_networks: Fix idempotency (#37075) --- .../cloud/ovirt/ovirt_host_networks.py | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/ansible/modules/cloud/ovirt/ovirt_host_networks.py b/lib/ansible/modules/cloud/ovirt/ovirt_host_networks.py index 20652fa7fa1..eee4fae3402 100644 --- a/lib/ansible/modules/cloud/ovirt/ovirt_host_networks.py +++ b/lib/ansible/modules/cloud/ovirt/ovirt_host_networks.py @@ -283,14 +283,33 @@ def main(): labels = module.params['labels'] nic_name = bond.get('name') if bond else module.params['interface'] - nics_service = hosts_service.host_service(host.id).nics_service() + host_service = hosts_service.host_service(host.id) + nics_service = host_service.nics_service() nic = search_by_name(nics_service, nic_name) + network_names = [network['name'] for network in networks] state = module.params['state'] if ( state == 'present' and (nic is None or host_networks_module.has_update(nics_service.service(nic.id))) ): + # Remove networks which are attached to different interface then user want: + attachments_service = host_service.network_attachments_service() + remove_network_attachments = [ + a for a in attachments_service.list() + if get_link_name(connection, a.network) in network_names and + get_link_name(connection, a.host_nic) != interface + ] + if remove_network_attachments: + host_networks_module.action( + entity=host, + action='setup_networks', + post_action=host_networks_module._action_save_configuration, + check_connectivity=module.params['check'], + removed_network_attachments=remove_network_attachments, + ) + + # Assign the networks: host_networks_module.action( entity=host, action='setup_networks', @@ -343,7 +362,9 @@ def main(): ), ), ], - ) for network in networks + ) for network in networks if network['name'] not in [ + get_link_name(connection, a.network) for a in attachments_service.list() + ] # Attach only networks which are not yet attached. ] if networks else None, ) elif state == 'absent' and nic: @@ -353,7 +374,6 @@ def main(): attachments = attachments_service.list() attached_labels = set([str(lbl.id) for lbl in nic_service.network_labels_service().list()]) if networks: - network_names = [network['name'] for network in networks] attachments = [ attachment for attachment in attachments if get_link_name(connection, attachment.network) in network_names @@ -374,7 +394,7 @@ def main(): ] if bond else None, removed_labels=[ otypes.NetworkLabel(id=str(name)) for name in labels - ], + ] if labels else None, removed_network_attachments=list(attachments), )