forked from napalm-automation/napalm-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnapalm_translate_yang.py
133 lines (109 loc) · 3.47 KB
/
napalm_translate_yang.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
(c) 2017 David Barroso <[email protected]>
This file is part of Ansible
Ansible is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Ansible is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Ansible. If not, see <http://www.gnu.org/licenses/>.
"""
# standard ansible module imports
from ansible.module_utils.basic import AnsibleModule
try:
import napalm_yang
except ImportError:
napalm_yang = None
DOCUMENTATION = '''
---
module: napalm_translate_yang
author: "David Barroso (@dbarrosop)"
version_added: "0.0"
short_description: "Translate a YANG object to native configuration"
description:
- "Load a YANG object from a dict and translates the object to native"
- "configuration"
requirements:
- napalm-yang
options:
models:
description:
- List of models to parse
required: True
profiles:
description:
- List of profiles to use to translate the object
required: True
data:
description:
- dict to load into the YANG object
required: True
merge:
description:
- When translating config, merge resulting config here
required: False
replace:
description:
- When translating config, replace resulting config here
required: False
'''
EXAMPLES = '''
- name: "Translate config"
napalm_translate_yang:
data: "{{ interfaces.yang_model }}"
profiles: ["eos"]
models:
- models.openconfig_interfaces
register: config
'''
RETURN = '''
config:
description: "Native configuration"
returned: always
type: string
sample: "interface Ethernet2\n no switchport\n ip address 192.168.0.1/24 \n"
'''
def get_root_object(models):
"""
Read list of models and returns a Root object with the proper models added.
"""
root = napalm_yang.base.Root()
for model in models:
current = napalm_yang
for p in model.split("."):
current = getattr(current, p)
root.add_model(current)
return root
def main():
module = AnsibleModule(
argument_spec=dict(
models=dict(type="list", required=True),
profiles=dict(type="list", required=False),
data=dict(type='dict', required=True),
merge=dict(type='dict', required=False),
replace=dict(type='dict', required=False),
),
supports_check_mode=True
)
if not napalm_yang:
module.fail_json(msg="the python module napalm-yang is required")
root = get_root_object(module.params["models"])
root.load_dict(module.params["data"])
running = get_root_object(module.params["models"])
args = {}
if module.params["merge"]:
args["merge"] = running
running.load_dict(module.params["merge"])
elif module.params["replace"]:
args["replace"] = running
running.load_dict(module.params["replace"])
config = root.translate_config(profile=module.params["profiles"], **args)
module.exit_json(config=config)
if __name__ == '__main__':
main()