diff --git a/ansible/ansible.cfg b/ansible/ansible.cfg index 866e4677e80..22b1d8ab473 100644 --- a/ansible/ansible.cfg +++ b/ansible/ansible.cfg @@ -13,6 +13,7 @@ inventory = /etc/ansible/hosts library = library:library/ixia +module_utils = module_utils remote_tmp = $HOME/.ansible/tmp pattern = * forks = 5 diff --git a/ansible/library/conn_graph_facts.py b/ansible/library/conn_graph_facts.py index b0288cef96c..1cf893b2022 100755 --- a/ansible/library/conn_graph_facts.py +++ b/ansible/library/conn_graph_facts.py @@ -8,6 +8,7 @@ from operator import itemgetter from itertools import groupby from collections import defaultdict +from ansible.module_utils.debug_utils import create_debug_file, print_debug_msg DOCUMENTATION=''' module: conn_graph_facts.py @@ -270,10 +271,12 @@ def find_graph(hostnames): # Finding the graph file contains all duts from hostnames, for fn in file_list: + print_debug_msg(debug_fname, "Looking at conn graph file: %s for hosts %s" % (fn, hostnames)) filename = os.path.join(LAB_GRAPHFILE_PATH, fn) lab_graph = Parse_Lab_Graph(filename) lab_graph.parse_graph() if lab_graph.contains_hosts(hostnames): + print_debug_msg(debug_fname, ("Returning lab graph from conn graph file: %s for hosts %s" % (fn, hostnames))) return lab_graph # Fallback to return an empty connection graph, this is @@ -284,6 +287,8 @@ def find_graph(hostnames): lab_graph.parse_graph() return lab_graph +debug_fname = None + def main(): module = AnsibleModule( argument_spec=dict( @@ -297,6 +302,8 @@ def main(): supports_check_mode=True ) m_args = module.params + global debug_fname + debug_fname = create_debug_file("/tmp/conn_graph_debug.txt") hostnames = m_args['hosts'] anchor = m_args['anchor'] diff --git a/ansible/module_utils/__init__.py b/ansible/module_utils/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/ansible/module_utils/debug_utils.py b/ansible/module_utils/debug_utils.py new file mode 100644 index 00000000000..a5273cb64e1 --- /dev/null +++ b/ansible/module_utils/debug_utils.py @@ -0,0 +1,25 @@ +import os +from pprint import pprint +from ansible.module_utils.basic import datetime + + +def create_debug_file(debug_fname, add_timestamp=True): + curtime = datetime.datetime.now().isoformat() + if add_timestamp: + # Check if there is an extension + fname_split = os.path.splitext(debug_fname) + if fname_split[1] != '': + # We have an extension + timed_debug_fname = (fname_split[0] + ".%s" + fname_split[1]) % curtime + else: + timed_debug_fname = (fname_split[0] + ".%s") % curtime + else: + timed_debug_fname = debug_fname + if os.path.exists(timed_debug_fname) and os.path.isfile(timed_debug_fname): + os.remove(timed_debug_fname) + return timed_debug_fname + + +def print_debug_msg(debug_fname, msg): + with open(debug_fname, 'a') as fp: + pprint(msg, fp)