@@ -79,10 +79,12 @@ options:
7979 to retrieve specific config
8080 format:
8181 description:
82- - text - configuration saved as text (curly-brace) format
83- - xml - configuration saved as XML
82+ - text - configuration saved as text (curly-brace) format.
83+ xml - configuration saved as XML.
84+ json - configuration saved as json, supported only for devices >=14.2
85+ Also with this format, rpc_reply attribute can be used with results
8486 required: false
85- choices: ['text','xml']
87+ choices: ['text','xml', 'json' ]
8688 default: 'xml'
8789 dest:
8890 description:
@@ -109,10 +111,6 @@ EXAMPLES = '''
109111 dest=get_interface_information.conf
110112 register=junos
111113
112- # print the config
113- - name: version
114- debug: msg="{{ junos.rpc_reply }}"
115-
116114# Example to fetch device configuration
117115- name: Get Device Configuration
118116 junos_rpc:
@@ -128,6 +126,25 @@ EXAMPLES = '''
128126 filter_xml="<configuration><interfaces/></configuration>"
129127 dest=get_config.conf
130128 register: junos
129+
130+ # Example to fetch configuration in json for >=14.2
131+ - name: Get Device Configuration
132+ hosts: all
133+ roles:
134+ - Juniper.junos
135+ connection: local
136+ gather_facts: no
137+ tasks:
138+ - name: Get rpc run
139+ junos_rpc:
140+ host={{ inventory_hostname }}
141+ rpc=get-interface-information
142+ kwargs={interface_name:em0,media:True}
143+ dest=get_interface_information.conf
144+ register: junos
145+
146+ - name: Print configuration
147+ debug: msg="{{ junos.rpc_reply }}"
131148'''
132149
133150import os
@@ -170,21 +187,22 @@ def junos_rpc(module, dev):
170187 if filter_xml is not None :
171188 filter_xml = etree .XML (filter_xml )
172189 values ['format' ] = args ['format' ]
173- results [ ' rpc_reply' ] = getattr (dev .rpc , rpc .replace ('-' ,'_' ))(filter_xml , options = values )
190+ rpc_reply = getattr (dev .rpc , rpc .replace ('-' ,'_' ))(filter_xml , options = values )
174191 else :
175- results [ ' rpc_reply' ] = getattr (dev .rpc , rpc .replace ('-' ,'_' ))({'format' :args ['format' ]}, ** values )
192+ rpc_reply = getattr (dev .rpc , rpc .replace ('-' ,'_' ))({'format' :args ['format' ]}, ** values )
176193 if module .params ['dest' ] is not None :
177194 with open (module .params ['dest' ], 'w' ) as confile :
178- if isinstance (results [ ' rpc_reply' ] , etree ._Element ):
195+ if isinstance (rpc_reply , etree ._Element ):
179196 if args .get ('format' ) == 'text' :
180- confile .write (( results [ ' rpc_reply' ]) .text )
197+ confile .write (rpc_reply .text )
181198 else :
182- confile .write (etree .tostring (results [ ' rpc_reply' ] ))
199+ confile .write (etree .tostring (rpc_reply ))
183200 else :
184201 if args .get ('format' ) == 'json' :
185- confile .write (str (results ['rpc_reply' ]))
202+ results ['rpc_reply' ] = rpc_reply
203+ confile .write (str (rpc_reply ))
186204 else :
187- confile .write (results [ ' rpc_reply' ] )
205+ confile .write (rpc_reply )
188206
189207 results ['changed' ] = True
190208 except Exception as err :
0 commit comments