Skip to content

Commit 2f2cea0

Browse files
authored
Merge pull request #357 from rsmekala/module_utils_ansiballz_integration
Module utils ansiballz integration
2 parents 664aa73 + 63cc76e commit 2f2cea0

13 files changed

+71
-524
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ dist: trusty
99
env:
1010
- ANSIBLE_VERSION=2.4.0.0
1111
- ANSIBLE_VERSION=2.3.0.0
12-
- ANSIBLE_VERSION=2.2.0.0
13-
- ANSIBLE_VERSION=2.1.3.0
1412

1513
install:
1614
## Create Docker with Ansible modules and all dependancies

library/juniper_junos_command.py

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -303,57 +303,16 @@
303303
import sys
304304

305305

306-
def import_juniper_junos_common():
307-
"""Imports the juniper_junos_common module from _module_utils_path.
306+
"""From Ansible 2.1, Ansible uses Ansiballz framework for assembling modules
307+
But custom module_utils directory is supported from Ansible 2.3
308+
Reference for the issue: https://groups.google.com/forum/#!topic/ansible-project/J8FL7Z1J1Mw """
308309

309-
Ansible versions < 2.4 do not provide a way to package common code in a
310-
role. This function solves that problem for juniper_junos_* modules by
311-
reading the module arguments passed on stdin and interpreting the special
312-
option _module_utils_path as a path to the the directory where the
313-
juniper_junos_common module resides. It temporarily inserts this path at
314-
the head of sys.path, imports the juniper_junos_common module, and removes
315-
the path from sys.path. It then returns the imported juniper_junos_common
316-
module object. All juniper_junos_* modules must include this boilerplate
317-
function in order to import the shared juniper_junos_common module.
318-
319-
Args:
320-
None.
321-
322-
Returns:
323-
The juniper_junos_common module object.
324-
325-
Raises:
326-
ImportError: If the juniper_junos_common object can not be imported
327-
from the path specified by the module_utils_path argument.
328-
"""
329-
from ansible.module_utils.basic import AnsibleModule
330-
import sys
331-
332-
juniper_junos_common = None
333-
module = AnsibleModule(
334-
argument_spec={
335-
'_module_utils_path': dict(type='path', default=None),
336-
# Avoids a warning about not specifying no_log for passwd.
337-
'passwd': dict(no_log=True)
338-
},
339-
# Doesn't really work due to Ansible bug. Keeping it here for when
340-
# Ansible bug is fixed.
341-
no_log=True,
342-
check_invalid_arguments=False,
343-
bypass_checks=True
344-
)
345-
import_path = module.params.get('_module_utils_path')
346-
if import_path is not None:
347-
sys.path.insert(0, import_path)
348-
import juniper_junos_common
349-
del sys.path[0]
350-
return juniper_junos_common
310+
# Ansiballz packages module_utils into ansible.module_utils
311+
from ansible.module_utils.basic import AnsibleModule
312+
from ansible.module_utils import juniper_junos_common
351313

352314

353315
def main():
354-
# Import juniper_junos_common
355-
juniper_junos_common = import_juniper_junos_common()
356-
357316
# Create the module instance.
358317
junos_module = juniper_junos_common.JuniperJunosModule(
359318
argument_spec=dict(

library/juniper_junos_config.py

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -732,57 +732,16 @@
732732
import time
733733

734734

735-
def import_juniper_junos_common():
736-
"""Imports the juniper_junos_common module from _module_utils_path.
735+
"""From Ansible 2.1, Ansible uses Ansiballz framework for assembling modules
736+
But custom module_utils directory is supported from Ansible 2.3
737+
Reference for the issue: https://groups.google.com/forum/#!topic/ansible-project/J8FL7Z1J1Mw """
737738

738-
Ansible versions < 2.4 do not provide a way to package common code in a
739-
role. This function solves that problem for juniper_junos_* modules by
740-
reading the module arguments passed on stdin and interpreting the special
741-
option _module_utils_path as a path to the the directory where the
742-
juniper_junos_common module resides. It temporarily inserts this path at
743-
the head of sys.path, imports the juniper_junos_common module, and removes
744-
the path from sys.path. It then returns the imported juniper_junos_common
745-
module object. All juniper_junos_* modules must include this boilerplate
746-
function in order to import the shared juniper_junos_common module.
747-
748-
Args:
749-
None.
750-
751-
Returns:
752-
The juniper_junos_common module object.
753-
754-
Raises:
755-
ImportError: If the juniper_junos_common object can not be imported
756-
from the path specified by the module_utils_path argument.
757-
"""
758-
from ansible.module_utils.basic import AnsibleModule
759-
import sys
760-
761-
juniper_junos_common = None
762-
module = AnsibleModule(
763-
argument_spec={
764-
'_module_utils_path': dict(type='path', default=None),
765-
# Avoids a warning about not specifying no_log for passwd.
766-
'passwd': dict(no_log=True)
767-
},
768-
# Doesn't really work due to Ansible bug. Keeping it here for when
769-
# Ansible bug is fixed.
770-
no_log=True,
771-
check_invalid_arguments=False,
772-
bypass_checks=True
773-
)
774-
import_path = module.params.get('_module_utils_path')
775-
if import_path is not None:
776-
sys.path.insert(0, import_path)
777-
import juniper_junos_common
778-
del sys.path[0]
779-
return juniper_junos_common
739+
# Ansiballz packages module_utils into ansible.module_utils
740+
from ansible.module_utils.basic import AnsibleModule
741+
from ansible.module_utils import juniper_junos_common
780742

781743

782744
def main():
783-
# Import juniper_junos_common
784-
juniper_junos_common = import_juniper_junos_common()
785-
786745
# Choices which are defined in the common module.
787746
config_format_choices = juniper_junos_common.CONFIG_FORMAT_CHOICES
788747
config_database_choices = [None] + \

library/juniper_junos_facts.py

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -171,51 +171,13 @@
171171

172172

173173

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 """
186177

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
219181

220182

221183
def get_facts_dict(junos_module):
@@ -318,9 +280,6 @@ def save_inventory(junos_module, inventory):
318280

319281

320282
def main():
321-
# Import juniper_junos_common
322-
juniper_junos_common = import_juniper_junos_common()
323-
324283
config_format_choices = [None]
325284
config_format_choices += juniper_junos_common.CONFIG_FORMAT_CHOICES
326285

library/juniper_junos_jsnapy.py

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -203,59 +203,18 @@
203203
import os.path
204204

205205

206-
def import_juniper_junos_common():
207-
"""Imports the juniper_junos_common module from _module_utils_path.
206+
"""From Ansible 2.1, Ansible uses Ansiballz framework for assembling modules
207+
But custom module_utils directory is supported from Ansible 2.3
208+
Reference for the issue: https://groups.google.com/forum/#!topic/ansible-project/J8FL7Z1J1Mw """
208209

209-
Ansible versions < 2.4 do not provide a way to package common code in a
210-
role. This function solves that problem for juniper_junos_* modules by
211-
reading the module arguments passed on stdin and interpreting the special
212-
option _module_utils_path as a path to the the directory where the
213-
juniper_junos_common module resides. It temporarily inserts this path at
214-
the head of sys.path, imports the juniper_junos_common module, and removes
215-
the path from sys.path. It then returns the imported juniper_junos_common
216-
module object. All juniper_junos_* modules must include this boilerplate
217-
function in order to import the shared juniper_junos_common module.
218-
219-
Args:
220-
None.
221-
222-
Returns:
223-
The juniper_junos_common module object.
224-
225-
Raises:
226-
ImportError: If the juniper_junos_common object can not be imported
227-
from the path specified by the module_utils_path argument.
228-
"""
229-
from ansible.module_utils.basic import AnsibleModule
230-
import sys
231-
232-
juniper_junos_common = None
233-
module = AnsibleModule(
234-
argument_spec={
235-
'_module_utils_path': dict(type='path', default=None),
236-
# Avoids a warning about not specifying no_log for passwd.
237-
'passwd': dict(no_log=True)
238-
},
239-
# Doesn't really work due to Ansible bug. Keeping it here for when
240-
# Ansible bug is fixed.
241-
no_log=True,
242-
check_invalid_arguments=False,
243-
bypass_checks=True
244-
)
245-
import_path = module.params.get('_module_utils_path')
246-
if import_path is not None:
247-
sys.path.insert(0, import_path)
248-
import juniper_junos_common
249-
del sys.path[0]
250-
return juniper_junos_common
210+
# Ansiballz packages module_utils into ansible.module_utils
211+
from ansible.module_utils.basic import AnsibleModule
212+
from ansible.module_utils import juniper_junos_common
251213

252214

253215
def main():
254216
JSNAPY_ACTION_CHOICES = ['check', 'snapcheck', 'snap_pre', 'snap_post']
255217

256-
# Import juniper_junos_common
257-
juniper_junos_common = import_juniper_junos_common()
258-
259218
# Create the module instance.
260219
junos_module = juniper_junos_common.JuniperJunosModule(
261220
argument_spec=dict(

library/juniper_junos_ping.py

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -380,57 +380,16 @@
380380
'''
381381

382382

383-
def import_juniper_junos_common():
384-
"""Imports the juniper_junos_common module from _module_utils_path.
385-
386-
Ansible versions < 2.4 do not provide a way to package common code in a
387-
role. This function solves that problem for juniper_junos_* modules by
388-
reading the module arguments passed on stdin and interpreting the special
389-
option _module_utils_path as a path to the the directory where the
390-
juniper_junos_common module resides. It temporarily inserts this path at
391-
the head of sys.path, imports the juniper_junos_common module, and removes
392-
the path from sys.path. It then returns the imported juniper_junos_common
393-
module object. All juniper_junos_* modules must include this boilerplate
394-
function in order to import the shared juniper_junos_common module.
395-
396-
Args:
397-
None.
398-
399-
Returns:
400-
The juniper_junos_common module object.
401-
402-
Raises:
403-
ImportError: If the juniper_junos_common object can not be imported
404-
from the path specified by the module_utils_path argument.
405-
"""
406-
from ansible.module_utils.basic import AnsibleModule
407-
import sys
408-
409-
juniper_junos_common = None
410-
module = AnsibleModule(
411-
argument_spec={
412-
'_module_utils_path': dict(type='path', default=None),
413-
# Avoids a warning about not specifying no_log for passwd.
414-
'passwd': dict(no_log=True)
415-
},
416-
# Doesn't really work due to Ansible bug. Keeping it here for when
417-
# Ansible bug is fixed.
418-
no_log=True,
419-
check_invalid_arguments=False,
420-
bypass_checks=True
421-
)
422-
import_path = module.params.get('_module_utils_path')
423-
if import_path is not None:
424-
sys.path.insert(0, import_path)
425-
import juniper_junos_common
426-
del sys.path[0]
427-
return juniper_junos_common
383+
"""From Ansible 2.1, Ansible uses Ansiballz framework for assembling modules
384+
But custom module_utils directory is supported from Ansible 2.3
385+
Reference for the issue: https://groups.google.com/forum/#!topic/ansible-project/J8FL7Z1J1Mw """
428386

387+
# Ansiballz packages module_utils into ansible.module_utils
388+
from ansible.module_utils.basic import AnsibleModule
389+
from ansible.module_utils import juniper_junos_common
429390

430-
def main():
431-
# Import juniper_junos_common
432-
juniper_junos_common = import_juniper_junos_common()
433391

392+
def main():
434393
# The argument spec for the module.
435394
argument_spec = dict(
436395
dest=dict(type='str',

0 commit comments

Comments
 (0)