Skip to content

Commit

Permalink
Adding module_utils to allow for common utilites that can be shared a…
Browse files Browse the repository at this point in the history
…mong ansible modules. (#2623)

What is the motivation for this PR?
Sometimes, there are utilities that are required for across multiple ansible modules in ansible/library.

For example:
* creating of a timestamped debug file, and printing message to it.
* minigraph_facts has logic to get port_name_to_alias_map that is based on 'hwksu'. If we want to use the same logic in some other ansible module, then we would have to replicate the same code in both the modules.

How did you do it?
Usage of ansible module_utils is defined at https://docs.ansible.com/ansible/latest/dev_guide/developing_module_utilities.html
To have custom module_utils, we need to:
* Add module_utils directory under ansible.
* Add module_utils in ansible.cfg to point to this module_utils directory.

So, we did the following:
* Added module_utils directory under ansible
* Added module_utils in ansible.cfg to use this directory for ansible modules.

Added debug_utils as an example with the following methods/utilities:
* create_debug_file - create a timestamped debug file
* print_debug_msg - print a message to the debug file.

Added debug commands above to conn_graph_facts as an example.

How did you verify/test it?
Made sure that conn_graph_facts works and the debug file is created.
  • Loading branch information
sanmalho-git authored Dec 16, 2020
1 parent 094d80b commit f5537b1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

inventory = /etc/ansible/hosts
library = library:library/ixia
module_utils = module_utils
remote_tmp = $HOME/.ansible/tmp
pattern = *
forks = 5
Expand Down
7 changes: 7 additions & 0 deletions ansible/library/conn_graph_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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']
Expand Down
Empty file.
25 changes: 25 additions & 0 deletions ansible/module_utils/debug_utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit f5537b1

Please sign in to comment.