Skip to content

Commit e41c68c

Browse files
author
Bradley A. Thornton
committed
added sample facts module, updated README.md
1 parent 7a8606a commit e41c68c

File tree

5 files changed

+104
-3
lines changed

5 files changed

+104
-3
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The resource module builder is an Ansible playbook that helps developers scaffol
1010
- Subsequent uses of RMB will only replace the module arspec and file containing the module doc string.
1111
- Complex examples can be stored along side the model in the same directory
1212
- Maintain the model as the source of truth for the module and use RMB to update the source files as needed.
13+
- Generates working sample modules for both `<network_os>_<resource>` and `<network_os>_facts`
1314

1415

1516
### Usage
@@ -71,6 +72,7 @@ ansible-playbook -e parent=~/github/rm_example \
7172
│   ├── inventory
7273
│   ├── modules
7374
│   │   ├── __init__.py
75+
│   │   ├── myos_facts.py
7476
│   │   └── myos_interfaces.py
7577
│   └── module_utils
7678
│   ├── __init__.py
@@ -125,6 +127,7 @@ ansible-playbook -e parent=~/github/rm_example/roles/my_role \
125127
└── my_role
126128
├── library
127129
│   ├── __init__.py
130+
│   ├── myos_facts.py
128131
│   └── myos_interfaces.py
129132
└── module_utils
130133
├── __init__.py
@@ -183,6 +186,13 @@ ln -s ~/github/rm_example ~/.ansible/collections/ansible_collections/cidrblock/m
183186
gather_facts: False
184187
tasks:
185188
- cidrblock.my_collection.myos_interfaces:
189+
register: result
190+
- debug:
191+
var: result
192+
- cidrblock.my_collection.myos_facts:
193+
- debug:
194+
var: net_configuration
195+
186196
```
187197

188198
**Using the role layout**
@@ -198,4 +208,10 @@ ln -s ~/github/rm_example ~/.ansible/collections/ansible_collections/cidrblock/m
198208
gather_facts: False
199209
tasks:
200210
- myos_interfaces:
211+
register: result
212+
- debug:
213+
var: result
214+
- myos_facts:
215+
- debug:
216+
var: net_configuration
201217
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# {{ rm['metadata']['copyright_str'] }}
4+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
"""
6+
The module file for {{ network_os }}_facts
7+
"""
8+
9+
from __future__ import absolute_import, division, print_function
10+
from ansible.module_utils.basic import AnsibleModule
11+
from ansible.module_utils.connection import Connection
12+
from {{ import_path }}. \
13+
{{ network_os }}.facts.facts import Facts
14+
15+
ANSIBLE_METADATA = {'metadata_version': '1.1',
16+
'status': {{ rm['metadata']['status'] }},
17+
'supported_by': {{ rm['metadata']['supported_by'] }}}
18+
19+
20+
DOCUMENTATION = """
21+
---
22+
module: {{ network_os }}_facts
23+
version_added: {{ rm['info']['version_added'] }}
24+
short_description: {{ rm['info']['short_description'] }}
25+
description:
26+
- Collects facts from network devices running the {{ network_os }} operating
27+
system. This module places the facts gathered in the fact tree keyed by the
28+
respective resource name. The facts module will always collect a
29+
base set of facts from the device and can enable or disable
30+
collection of additional facts.
31+
author: {{ rm['info']['authors'] }}
32+
options:
33+
gather_subset:
34+
description:
35+
- When supplied, this argument will restrict the facts collected
36+
to a given subset. Possible values for this argument include
37+
all, and net_configuration_<resource_name>. Can specify a
38+
list of values to include a larger subset. Values can also be used
39+
with an initial C(M(!)) to specify that a specific subset should
40+
not be collected.
41+
required: false
42+
default: 'all'
43+
version_added: "2.2"
44+
"""
45+
46+
EXAMPLES = """
47+
# Gather all facts
48+
- {{ network_os }}_facts:
49+
gather_subset: all
50+
51+
# Collect only the {{ resource }} and default facts
52+
- {{ network_os }}_facts:
53+
gather_subset:
54+
- net_configuration_{{ resource }}
55+
56+
# Do not collect {{ resource }} facts
57+
- {{ network_os }}_facts:
58+
gather_subset:
59+
- "!net_configuration_{{ resource }}"
60+
"""
61+
62+
RETURN = """
63+
See the respective resource module parameters for the tree.
64+
"""
65+
66+
def main():
67+
"""
68+
Main entry point for module execution
69+
70+
:returns: ansible_facts
71+
"""
72+
module = AnsibleModule(argument_spec=Facts.argument_spec,
73+
supports_check_mode=True)
74+
warnings = list()
75+
76+
connection = Connection(module._socket_path) #pylint: disable=W0212
77+
gather_subset = module.params['gather_subset']
78+
ansible_facts = Facts().get_facts(module, connection, gather_subset)
79+
module.exit_json(ansible_facts=ansible_facts, warnings=warnings)
80+
81+
if __name__ == '__main__':
82+
main()

roles/resource_module/templates/module_directory/network_os/network_os_resource.py.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ from {{ import_path }}. \
3333
{{ network_os }}.config.{{ resource }}.{{ resource }} import {{ resource|capitalize }}
3434

3535
ANSIBLE_METADATA = {'metadata_version': '1.1',
36-
'status': ['preview'],
37-
'supported_by': 'network'}
36+
'status': {{ rm['metadata']['status'] }},
37+
'supported_by': {{ rm['metadata']['supported_by'] }}}
3838

3939

4040
DOCUMENTATION = """

roles/resource_module/templates/module_utils/network_os/facts/resource/resource.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class {{ resource|capitalize }}Facts(FactsBase):
3131
pass
3232

3333
if not data:
34-
data = "" # connection.get('show running-config | section ^interface')
34+
data = "foo" # connection.get('show running-config | section ^interface')
3535

3636
# operate on a collection of resource x
3737
config = [data] # data.split('interface ')

roles/resource_module/vars/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ resource_module_templates:
4848
- source: module_directory/network_os/network_os_resource.py.j2
4949
destination: "{{ parent_directory }}/{{ module_directory }}/{{ network_os }}_{{ resource }}.py"
5050
overwrite: True
51+
- source: module_directory/network_os/network_os_facts.py.j2
52+
destination: "{{ parent_directory }}/{{ module_directory }}/{{ network_os }}_facts.py"
53+
overwrite: False
5154
- source: module_utils/network_os/argspec/facts/facts.py.j2
5255
destination: "{{ parent_directory}}/module_utils/{{ network_os }}/argspec/facts/facts.py"
5356
overwrite: False

0 commit comments

Comments
 (0)