forked from ktbyers/ansible-junos-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjunos_zeroize
161 lines (146 loc) · 5.54 KB
/
junos_zeroize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python2.7
# Copyright (c) 1999-2014, Juniper Networks Inc.
# 2014, Jeremy Schulman
#
# All rights reserved.
#
# License: Apache 2.0
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of the Juniper Networks nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
DOCUMENTATION = '''
---
module: junos_zeroize
author: Jeremy Schulman, Juniper Networks
version_added: "1.6.0"
short_description: Zeroize Junos system
description:
- Remove all configuration information on the Routing Engines and
reset all key values. The command removes all data files, including
customized configuration and log files, by unlinking the files from
their directories.
requirements:
- py-junos-eznc >= 0.0.5
- netconify, if using I(console) option
options:
host:
description:
- should be {{ inventory_hostname }}
required: false
user:
description:
- login user-name
required: false
default: $USER
passwd:
description:
- login password
required: false
default: assumes ssh-key active
console:
description:
- SERIAL or TERMINAL-SERVER port setting, per use with
the B(netconify) utility
required: false
default: None
notes:
- ex: "--telnet=termserv101,2000"
zeroize:
description:
- safety mechanism, you B(MUST) set this to 'zeroize'
required: yes
default: None
logfile:
description:
- path on the local server where progress status is logged
for debugging purposes
required: false
default: None
notes:
- You B(MUST) either use the I(host) option or the I(port) option to designate
how the device is accessed.
'''
EXAMPLES = '''
- junos_zeroize:
host={{ inventory_hostname }}
zeroize="zeroize"
'''
import sys, os
import logging
def main():
module = AnsibleModule(
argument_spec = dict(
zeroize=dict(required=True, default=None), # must be set to 'zeroize'
host=dict(required=False, default=None), # host or ipaddr
console=dict(required=False, default=None), # param to netconify
user=dict(required=False, default=os.getenv('USER')),
passwd=dict(required=False, default=None),
logfile=dict(required=False, default=None)),
supports_check_mode = False )
args = module.params
if args['zeroize'] != 'zeroize':
results = dict(changed=False, failed=True)
results['msg'] = "You must set 'zeroize=zeroize' ({})".format(args['zeroize'])
module.exit_json(**results)
#
# ! UNREACHABLE
results = {}
logfile = module.params['logfile']
if logfile is not None:
logging.basicConfig(filename=logfile, level=logging.INFO,
format='%(asctime)s:%(name)s:%(message)s')
logging.getLogger().name = 'NETCONIFY:'+module.params['host']
def log_notify(event,message):
logging.info("%s:%s"%(event,message))
use_notifier = log_notify
else:
def silent_notify(event,message): pass
use_notifier = silent_notify
if args.get('console') is None:
from jnpr.junos import Device
dev = Device(args['host'], user=args['user'], password=args['passwd'])
try:
dev.open()
except Exception as err:
msg = 'unable to connect to {}: {}'.format(args['host'], str(err))
module.fail_json(msg=msg)
# --- UNREACHABLE ---
dev.cli('request system zeroize')
results['changed'] = True
# no close, we're done after this point.
else:
from netconify.cmdo import netconifyCmdo
nc_args = []
nc_args.append( args['console'] )
nc_args.append( '--zeroize')
if args['user'] is not None: nc_args.append('--user='+args['user'])
if args['passwd'] is not None: nc_args.append('--passwd='+args['passwd'])
nc = netconifyCmdo(notify=use_notifier)
nc.run( nc_args )
results['changed'] = True
use_notifier('DONE','OK')
module.exit_json(**results)
from ansible.module_utils.basic import *
main()