@@ -8,6 +8,7 @@ this file validates each subset of facts and selectively
8
8
calls the appropriate facts gathering function
9
9
"""
10
10
11
+ from ansible .module_utils .six import string_types , iteritems
11
12
from {{ import_path }}. \
12
13
{{ network_os }}.argspec .facts .facts import FactsArgs
13
14
from {{ import_path }}. \
@@ -17,36 +18,34 @@ from {{ import_path }}. \
17
18
from {{ import_path }}. \
18
19
{{ network_os }}.facts .{{ resource }}.{{ resource }} import {{ resource | capitalize }}Facts
19
20
21
+
22
+ FACT_SUBSETS = {}
23
+
20
24
class Facts (FactsArgs , FactsBase ): #pylint: disable=R0903
21
25
""" The fact class for {{ network_os }}
22
26
"""
23
27
24
- def get_facts (self , module , connection , gather_subset = ['all' ]):
25
- """ Collect the facts for {{ network_os }}
26
-
27
- :param module: The module instance
28
- :param connection: The device connection
29
- :param gather_subset: The facts subset to collect
30
- :rtype: dict
31
- :returns: the facts gathered
32
- """
33
- valid_subsets = self .argument_spec ['gather_subset' ].get ('choices' , [])
34
- if valid_subsets and 'all' in valid_subsets :
35
- valid_subsets .remove ('all' )
28
+ VALID_GATHER_SUBSETS = frozenset (FACT_SUBSETS .keys ())
36
29
30
+ def generate_runable_subsets (self , module , subsets , valid_subsets ):
37
31
runable_subsets = set ()
38
32
exclude_subsets = set ()
39
- if not gather_subset :
40
- gather_subset = ['all' ]
33
+ minimal_gather_subset = frozenset (['default' ])
41
34
42
- for subset in gather_subset :
35
+ for subset in subsets :
43
36
if subset == 'all' :
44
37
runable_subsets .update (valid_subsets )
45
38
continue
39
+ if subset == 'min' and minimal_gather_subset :
40
+ runable_subsets .update (minimal_gather_subset )
41
+ continue
46
42
if subset .startswith ('!' ):
47
43
subset = subset [1 :]
44
+ if subset == 'min' :
45
+ exclude_subsets .update (minimal_gather_subset )
46
+ continue
48
47
if subset == 'all' :
49
- exclude_subsets .update (valid_subsets )
48
+ exclude_subsets .update (valid_subsets - minimal_gather_subset )
50
49
continue
51
50
exclude = True
52
51
else :
@@ -62,16 +61,61 @@ class Facts(FactsArgs, FactsBase): #pylint: disable=R0903
62
61
63
62
if not runable_subsets :
64
63
runable_subsets .update (valid_subsets )
65
-
66
64
runable_subsets .difference_update (exclude_subsets )
67
- self .ansible_facts ['gather_subset' ] = list (runable_subsets )
68
65
69
- for attr in runable_subsets :
70
- getattr (self , '_get_%s' % attr , {})(module , connection )
66
+ return runable_subsets
67
+
68
+ def get_facts (self , module , connection , gather_subset = ['!config' ], gather_network_resources = ['all' ]):
69
+ """ Collect the facts for {{ network_os }}
70
+
71
+ :param module: The module instance
72
+ :param connection: The device connection
73
+ :param gather_subset: The facts subset to collect
74
+ :param gather_network_resources: The resource subset to collect
75
+ :rtype: dict
76
+ :returns: the facts gathered
77
+ """
78
+ self .ansible_facts ['gather_network_resources' ] = list ()
79
+ self .ansible_facts ['gather_subset' ] = list ()
80
+
81
+ valid_network_resources_subsets = self .argument_spec ['gather_network_resources' ].get ('choices' , [])
82
+ if valid_network_resources_subsets and 'all' in valid_network_resources_subsets :
83
+ valid_network_resources_subsets .remove ('all' )
84
+
85
+ if valid_network_resources_subsets :
86
+ resources_runable_subsets = self .generate_runable_subsets (module , gather_network_resources , valid_network_resources_subsets )
87
+ if resources_runable_subsets :
88
+ self .ansible_facts ['gather_network_resources' ] = list (resources_runable_subsets )
89
+ for attr in resources_runable_subsets :
90
+ getattr (self , '_get_%s' % attr , {})(module , connection )
91
+
92
+ if self .VALID_GATHER_SUBSETS :
93
+ runable_subsets = self .generate_runable_subsets (module , gather_subset , self .VALID_GATHER_SUBSETS )
94
+
95
+ if runable_subsets :
96
+ facts = dict ()
97
+ self .ansible_facts ['gather_subset' ] = list (runable_subsets )
98
+
99
+ instances = list ()
100
+ for key in runable_subsets :
101
+ instances .append (FACT_SUBSETS [key ](module ))
102
+
103
+ for inst in instances :
104
+ inst .populate ()
105
+ facts .update (inst .facts )
106
+ warnings .extend (inst .warnings )
107
+
108
+ for key , value in iteritems (facts ):
109
+ key = 'ansible_net_%s' % key
110
+ self .ansible_facts [key ] = value
111
+
112
+ if warnings :
113
+ return self .ansible_facts , warnings
114
+ else :
115
+ return self .ansible_facts
71
116
72
- return self .ansible_facts
73
117
74
118
@staticmethod
75
- def _get_net_configuration_ {{ resource }}(module , connection ):
119
+ def _get_ {{ resource }}(module , connection ):
76
120
return {{ resource | capitalize }}Facts ({{ resource | capitalize }}Args . \
77
121
argument_spec , 'config' , 'options' ).populate_facts (module , connection )
0 commit comments