0
# Configuration Management
1
2
Configuration management utilities for loading, committing, rolling back, and managing Junos device configurations. Supports multiple configuration formats, advanced commit options, database locking, and configuration comparison operations.
3
4
## Capabilities
5
6
### Config Utility Class
7
8
The Config utility provides comprehensive configuration management capabilities including loading configurations in multiple formats, committing changes with various options, and managing configuration database locks.
9
10
```python { .api }
11
class Config:
12
def __init__(self, dev):
13
"""
14
Initialize Config utility bound to a device.
15
16
Parameters:
17
- dev (Device): Device object to bind to
18
"""
19
20
def load(
21
self,
22
config_data,
23
format='text',
24
merge=False,
25
overwrite=False,
26
template_vars=None,
27
path=None,
28
ignore_warning=None,
29
timeout=None
30
):
31
"""
32
Load configuration data into the candidate configuration.
33
34
Parameters:
35
- config_data (str|dict): Configuration data to load
36
- format (str): Configuration format ('text', 'set', 'xml', 'json')
37
- merge (bool): Merge with existing configuration (default: True)
38
- overwrite (bool): Overwrite existing configuration
39
- template_vars (dict): Variables for Jinja2 template processing
40
- path (str): Configuration hierarchy path for partial loads
41
- ignore_warning (list): List of warnings to ignore
42
- timeout (int): Load operation timeout
43
44
Raises:
45
- ConfigLoadError: Configuration load failed
46
- LockError: Configuration database is locked
47
"""
48
49
def commit(
50
self,
51
comment=None,
52
confirm=None,
53
timeout=None,
54
full=False,
55
detail=False,
56
sync=False
57
):
58
"""
59
Commit the candidate configuration.
60
61
Parameters:
62
- comment (str): Commit comment
63
- confirm (int): Automatic rollback timeout in minutes
64
- timeout (int): Commit operation timeout
65
- full (bool): Perform full commit (bypass commit scripts)
66
- detail (bool): Return detailed commit information
67
- sync (bool): Synchronize commit across routing engines
68
69
Returns:
70
- bool: True if commit successful
71
72
Raises:
73
- CommitError: Commit operation failed
74
- LockError: Configuration is locked by another user
75
"""
76
77
def commit_check(self):
78
"""
79
Check candidate configuration for errors without committing.
80
81
Returns:
82
- bool: True if configuration is valid
83
84
Raises:
85
- CommitError: Configuration has errors
86
"""
87
```
88
89
### Configuration Database Operations
90
91
Operations for managing configuration database locks, comparing configurations, and rolling back to previous configurations.
92
93
```python { .api }
94
def lock(self):
95
"""
96
Lock the configuration database for exclusive access.
97
98
Raises:
99
- LockError: Unable to acquire configuration lock
100
"""
101
102
def unlock(self):
103
"""
104
Unlock the configuration database.
105
106
Raises:
107
- UnlockError: Unable to release configuration lock
108
"""
109
110
def diff(self, rb_id=0, timeout=None):
111
"""
112
Show differences between candidate and active configurations.
113
114
Parameters:
115
- rb_id (int): Rollback ID to compare against (0=current active)
116
- timeout (int): Operation timeout
117
118
Returns:
119
- str: Configuration differences in unified diff format
120
"""
121
122
def rollback(self, rb_id=0, timeout=None):
123
"""
124
Load a previous configuration as the candidate configuration.
125
126
Parameters:
127
- rb_id (int): Rollback configuration ID (0=current, 1=previous, etc.)
128
- timeout (int): Rollback operation timeout
129
130
Raises:
131
- ConfigLoadError: Unable to load rollback configuration
132
"""
133
```
134
135
### Rescue Configuration Management
136
137
Manage rescue configurations for device recovery scenarios, allowing save and restore operations for emergency configurations.
138
139
```python { .api }
140
def rescue(self, action=None, timeout=None):
141
"""
142
Manage rescue configuration operations.
143
144
Parameters:
145
- action (str): Rescue action ('save', 'get', 'delete')
146
- 'save': Save current configuration as rescue
147
- 'get': Load rescue configuration as candidate
148
- 'delete': Delete rescue configuration
149
- timeout (int): Operation timeout
150
151
Returns:
152
- bool: True if operation successful
153
154
Raises:
155
- RpcError: Rescue operation failed
156
"""
157
```
158
159
### Configuration Binding
160
161
Bind the Config utility to a Device instance to enable configuration management operations.
162
163
```python { .api }
164
# Binding Config utility to device
165
dev.bind(cu=Config) # 'cu' is conventional name for Config utility
166
167
# Access bound utility
168
dev.cu.load(config_data)
169
dev.cu.commit()
170
```
171
172
## Usage Examples
173
174
### Basic Configuration Management
175
176
```python
177
from jnpr.junos import Device
178
from jnpr.junos.utils.config import Config
179
180
dev = Device(host='router1.example.com', user='admin', passwd='secret')
181
dev.open()
182
183
# Bind Config utility
184
dev.bind(cu=Config)
185
186
# Load configuration using set commands
187
config_data = '''
188
set interfaces ge-0/0/0 description "WAN Connection"
189
set interfaces ge-0/0/0 unit 0 family inet address 192.168.1.1/24
190
'''
191
192
dev.cu.load(config_data, format='set')
193
194
# Check configuration before committing
195
if dev.cu.commit_check():
196
print("Configuration is valid")
197
198
# Show what will change
199
diff = dev.cu.diff()
200
print("Configuration changes:")
201
print(diff)
202
203
# Commit with comment
204
dev.cu.commit(comment='Added WAN interface configuration')
205
print("Configuration committed successfully")
206
else:
207
print("Configuration has errors")
208
209
dev.close()
210
```
211
212
### XML Configuration Loading
213
214
```python
215
from jnpr.junos import Device
216
from jnpr.junos.utils.config import Config
217
218
dev = Device(host='router1.example.com', user='admin', passwd='secret')
219
dev.open()
220
dev.bind(cu=Config)
221
222
# Load XML configuration
223
xml_config = '''
224
<configuration>
225
<interfaces>
226
<interface>
227
<name>ge-0/0/1</name>
228
<description>LAN Connection</description>
229
<unit>
230
<name>0</name>
231
<family>
232
<inet>
233
<address>
234
<name>10.0.1.1/24</name>
235
</address>
236
</inet>
237
</family>
238
</unit>
239
</interface>
240
</interfaces>
241
</configuration>
242
'''
243
244
dev.cu.load(xml_config, format='xml')
245
dev.cu.commit(comment='Added LAN interface via XML')
246
247
dev.close()
248
```
249
250
### Configuration with Template Variables
251
252
```python
253
from jnpr.junos import Device
254
from jnpr.junos.utils.config import Config
255
256
dev = Device(host='router1.example.com', user='admin', passwd='secret')
257
dev.open()
258
dev.bind(cu=Config)
259
260
# Configuration template with variables
261
config_template = '''
262
set interfaces {{ interface }} description "{{ description }}"
263
set interfaces {{ interface }} unit 0 family inet address {{ ip_address }}
264
'''
265
266
# Load configuration with template variables
267
dev.cu.load(
268
config_template,
269
format='set',
270
template_vars={
271
'interface': 'ge-0/0/2',
272
'description': 'Template Interface',
273
'ip_address': '10.0.2.1/24'
274
}
275
)
276
277
dev.cu.commit(comment='Interface configured using template')
278
279
dev.close()
280
```
281
282
### Advanced Commit Operations
283
284
```python
285
from jnpr.junos import Device
286
from jnpr.junos.utils.config import Config
287
288
dev = Device(host='router1.example.com', user='admin', passwd='secret')
289
dev.open()
290
dev.bind(cu=Config)
291
292
# Load some configuration
293
config = 'set system host-name new-hostname'
294
dev.cu.load(config, format='set')
295
296
# Commit with confirm (auto-rollback) for safety
297
dev.cu.commit(
298
comment='Hostname change with auto-rollback',
299
confirm=5 # Auto-rollback in 5 minutes if not confirmed
300
)
301
302
print("Configuration committed with 5-minute auto-rollback")
303
304
# To confirm the commit and prevent rollback
305
# dev.cu.commit()
306
307
dev.close()
308
```
309
310
### Configuration Database Locking
311
312
```python
313
from jnpr.junos import Device
314
from jnpr.junos.utils.config import Config
315
from jnpr.junos.exception import LockError
316
317
dev = Device(host='router1.example.com', user='admin', passwd='secret')
318
dev.open()
319
dev.bind(cu=Config)
320
321
try:
322
# Lock configuration database
323
dev.cu.lock()
324
print("Configuration database locked")
325
326
# Load and commit configuration
327
config = 'set system location building "Main Office"'
328
dev.cu.load(config, format='set')
329
dev.cu.commit(comment='Updated system location')
330
331
except LockError:
332
print("Configuration is locked by another user")
333
finally:
334
# Always unlock when done
335
try:
336
dev.cu.unlock()
337
print("Configuration database unlocked")
338
except:
339
pass
340
341
dev.close()
342
```
343
344
### Configuration Rollback
345
346
```python
347
from jnpr.junos import Device
348
from jnpr.junos.utils.config import Config
349
350
dev = Device(host='router1.example.com', user='admin', passwd='secret')
351
dev.open()
352
dev.bind(cu=Config)
353
354
# View current configuration differences
355
current_diff = dev.cu.diff()
356
print("Current candidate configuration:")
357
print(current_diff)
358
359
# Rollback to previous configuration
360
dev.cu.rollback(rb_id=1) # Load the previous committed configuration
361
362
# Show what the rollback would change
363
rollback_diff = dev.cu.diff()
364
print("Changes from rollback:")
365
print(rollback_diff)
366
367
# Commit the rollback
368
dev.cu.commit(comment='Rolled back to previous configuration')
369
370
dev.close()
371
```
372
373
### Rescue Configuration Operations
374
375
```python
376
from jnpr.junos import Device
377
from jnpr.junos.utils.config import Config
378
379
dev = Device(host='router1.example.com', user='admin', passwd='secret')
380
dev.open()
381
dev.bind(cu=Config)
382
383
# Save current configuration as rescue configuration
384
dev.cu.rescue(action='save')
385
print("Current configuration saved as rescue")
386
387
# Later, if needed, load rescue configuration
388
dev.cu.rescue(action='get')
389
print("Rescue configuration loaded as candidate")
390
391
# Check what rescue configuration contains
392
diff = dev.cu.diff()
393
print("Rescue configuration differences:")
394
print(diff)
395
396
# Commit rescue configuration
397
dev.cu.commit(comment='Restored from rescue configuration')
398
399
dev.close()
400
```
401
402
### Error Handling
403
404
```python
405
from jnpr.junos import Device
406
from jnpr.junos.utils.config import Config
407
from jnpr.junos.exception import ConfigLoadError, CommitError, LockError
408
409
dev = Device(host='router1.example.com', user='admin', passwd='secret')
410
dev.open()
411
dev.bind(cu=Config)
412
413
try:
414
# Attempt to load invalid configuration
415
invalid_config = 'set interfaces invalid-interface-name unit 0'
416
dev.cu.load(invalid_config, format='set')
417
418
except ConfigLoadError as e:
419
print(f"Configuration load error: {e}")
420
421
try:
422
# Attempt to commit configuration with errors
423
dev.cu.commit()
424
425
except CommitError as e:
426
print(f"Commit error: {e}")
427
print("Error details:", e.rpc_error)
428
429
except LockError as e:
430
print(f"Lock error: {e}")
431
432
dev.close()
433
```
434
435
## Types
436
437
```python { .api }
438
# Configuration data types
439
ConfigData = str | dict # Configuration in text, set, XML, or JSON format
440
ConfigFormat = str # 'text', 'set', 'xml', 'json'
441
442
# Template variable types
443
TemplateVars = dict[str, any] # Variables for Jinja2 template processing
444
445
# Commit options
446
CommitOptions = dict[str, any] # Dictionary of commit parameters
447
448
# Configuration comparison result
449
ConfigDiff = str # Unified diff format configuration differences
450
451
# Rollback ID type
452
RollbackId = int # Configuration rollback identifier (0=current, 1=previous, etc.)
453
```