Skip to content

Commit

Permalink
Workaround for ansible 2.13.13 customized module ImportError (sonic-n…
Browse files Browse the repository at this point in the history
…et#11804)

What is the motivation for this PR?
After upgraded ansible version to core 2.13.13 in sonic-mgmt docker, customized ansible modules can no longer import modules from the ansible package. They can only import modules under ansible.module_utils.

For example, the vmhost_server_info module has below imports:

from ansible.parsing.dataloader import DataLoader
from ansible.inventory.manager import InventoryManager
Run this module using ansible 2.13.13 will raise ImportError "ansible.module_utils.common.yaml not found.".

How did you do it?
This workaround is to convert the vmhost_server_info module to an ordinary script and use ansible script module to directly run it.

This change also fixed a bug of building vlan_intfs list. This bug is only triggered in new ansible version.

How did you verify/test it?
Tested deploy-mg on VS setup.

Signed-off-by: Xin Wang <[email protected]>
  • Loading branch information
wangxin authored Feb 29, 2024
1 parent 49f76fe commit dd9f8f1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 62 deletions.
2 changes: 1 addition & 1 deletion ansible/config_sonic_basedon_testbed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@

- name: find all vlan interface names for T0 topology
set_fact:
vlan_intfs: "{{ vlan_intfs|default([])}} + ['{{ port_alias[item] }}' ]"
vlan_intfs: "{{ vlan_intfs|default([]) + [port_alias[item]] }}"
with_items: "{{ host_if_indexes }}"
when: "('host_interfaces_by_dut' in vm_topo_config) and ('tor' in vm_topo_config['dut_type'] | lower)"

Expand Down
7 changes: 5 additions & 2 deletions ansible/dualtor/config_simulated_y_cable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
when: restart_pmon is not defined and restart_pmon|bool == true

- name: Get host server address
vmhost_server_info: vmhost_server_name={{ testbed_facts['server'] }} vm_file={{ vm_file }}
script: scripts/vmhost_server_address.py --inv-file veos_vtb --server-name server_1
args:
executable: python
delegate_to: localhost
register: vmhost_server_address

- name: Set y cable simulator server address
set_fact:
mux_simulator_server: "{{ vmhost_server_address }}"
mux_simulator_server: "{{ vmhost_server_address.stdout }}"

- name: Set default y cable simulator server port
set_fact:
Expand Down
59 changes: 0 additions & 59 deletions ansible/library/vmhost_server_info.py

This file was deleted.

58 changes: 58 additions & 0 deletions ansible/scripts/vmhost_server_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""This script is to parse ansible inventory file and return the mgmt IP for given host server.
"""

import argparse
import sys

from ansible.parsing.dataloader import DataLoader
from ansible.inventory.manager import InventoryManager


def main(args):
server_name = args.server_name
inv_file = args.inv_file
ip_ver = args.ip_ver

inv_mgr = InventoryManager(loader=DataLoader(), sources=inv_file)
all_hosts = inv_mgr.get_hosts(pattern=server_name)

if len(all_hosts) == 0:
sys.stderr.write("No host matches {} in inventory file {}".format(server_name, inv_file))
sys.exit(1)
else:
for host in all_hosts:
if host.name.startswith('VM'):
continue
if ip_ver == 'ipv4':
result = host.get_vars().get("ansible_host", "")
else:
result = host.get_vars().get("ansible_hostv6", "")
sys.stdout.write(result)
sys.exit(0)

sys.stderr.write(
"Unable to find IP address of host server {} in inventory file {}".format(server_name, inv_file)
)
sys.exit(2)


if __name__ == "__main__":

parser = argparse.ArgumentParser(description='Gather mgmt IP for given host server (like server_17)')
parser.add_argument(
'--server-name',
help='The name of vm_host server, like server_1'
)
parser.add_argument(
'--inv-file',
default='veos',
help='The inventory file contains server information. Default is veos.'
)
parser.add_argument(
'--ip-ver',
default='ipv4',
choices=['ipv4', 'ipv6'],
help='The IP version to return. Default is ipv4.'
)
args = parser.parse_args()
main(args)

0 comments on commit dd9f8f1

Please sign in to comment.