forked from ktbyers/nornir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.py
82 lines (64 loc) · 2.3 KB
/
configure.py
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
"""
In this example we write a CLI tool with brigade and click to deploy configuration.
"""
import logging
from brigade.core import Brigade
from brigade.plugins.inventory.simple import SimpleInventory
from brigade.plugins.tasks import data, networking, text
import click
def base_config(task):
"""
1. logs all the facts, even the ones inherited from groups
2. Creates a placeholder for device configuration
3. Initializes some basic configuration
"""
logging.info({task.host.name: task.host.items()})
task.host["config"] = ""
r = text.template_file(task=task,
template="base.j2",
path="templates/base/{nos}")
task.host["config"] += r["result"]
def configure_interfaces(task):
"""
1. Load interface data from an external yaml file
2. Creates interface configuration
"""
r = data.load_yaml(task=task,
file="extra_data/{host}/interfaces.yaml")
task.host["interfaces"] = r["result"]
r = text.template_file(task=task,
template="interfaces.j2",
path="templates/interfaces/{nos}")
task.host["config"] += r["result"]
def deploy_config(task):
"""
1. Load configuration into the device
2. Prints diff
"""
r = networking.napalm_configure(task=task,
replace=False,
configuration=task.host["config"])
click.secho("--- {} ({})".format(task.host, r.changed), fg="blue", bold=True)
click.secho(r.diff, fg='yellow')
click.echo()
@click.command()
@click.option('--commit/--no-commit', default=False)
@click.option('--debug/--no-debug', default=False)
@click.argument('site')
@click.argument('role')
def deploy(commit, debug, site, role):
logging.basicConfig(
filename="log",
level=logging.DEBUG if debug else logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
)
brigade = Brigade(
inventory=SimpleInventory("hosts.yaml", "groups.yaml"),
dry_run=not commit,
)
filtered = brigade.filter(site=site, role=role)
filtered.run(task=base_config)
filtered.run(task=configure_interfaces)
filtered.run(task=deploy_config)
if __name__ == "__main__":
deploy()