Developing network resource modules

The resource module builder is an Ansible Playbook that helps developers scaffold and maintain an Ansible network resource module.

The resource module builder has the following capabilities:

  • Uses a defined model to scaffold a resource module directory layout and initial class files.
  • Scaffolds either an Ansible role or a collection.
  • Subsequent uses of the resource module builder will only replace the module arspec and file containing the module docstring.
  • Allows you to store complex examples along side the model in the same directory.
  • Maintains the model as the source of truth for the module and use resource module builder to update the source files as needed.
  • Generates working sample modules for both <networkos><resource> and <network_os>_facts.

Accessing the resource module builder

To access the resource module builder:

  • clone the github repository:
  • Install the requirements:
  1. pip install -r requirements.txt

Creating a model

You must create a model for your new resource. The resource module builder uses this model to create:

  • The scaffold for a new module
  • The argspec for the new module
  • The docstring for the new module

The model is then the single source of truth for both the argspec and docstring, keeping them in sync. Use the resource module builder to generate this scaffolding. For any subsequent updates to the module, update the model first and use the resource module builder to update the module argspec and docstring.

For example, the resource model builder includes the myos_interfaces.yml sample in the models directory, as seen below:

  1. ---
  2. GENERATOR_VERSION: '1.0'
  3. ANSIBLE_METADATA: |
  4. {
  5. 'metadata_version': '1.1',
  6. 'status': ['preview'],
  7. 'supported_by': '<support_group>'
  8. }
  9. NETWORK_OS: myos
  10. RESOURCE: interfaces
  11. COPYRIGHT: Copyright 2019 Red Hat
  12. LICENSE: gpl-3.0.txt
  13.  
  14. DOCUMENTATION: |
  15. module: myos_interfaces
  16. version_added: 2.9
  17. short_description: 'Manages <xxxx> attributes of <network_os> <resource>'
  18. description: 'Manages <xxxx> attributes of <network_os> <resource>.'
  19. author: Ansible Network Engineer
  20. notes:
  21. - 'Tested against <network_os> <version>'
  22. options:
  23. config:
  24. description: The provided configuration
  25. type: list
  26. elements: dict
  27. suboptions:
  28. name:
  29. type: str
  30. description: The name of the <resource>
  31. some_string:
  32. type: str
  33. description:
  34. - The some_string_01
  35. choices:
  36. - choice_a
  37. - choice_b
  38. - choice_c
  39. default: choice_a
  40. some_bool:
  41. description:
  42. - The some_bool.
  43. type: bool
  44. some_int:
  45. description:
  46. - The some_int.
  47. type: int
  48. version_added: '1.1'
  49. some_dict:
  50. type: dict
  51. description:
  52. - The some_dict.
  53. suboptions:
  54. property_01:
  55. description:
  56. - The property_01
  57. type: str
  58. state:
  59. description:
  60. - The state of the configuration after module completion.
  61. type: str
  62. choices:
  63. - merged
  64. - replaced
  65. - overridden
  66. - deleted
  67. default: merged
  68. EXAMPLES:
  69. - deleted_example_01.txt
  70. - merged_example_01.txt
  71. - overridden_example_01.txt
  72. - replaced_example_01.txt

Notice that you should include examples for each of the states that the resource supports. The resource module builder also includes these in the sample model.

See Ansible network resource models for more examples.

Using the resource module builder

To use the resource module builder to create a collection scaffold from your resource model:

  1. ansible-playbook -e rm_dest=<destination for modules and module utils> \
  2. -e structure=collection \
  3. -e collection_org=<collection_org> \
  4. -e collection_name=<collection_name> \
  5. -e model=<model> \
  6. site.yml

Where the parameters are as follows:

  • rm_dest: The directory where the resource module builder places the files and directories for the resource module and facts modules.
  • structure: The directory layout type (role or collection)
    • role: Generate a role directory layout.
    • collection: Generate a collection directory layout.
  • collectionorg: The organization of the collection, required when _structure=collection.
  • collectionname: The name of the collection, required when _structure=collection.
  • model: The path to the model file.

To use the resource module builder to create a role scaffold:

  1. ansible-playbook -e rm_dest=<destination for modules and module utils> \
  2. -e structure=role \
  3. -e model=<model> \
  4. site.yml

Examples

Collection directory layout

This example shows the directory layout for the following:

  • network_os: myos
  • resource: interfaces
  1. ansible-playbook -e rm_dest=~/github/rm_example \
  2. -e structure=collection \
  3. -e collection_org=cidrblock \
  4. -e collection_name=my_collection \
  5. -e model=models/myos/interfaces/myos_interfaces.yml \
  6. site.yml
  1. ├── docs
  2. ├── LICENSE.txt
  3. ├── playbooks
  4. ├── plugins
  5. | ├── action
  6. | ├── filter
  7. | ├── inventory
  8. | ├── modules
  9. | | ├── __init__.py
  10. | | ├── myos_facts.py
  11. | | └── myos_interfaces.py
  12. | └── module_utils
  13. | ├── __init__.py
  14. | └── network
  15. | ├── __init__.py
  16. | └── myos
  17. | ├── argspec
  18. | | ├── facts
  19. | | | ├── facts.py
  20. | | | └── __init__.py
  21. | | ├── __init__.py
  22. | | └── interfaces
  23. | | ├── __init__.py
  24. | | └── interfaces.py
  25. | ├── config
  26. | | ├── __init__.py
  27. | | └── interfaces
  28. | | ├── __init__.py
  29. | | └── interfaces.py
  30. | ├── facts
  31. | | ├── facts.py
  32. | | ├── __init__.py
  33. | | └── interfaces
  34. | | ├── __init__.py
  35. | | └── interfaces.py
  36. | ├── __init__.py
  37. | └── utils
  38. | ├── __init__.py
  39. | └── utils.py
  40. ├── README.md
  41. └── roles

Role directory layout

This example displays the role directory layout for the following:

  • network_os: myos
  • resource: interfaces
  1. ansible-playbook -e rm_dest=~/github/rm_example/roles/my_role \
  2. -e structure=role \
  3. -e model=models/myos/interfaces/myos_interfaces.yml \
  4. site.yml
  1. roles
  2. └── my_role
  3. ├── library
  4. ├── __init__.py
  5. ├── myos_facts.py
  6. └── myos_interfaces.py
  7. ├── LICENSE.txt
  8. ├── module_utils
  9. ├── __init__.py
  10. └── network
  11. ├── __init__.py
  12. └── myos
  13. ├── argspec
  14. ├── facts
  15. ├── facts.py
  16. └── __init__.py
  17. ├── __init__.py
  18. └── interfaces
  19. ├── __init__.py
  20. └── interfaces.py
  21. ├── config
  22. ├── __init__.py
  23. └── interfaces
  24. ├── __init__.py
  25. └── interfaces.py
  26. ├── facts
  27. ├── facts.py
  28. ├── __init__.py
  29. └── interfaces
  30. ├── __init__.py
  31. └── interfaces.py
  32. ├── __init__.py
  33. └── utils
  34. ├── __init__.py
  35. └── utils.py
  36. └── README.md

Using the collection

This example shows how to use the generated collection in a playbook:

  1. ——- hosts: myos101 gather_facts: False tasks: - cidrblock.my_collection.myos_interfaces: register: result - debug: var: result - cidrblock.my_collection.myos_facts: - debug: var: ansible_network_resources

Using the role

This example shows how to use the generated role in a playbook:

  1. - hosts: myos101
  2. gather_facts: False
  3. roles:
  4. - my_role
  5.  
  6. - hosts: myos101
  7. gather_facts: False
  8. tasks:
  9. - myos_interfaces:
  10. register: result
  11. - debug:
  12. var: result
  13. - myos_facts:
  14. - debug:
  15. var: ansible_network_resources

Resource module structure and workflow

The resource module structure includes the following components:

  • Module
    • library/<ansiblenetwork_os><resource>.py.
    • Imports the module_utils resource package and calls execute_module API
  1. def main():
  2. result = <resource_package>(module).execute_module()
  • Module argspec
    • module_utils/<ansible_network_os>/argspec/<resource>/.
    • Argspec for the resource.
  • Facts
    • module_utils/<ansible_network_os>/facts/<resource>/.
    • Populate facts for the resource.
    • Entry in module_utils/<ansible_network_os>/facts/facts.py for get_facts API to keep <ansible_network_os>_facts module and facts gathered for the resource module in sync for every subset.
    • Entry of Resource subset in FACTS_RESOURCE_SUBSETS list in module_utils/<ansible_network_os>/facts/facts.py to make facts collection work.
  • Module package in module_utils
    • module_utils/<ansible_network_os>/<config>/<resource>/.
    • Implement execute_module API that loads the configuration to device and generates the result with changed, commands, before and after keys.
    • Call get_facts API that returns the <resource> configuration facts or return the difference if the device has onbox diff support.
    • Compare facts gathered and given key-values if diff is not supported.
    • Generate final configuration.
  • Utils
    • module_utils/<ansible_network_os>/utils.
    • Utilities for the <ansible_network_os> platform.

Developer notes

The tests rely on a role generated by the resource module builder. After changes to the resource module builder, the role should be regenerated and the tests modified and run as needed. To generate the role after changes:

  1. rm -rf rmb_tests/roles/my_role
  2. ansible-playbook -e rm_dest=./rmb_tests/roles/my_role \
  3. -e structure=role \
  4. -e model=models/myos/interfaces/myos_interfaces.yml \
  5. site.yml