|
171 | 171 |
|
172 | 172 |
|
173 | 173 |
|
174 | | -def import_juniper_junos_common(): |
175 | | - """Imports the juniper_junos_common module from _module_utils_path. |
176 | | -
|
177 | | - Ansible versions < 2.4 do not provide a way to package common code in a |
178 | | - role. This function solves that problem for juniper_junos_* modules by |
179 | | - reading the module arguments passed on stdin and interpreting the special |
180 | | - option _module_utils_path as a path to the the directory where the |
181 | | - juniper_junos_common module resides. It temporarily inserts this path at |
182 | | - the head of sys.path, imports the juniper_junos_common module, and removes |
183 | | - the path from sys.path. It then returns the imported juniper_junos_common |
184 | | - module object. All juniper_junos_* modules must include this boilerplate |
185 | | - function in order to import the shared juniper_junos_common module. |
| 174 | +"""From Ansible 2.1, Ansible uses Ansiballz framework for assembling modules |
| 175 | +But custom module_utils directory is supported from Ansible 2.3 |
| 176 | +Reference for the issue: https://groups.google.com/forum/#!topic/ansible-project/J8FL7Z1J1Mw """ |
186 | 177 |
|
187 | | - Args: |
188 | | - None. |
189 | | -
|
190 | | - Returns: |
191 | | - The juniper_junos_common module object. |
192 | | -
|
193 | | - Raises: |
194 | | - ImportError: If the juniper_junos_common object can not be imported |
195 | | - from the path specified by the module_utils_path argument. |
196 | | - """ |
197 | | - from ansible.module_utils.basic import AnsibleModule |
198 | | - import sys |
199 | | - |
200 | | - juniper_junos_common = None |
201 | | - module = AnsibleModule( |
202 | | - argument_spec={ |
203 | | - '_module_utils_path': dict(type='path', default=None), |
204 | | - # Avoids a warning about not specifying no_log for passwd. |
205 | | - 'passwd': dict(no_log=True) |
206 | | - }, |
207 | | - # Doesn't really work due to Ansible bug. Keeping it here for when |
208 | | - # Ansible bug is fixed. |
209 | | - no_log=True, |
210 | | - check_invalid_arguments=False, |
211 | | - bypass_checks=True |
212 | | - ) |
213 | | - import_path = module.params.get('_module_utils_path') |
214 | | - if import_path is not None: |
215 | | - sys.path.insert(0, import_path) |
216 | | - import juniper_junos_common |
217 | | - del sys.path[0] |
218 | | - return juniper_junos_common |
| 178 | +# Ansiballz packages module_utils into ansible.module_utils |
| 179 | +from ansible.module_utils.basic import AnsibleModule |
| 180 | +from ansible.module_utils import juniper_junos_common |
219 | 181 |
|
220 | 182 |
|
221 | 183 | def get_facts_dict(junos_module): |
@@ -318,9 +280,6 @@ def save_inventory(junos_module, inventory): |
318 | 280 |
|
319 | 281 |
|
320 | 282 | def main(): |
321 | | - # Import juniper_junos_common |
322 | | - juniper_junos_common = import_juniper_junos_common() |
323 | | - |
324 | 283 | config_format_choices = [None] |
325 | 284 | config_format_choices += juniper_junos_common.CONFIG_FORMAT_CHOICES |
326 | 285 |
|
|
0 commit comments