Skip to content

Commit 338356d

Browse files
committed
Merge pull request #52 from shermdog/13
Add logging for netconify 1.0
2 parents 589d694 + 6d7421a commit 338356d

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

library/junos_get_facts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ description:
4646
The information is similar to facts gathered by other IT frameworks.
4747
requirements:
4848
- junos-eznc >= 1.1.0
49-
- junos-netconify >= 0.1.1, when using the I(console) option
49+
- junos-netconify >= 1.0.0, when using the I(console) option
5050
options:
5151
host:
5252
description:
@@ -115,6 +115,7 @@ import os
115115

116116

117117
def main():
118+
import re
118119
module = AnsibleModule(
119120
argument_spec=dict(
120121
host=dict(required=True),
@@ -133,7 +134,6 @@ def main():
133134
try:
134135
from jnpr.junos import Device
135136
from jnpr.junos.version import VERSION
136-
import re
137137
if not float(re.match('\d+.\d+', VERSION).group()) >= 1.1:
138138
module.fail_json(msg='junos-eznc >= 1.1.x is required for this module')
139139
except ImportError:
@@ -159,8 +159,11 @@ def main():
159159
# -----------
160160
try:
161161
from netconify.cmdo import netconifyCmdo
162+
from netconify.constants import version
163+
if not float(re.match('\d+.\d+', version).group()) >= 1.0:
164+
module.fail_json(msg='junos-netconify >= 1.0.x is required for this module')
162165
except ImportError:
163-
module.fail_json(msg='junos-netconify is required for this module')
166+
module.fail_json(msg='junos-netconify >= 1.0.x is required for this module')
164167
import logging
165168

166169
c_args = []
@@ -180,16 +183,19 @@ def main():
180183
format='%(asctime)s:%(name)s:%(message)s')
181184
logging.getLogger().name = 'NETCONIFY:' + module.params['host']
182185

183-
def log_notify(event, message):
186+
def log_notify(self, event, message):
184187
logging.info("%s:%s" % (event, message))
185188
use_notifier = log_notify
186189
else:
187-
def silent_notify(event, message):
190+
def silent_notify(self, event, message):
188191
pass
189192
use_notifier = silent_notify
190193

191-
nc = netconifyCmdo(notify=use_notifier)
192-
c_results = nc.run(c_args)
194+
try:
195+
nc = netconifyCmdo(notify=use_notifier)
196+
c_results = nc.run(c_args)
197+
except Exception as err:
198+
module.fail_json(msg=str(err))
193199
m_results['args'] = m_args # for debug
194200
m_results['facts'] = c_results['facts']
195201

library/junos_install_config

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ description:
5252
Configurations performed through the console must only use ASCII text formatting.
5353
requirements:
5454
- junos-eznc >= 1.1.0
55-
- junos-netconify >= 0.1.1, when using the I(console) option
55+
- junos-netconify >= 1.0.0, when using the I(console) option
5656
options:
5757
host:
5858
description:
@@ -152,14 +152,14 @@ EXAMPLES = '''
152152

153153
import logging
154154
from os.path import isfile
155-
156155
import os
156+
import re
157+
157158
try:
158159
from jnpr.junos import Device
159160
from jnpr.junos.exception import *
160161
from jnpr.junos.utils.config import Config
161162
from jnpr.junos.version import VERSION
162-
import re
163163
if not float(re.match('\d+.\d+', VERSION).group()) >= 1.1:
164164
HAS_PYEZ = False
165165
else:
@@ -301,8 +301,11 @@ def _load_via_netconf(module):
301301
def _load_via_console(module):
302302
try:
303303
from netconify.cmdo import netconifyCmdo
304+
from netconify.constants import version
305+
if not float(re.match('\d+.\d+', version).group()) >= 1.0:
306+
module.fail_json(msg='junos-netconify >= 1.0.x is required for this module')
304307
except ImportError:
305-
module.fail_json(msg='junos-netconify is required for this module')
308+
module.fail_json(msg='junos-netconify >= 1.0.x is required for this module')
306309

307310
m_args = module.params
308311

@@ -331,16 +334,19 @@ def _load_via_console(module):
331334
format='%(asctime)s:%(name)s:%(message)s')
332335
logging.getLogger().name = 'NETCONIFY:' + m_args['host']
333336

334-
def log_notify(event, message):
337+
def log_notify(self, event, message):
335338
logging.info("%s:%s" % (event, message))
336339
use_notifier = log_notify
337340
else:
338-
def silent_notify(event, message):
341+
def silent_notify(self, event, message):
339342
pass
340343
use_notifier = silent_notify
341344

342-
nc = netconifyCmdo(notify=use_notifier)
343-
c_results = nc.run(c_args)
345+
try:
346+
nc = netconifyCmdo(notify=use_notifier)
347+
c_results = nc.run(c_args)
348+
except Exception as err:
349+
module.fail_json(msg=str(err))
344350
m_results = dict(changed=c_results['changed'])
345351
if c_results['failed'] is True:
346352
module.fail_json(msg=c_results['errmsg'])

library/junos_install_os

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ import os
130130
try:
131131
from jnpr.junos import Device
132132
from jnpr.junos.version import VERSION
133-
import re
134133
if not float(re.match('\d+.\d+', VERSION).group()) >= 1.1:
135134
HAS_PYEZ = False
136135
else:

library/junos_zeroize

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ description:
5353
as root in order to access the device.
5454
requirements:
5555
- junos-eznc >= 1.1.0
56-
- junos-netconify >= 0.1.1, when using the I(console) option
56+
- junos-netconify >= 1.0.0, when using the I(console) option
5757
options:
5858
host:
5959
description:
@@ -106,6 +106,7 @@ EXAMPLES = '''
106106

107107
import os
108108
import logging
109+
import re
109110

110111

111112
def main():
@@ -135,14 +136,14 @@ def main():
135136
logfile = module.params['logfile']
136137
if logfile is not None:
137138
logging.basicConfig(filename=logfile, level=logging.INFO,
138-
format='%(asctime)s:%(name)s:%(message)s')
139+
format='%(asctime)s:%(name)s:%(message)s')
139140
logging.getLogger().name = 'NETCONIFY:' + module.params['host']
140141

141-
def log_notify(event, message):
142+
def log_notify(self, event, message):
142143
logging.info("%s:%s" % (event, message))
143144
use_notifier = log_notify
144145
else:
145-
def silent_notify(event, message):
146+
def silent_notify(self, event, message):
146147
pass
147148
use_notifier = silent_notify
148149

@@ -151,31 +152,33 @@ def main():
151152
try:
152153
from jnpr.junos import Device
153154
from jnpr.junos.version import VERSION
154-
import re
155155
if not float(re.match('\d+.\d+', VERSION).group()) >= 1.1:
156156
module.fail_json(msg='junos-eznc >= 1.1.x is required for this module')
157157
except ImportError:
158158
module.fail_json(msg='junos-eznc >= 1.1.x is required for this module')
159159

160160
dev = Device(args['host'], user=args['user'], password=args['passwd'], port=args['port'])
161161
try:
162-
use_notifier('LOGIN', 'host={0}'.format(args['host']))
162+
use_notifier(None, 'LOGIN', 'host={0}'.format(args['host']))
163163
dev.open()
164-
use_notifier('LOGIN', 'OK')
164+
use_notifier(None, 'LOGIN', 'OK')
165165
except Exception as err:
166166
msg = 'unable to connect to {0}: {1}'.format(args['host'], str(err))
167167
module.fail_json(msg=msg)
168168
# --- UNREACHABLE ---
169169

170-
use_notifier('ZEROIZE', 'invoking command')
170+
use_notifier(None, 'ZEROIZE', 'invoking command')
171171
dev.cli('request system zeroize')
172172
results['changed'] = True
173173
# no close, we're done after this point.
174174
else:
175175
try:
176176
from netconify.cmdo import netconifyCmdo
177+
from netconify.constants import version
178+
if not float(re.match('\d+.\d+', version).group()) >= 1.0:
179+
module.fail_json(msg='junos-netconify >= 1.0.x is required for this module')
177180
except ImportError:
178-
module.fail_json(msg='junos-netconify is required for this module')
181+
module.fail_json(msg='junos-netconify >= 1.0.x is required for this module')
179182
nc_args = []
180183
nc_args.append(args['console'])
181184
nc_args.append('--zeroize')
@@ -184,12 +187,15 @@ def main():
184187
if args['passwd'] is not None:
185188
nc_args.append('--passwd=' + args['passwd'])
186189

187-
nc = netconifyCmdo(notify=use_notifier)
188-
nc.run(nc_args)
190+
try:
191+
nc = netconifyCmdo(notify=use_notifier)
192+
nc.run(nc_args)
193+
except Exception as err:
194+
module.fail_json(msg=str(err))
189195
results['changed'] = True
190196

191197
# indicate done in the logfile and return results
192-
use_notifier('DONE', 'OK')
198+
use_notifier(None, 'DONE', 'OK')
193199
module.exit_json(**results)
194200

195201
from ansible.module_utils.basic import *

0 commit comments

Comments
 (0)