3636DOCUMENTATION = '''
3737---
3838module: junos_jsnapy
39- author: Roslan Zaki, Juniper Networks
40- version_added: "1.0 .0"
39+ author: Roslan Zaki & Damien Garros , Juniper Networks
40+ version_added: "1.4 .0"
4141version_control:
4242 13-Apr-2016 v1.0.0 - Basic working model
4343
44- Short_description: Integrate JSnapy to ansible
44+ Short_description: Integrate JSnapy to ansible.
4545description:
46- - Execute JSnapy module from Ansible
46+ - Execute JSnapy test from Ansible.
47+ Attention, to not break Ansible behavior, this module only report "failed"
48+ if the module itself fails, not if a test fails.
49+ To check the test results you need to subscribe to the result and assert
50+ the returned value.
51+ An experimental Callback_Plugin for junos_jsnapy is available to provide
52+ additional information about tests that failed.
53+ To enable it, you need to add "callback_whitelist = jsnapy" in your ansible
54+ configuration file.
4755requirements:
4856 - junos-eznc >= 1.2.2
4957options:
@@ -114,20 +122,41 @@ options:
114122EXAMPLES = '''
115123 - name: JUNOS Post Checklist
116124 junos_jsnapy:
117- host: "{{ inventory_hostname}}"
118- passwd: "{{ tm1_password }}"
119- action: "snap_post"
120- config_file: "first_test.yml"
121- logfile: "migration_post.log"
122- register: jsnapy
125+ host: "{{ inventory_hostname}}"
126+ passwd: "{{ tm1_password }}"
127+ action: "snap_post"
128+ config_file: "first_test.yml"
129+ logfile: "migration_post.log"
130+ register: test1
131+
132+ - name: Check JSNAPy tests results
133+ assert:
134+ that:
135+ - "test1.passPercentage == 100"
123136
124137 - name: Debug jsnapy
125- debug: msg="{{jsnapy}}"
138+ debug: msg=test1
139+
140+ ---------
141+ - name: Test based on a test_file directly
142+ junos_jsnapy:
143+ host: "{{ junos_host }}"
144+ port: "{{ netconf_port }}"
145+ user: "{{ ansible_ssh_user }}"
146+ passwd: "{{ ansible_ssh_pass }}"
147+ test_files: tests/test_junos_interface.yaml
148+ action: snapcheck
149+ register: test1
126150
151+ - name: Check JSNAPy tests results
152+ assert:
153+ that:
154+ - "test1.passPercentage == 100"
127155'''
128156from distutils .version import LooseVersion
129157import logging
130158from lxml .builder import E
159+ import os .path
131160import os
132161import time
133162
@@ -153,6 +182,13 @@ def jsnap_selection(dev, module):
153182 config_data = os .path .join (config_dir , config_file )
154183 else :
155184 test_files = args .get ('test_files' )
185+
186+ for test_file in test_files :
187+ if not os .path .isfile (test_file ):
188+ msg = 'unable to find the test file {0}' .format (test_file )
189+ logging .error (msg )
190+ module .fail_json (msg = msg )
191+
156192 config_data = {'tests' : test_files }
157193
158194 results = {}
@@ -167,6 +203,7 @@ def jsnap_selection(dev, module):
167203 elif action == 'check' :
168204 snapValue = js .check (data = config_data , dev = dev , pre_file = 'PRE' , post_file = 'POST' )
169205
206+ percentagePassed = 0
170207 if isinstance (snapValue , (list )):
171208 for snapCheck in snapValue :
172209 router = snapCheck .device
@@ -177,7 +214,10 @@ def jsnap_selection(dev, module):
177214 results ['test_results' ] = snapCheck .test_results
178215 total_test = int (snapCheck .no_passed ) + int (snapCheck .no_failed )
179216 results ['total_tests' ] = total_test
180- percentagePassed = (int (results ['total_passed' ]) * 100 ) / (results ['total_tests' ])
217+
218+ if total_test != 0 :
219+ percentagePassed = (int (results ['total_passed' ]) * 100 ) / (results ['total_tests' ])
220+
181221 results ['passPercentage' ] = percentagePassed
182222
183223 return results
@@ -203,7 +243,7 @@ def main():
203243 args = module .params
204244 results = {}
205245
206- if import_err_message is not None :
246+ if import_err_message is not None :
207247 module .fail_json (msg = import_err_message )
208248
209249 if args ['mode' ] is not None and LooseVersion (VERSION ) < LooseVersion ('2.0.0' ):
@@ -236,7 +276,7 @@ def main():
236276 )
237277 logging .error (msg )
238278 dev .close ()
239- module .fail_json (msg = msg , ** data )
279+ module .exit_json (msg = msg , ** data )
240280
241281 except Exception as err :
242282 msg = 'Uncaught exception - please report: {0}' .format (str (err ))
0 commit comments