Skip to content

Commit dd9f8f1

Browse files
authored
Workaround for ansible 2.13.13 customized module ImportError (sonic-net#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 <xiwang5@microsoft.com>
1 parent 49f76fe commit dd9f8f1

File tree

4 files changed

+64
-62
lines changed

4 files changed

+64
-62
lines changed

ansible/config_sonic_basedon_testbed.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161

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

ansible/dualtor/config_simulated_y_cable.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
when: restart_pmon is not defined and restart_pmon|bool == true
1515

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

2023
- name: Set y cable simulator server address
2124
set_fact:
22-
mux_simulator_server: "{{ vmhost_server_address }}"
25+
mux_simulator_server: "{{ vmhost_server_address.stdout }}"
2326

2427
- name: Set default y cable simulator server port
2528
set_fact:

ansible/library/vmhost_server_info.py

-59
This file was deleted.
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""This script is to parse ansible inventory file and return the mgmt IP for given host server.
2+
"""
3+
4+
import argparse
5+
import sys
6+
7+
from ansible.parsing.dataloader import DataLoader
8+
from ansible.inventory.manager import InventoryManager
9+
10+
11+
def main(args):
12+
server_name = args.server_name
13+
inv_file = args.inv_file
14+
ip_ver = args.ip_ver
15+
16+
inv_mgr = InventoryManager(loader=DataLoader(), sources=inv_file)
17+
all_hosts = inv_mgr.get_hosts(pattern=server_name)
18+
19+
if len(all_hosts) == 0:
20+
sys.stderr.write("No host matches {} in inventory file {}".format(server_name, inv_file))
21+
sys.exit(1)
22+
else:
23+
for host in all_hosts:
24+
if host.name.startswith('VM'):
25+
continue
26+
if ip_ver == 'ipv4':
27+
result = host.get_vars().get("ansible_host", "")
28+
else:
29+
result = host.get_vars().get("ansible_hostv6", "")
30+
sys.stdout.write(result)
31+
sys.exit(0)
32+
33+
sys.stderr.write(
34+
"Unable to find IP address of host server {} in inventory file {}".format(server_name, inv_file)
35+
)
36+
sys.exit(2)
37+
38+
39+
if __name__ == "__main__":
40+
41+
parser = argparse.ArgumentParser(description='Gather mgmt IP for given host server (like server_17)')
42+
parser.add_argument(
43+
'--server-name',
44+
help='The name of vm_host server, like server_1'
45+
)
46+
parser.add_argument(
47+
'--inv-file',
48+
default='veos',
49+
help='The inventory file contains server information. Default is veos.'
50+
)
51+
parser.add_argument(
52+
'--ip-ver',
53+
default='ipv4',
54+
choices=['ipv4', 'ipv6'],
55+
help='The IP version to return. Default is ipv4.'
56+
)
57+
args = parser.parse_args()
58+
main(args)

0 commit comments

Comments
 (0)