forked from ktbyers/ansible-junos-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
junos_get_facts
175 lines (158 loc) · 5.92 KB
/
junos_get_facts
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
172
173
174
175
#!/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_get_facts
author: Jeremy Schulman, Juniper Networks
version_added: "1.6.0"
short_description: Retrieve Junos device facts
description:
- Junos facts returns as a JSON dictionary, includes items
such as version, serial-number, product module, etc. Similar
to facts gathered by other IT frameworks. Support for both
NETCONF and CONSOLE based retrieval.
requirements:
- py-junos-eznc (NETCONF)
- netconify if using the I(console) option
options:
host:
description:
- should be {{ inventory_hostname }}
required: true
user:
description:
- login user-name
required: false
default: $USER
passwd:
description:
- login password
required: false
default: assumes ssh-key
console:
description:
- CONSOLE port, per the B(netconify) utility
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. Used only with I(console) option.
required: false
default: $CWD
logfile:
description:
- path on the local server where progress status is logged
for debugging purposes. Used only with I(console) option
required: false
default: None
'''
EXAMPLES = '''
# getting facts via NETCONF, assumes ssh-keys
- junos_get_faces: host={{ inventory_hostname }}
register: junos
# getting facts via CONSOLE, assumes Amnesiac system
# root login, no password
- junos_get_facts:
host={{ inventory_hostname }}
user=root
console="--telnet={{TERMSERV}},{{TERMSERVPORT}}"
savedir=/usr/local/junos/inventory
register: junos
# accessing the facts
- name: version
debug: msg="{{ junos.facts.version }}"
'''
import sys
def main():
module = AnsibleModule(
argument_spec = dict(
host=dict(required=True),
console=dict(required=False, default=None),
logfile=dict(required=False, default=None),
savedir=dict(required=False, default=None),
user=dict(required=False, default=os.getenv('USER')),
passwd=dict(required=False, default=None)),
supports_check_mode = False)
m_args = module.params
m_results = dict(changed=False)
if m_args['console'] is None:
from jnpr.junos import Device
# -----------
# via NETCONF
# -----------
dev = Device(m_args['host'], user=m_args['user'], passwd=m_args['passwd'])
try:
dev.open()
except Exception as err:
msg = 'unable to connect to {}: {}'.format(m_args['host'], str(err))
module.fail_json(msg=msg)
return
else:
dev.close()
del dev.facts['version_info'] # currently not JSON serializable.
m_results['facts'] = dev.facts
else:
# -----------
# via CONSOLE
# -----------
from netconify.cmdo import netconifyCmdo
import logging
c_args = []
c_args.append(m_args['console'])
c_args.append('--facts')
if m_args['savedir'] is not None:
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'])
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:'+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
nc = netconifyCmdo(notify=use_notifier)
c_results = nc.run( c_args )
m_results['args'] = m_args # for debug
m_results['facts'] = c_results['facts']
module.exit_json(**m_results)
from ansible.module_utils.basic import *
main()