forked from sonic-net/sonic-mgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workaround for ansible 2.13.13 customized module ImportError (sonic-n…
…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
Showing
4 changed files
with
64 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |