Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding module_utils to allow for common utilites that can be shared among ansible modules. #2623

Merged
merged 2 commits into from Dec 16, 2020

Conversation

sanmalho-git
Copy link
Contributor

Description of PR

Summary:
Fixes # (issue)

Type of change

  • Bug fix
  • Testbed and Framework(new/improvement)
  • Test case(new/improvement)

Approach

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.

Any platform specific information?

Supported testbed topology if it's a new test case?

Documentation

Copy link
Collaborator

@wangxin wangxin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea of introducing module_utils is great!

Just got concern with passing debug file name to function create_debug_file.


def create_debug_file(debug_fname):
curtime = datetime.datetime.now().isoformat()
timed_debug_fname = debug_fname % curtime
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires that the debug_fname must be a template string. Without looking into the code, user may pass in an ordinary string as debug file name. Can this be improved?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added add_timestamp option to create_debug_file which defaults to True. If specified it will add the timestamp in between the extension (if present) and the base file name. Otherwise it will append timestamp to the end of the passed filename.

If add_timestamp is False, we will create the file with the passed filename itself with no timestamp.

Sometimes, there are utilities that are required for across multiple
ansible modules in ansible/library.

This commit adds module_utils directory under ansible and 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.
Added add_timestamp optional arg to create_debug_file.
If add_timestamp is true, then
  if there is an extension (like .txt) in the passed filename, then
    insert curtime before the extension
  else
    insert curtime at the end
  create file with name that has timestamp added to the passed filename
else
  create file with name as the passed filename
@wangxin wangxin merged commit f5537b1 into sonic-net:master Dec 16, 2020
@sanmalho-git sanmalho-git deleted the module_utils branch December 16, 2020 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants