forked from ktbyers/ansible-junos-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
junos_console_devicemode
172 lines (157 loc) · 5.82 KB
/
junos_console_devicemode
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
161
162
163
164
165
166
167
168
169
170
171
#!/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_console_devicemode
author: Jeremy Schulman, Juniper Networks
version_added: "1.5.0"
short_description: Shutdown or Reboot the Junos device
description:
- New Out of Box (NOOB) console configuration that will change the "device mode".
For QFX devices in QFabric deployments, the QFX switch must be
configured into 'node' mode. This action is performed over the device
CONSOLE port using either a SERIAL-PORT or a TERMINAL-SERVER.
This action will also log the device "facts" and "inventory" to a
local server directory, which then provdies you with specific details
about the device located on that console. Refer to netconify for
further details on this process.
requirements:
- netconify
options:
host:
description:
- should be {{ inventory_hostname }}
required: true
user:
description:
- login user-name
required: false
default: root
passwd:
description:
- login password
required: false
default: None
port:
description:
- CONSOLE port, per the B(netconify) utility
required: true
default: None
qfx:
description:
- determine the QFX device mode
required: true
choices: ['node','switch']
default: node
logfile:
description:
- path on the local server where progress status is logged
for debugging purposes
required: false
default: None
savedir:
description:
- path to the local server directory where device facts and
inventory files will be stored. Refer to the B(netconify)
utility for details. If the path is not explicit, it will
be local to the playbook execution directory
required: false
default: None
'''
EXAMPLES = '''
- junos_console_devicemode:
host={{ inventory_hostname }}
port='-t={{TERMSERV}},{{TERMSERVPORT}}'
qfx=node
logfile=/usr/local/junos/logs/qfx_node.log
savedir=/usr/local/junos/inventory
'''
import sys
import logging
from netconify.cmdo import *
def main():
module = AnsibleModule(
argument_spec = dict(
host=dict(required=True),
console=dict(required=True),
qfx=dict(required=False, default=None, choices=['node','switch']),
logfile=dict(required=False, default=None),
savedir=dict(required=False),
user=dict(required=False, default='root'),
passwd=dict(required=False, default=None)
),
supports_check_mode = False )
# -----------------------------------------------
# build the command args to the netconify utility
# -----------------------------------------------
m_args = module.params
c_args = [] # for netconify
gather_facts = False
c_args.append(m_args['console'])
if m_args['qfx'] is not None:
c_args.append('--qfx-'+m_args['qfx'])
if m_args['savedir'] is not None:
# gather facts and save them to local filesys
gather_facts = True
c_args.append('--facts')
c_args.append('--savedir='+m_args['savedir'])
c_args.append('--user='+m_args['user'])
if m_args['passwd'] is not None:
c_args.append('--passwd='+m_args['passwd'])
# the final arg should be the name we designate to this device.
c_args.append(m_args['host'])
logfile = m_args['logfile']
if logfile is not None:
logging.basicConfig(filename=logfile, level=logging.INFO,
format='%(asctime)s:%(name)s:%(message)s')
logging.getLogger().name = 'NETCONIFY:'+m_args['host']
def log_notify(event,message):
log_notify.event = event
log_notify.message = message
logging.info("%s:%s"%(event,message))
use_notifier = log_notify
else:
def silent_notify(event,message): pass
use_notifier = silent_notify
nc = netconifyCmdo(notify=use_notifier)
c_results = nc.run( c_args )
m_results = dict(changed=c_results['changed'])
m_results['args'] = m_args # just for debug really.
if gather_facts is True:
m_results['facts'] = c_results['facts']
if c_results['failed'] is True:
module.fail_json(msg=c_results['errmsg'])
else:
module.exit_json(**m_results)
from ansible.module_utils.basic import *
main()