forked from arista-netdevops-community/cvaas-migration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvaas_migrationV2.yaml
190 lines (185 loc) · 6.88 KB
/
cvaas_migrationV2.yaml
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# This playbook is aimed to migrate all the devices, configlets and containers from an on-prem CVP to CVaaS
# Author: Tamas Plugor - EMEA
---
- name: "Fetch CV on-prem device inventory and generate CVaaS onboarding token"
hosts: cv_server
connection: local
gather_facts: no
vars:
cvaas_api: https://www.cv-staging.corp.arista.io/api/resources/admin.Enrollment/AddEnrollmentToken # change the URL to the right CVaaS instance
cvaas_token: "{{lookup('env', 'CVAAS_TOKEN')}}"
cvp_device_api: "https://192.0.2.178/cvpservice/inventory/devices?provisioned=true" # change the IP to the right CVP On-prem instance
cvp_token: "{{lookup('env', 'ON_PREM_TOKEN')}}"
tasks:
- name: 'Collect devices facts from {{inventory_hostname}}'
tags: [facts, phase1]
arista.cvp.cv_facts_v3:
facts:
- devices
- configlets
- containers
register: facts_devices
- name: "Get Current Device Inventory Data From CVP REST API"
tags: [build, phase1, devinv]
ansible.builtin.uri:
url: "{{ cvp_device_api }}"
method: GET
headers:
Accept: "application/json"
Cookie: "access_token={{ cvp_token }}"
validate_certs: no
return_content: yes
register: current_cvp_devices_api_result
until: current_cvp_devices_api_result.status == 200
retries: 10
delay: 2
# Build list of device IPs from the inventory REST API call
- name: "Building list of IP addresses"
tags: [build, phase1, devinv]
set_fact:
device_inventory: "{{ device_inventory | default([]) + [item.ipAddress] }}"
with_items: "{{ current_cvp_devices_api_result.content }}"
- name: Concatenating device_inventory
tags: [build, phase1]
set_fact:
device_inventory_list: "{{ device_inventory | join('\n') }}"
- name: "Building temporary device inventory"
tags: [build, phase1, devinv]
copy:
content: "{{ device_inventory_list }}"
dest: "./inventory/onprem_devices.yaml"
delegate_to: localhost
- name: "Update onprem_devices.yaml file with group tag..."
tags: [build, phase1, devinv]
lineinfile:
path: "./inventory/onprem_devices.yaml"
line: '[onprem_devices]'
insertbefore: BOF
- name: "Generating TerminAttr onboarding token on CVaaS instance"
tags: [build, phase1]
ansible.builtin.uri:
url: "{{ cvaas_api }}"
method: POST
headers:
Accept: "application/json"
Cookie: "access_token={{ cvaas_token }}"
validate_certs: no
return_content: yes
body: '{"enrollmentToken": {"reenrollDevices": ["*"], "validFor": "86400s"}}'
register: cv_onboarding_token
until: cv_onboarding_token.status == 200
retries: 10
delay: 2
# Writing cvaas token to file
- name: "Parsing CVaaS token"
tags: [build, phase1]
set_fact:
cv_onboarding_token: "{{ cv_onboarding_token.content | list | join }}"
- name: "Writing CVaaS token to file"
tags: [phase1]
copy:
content: "{{ cv_onboarding_token[0].enrollmentToken.token }}"
dest: "./cv-onboarding-token"
- name: Backing up CVP Facts
hosts: localhost
connection: local
vars:
cv_facts_dir: "cv_facts"
documentation_dir: "documentation"
tasks:
- name: Create required output directories if not present
tags: [build, provision, phase1]
file:
path: "{{ item }}"
state: directory
mode: 0775
loop:
- "{{ documentation_dir }}"
- "{{ cv_facts_dir }}"
delegate_to: localhost
run_once: true
- name: "Writing on-prem device facts from {{inventory_hostname}} to file"
tags: [facts, phase1]
copy:
content: "{{ hostvars['cv_server'].facts_devices.data.cvp_devices | to_nice_yaml( width=50, explicit_start=True, explicit_end=True) }}"
dest: "{{ cv_facts_dir }}/cvp_devices.yaml"
delegate_to: localhost
- name: "Writing on-prem container facts from {{inventory_hostname}} to file"
tags: [facts, phase1]
copy:
content: "{{ hostvars['cv_server'].facts_devices.data.cvp_containers | to_nice_yaml( width=50, explicit_start=True, explicit_end=True) }}"
dest: "{{ cv_facts_dir }}/cvp_containers.yaml"
delegate_to: localhost
- name: "Writing on-prem configlet facts from {{inventory_hostname}} to file"
tags: [facts, phase1]
copy:
content: "{{ hostvars['cv_server'].facts_devices.data.cvp_configlets | to_nice_yaml( width=50, explicit_start=True, explicit_end=True) }}"
dest: "{{ cv_facts_dir }}/cvp_configlets.yaml"
delegate_to: localhost
- name: "Copying the CVaaS token and reconfiguring TerminAttr"
hosts: onprem_devices
vars:
ansible_user: cvpadmin
ansible_password: arastra
ansible_connection: network_cli
ansible_httpapi_use_ssl: True
ansible_httpapi_validate_certs: False
ansible_network_os: eos
ansible_httpapi_port: 443
ansible_python_interpreter: $(which python3)
tasks:
- name: Copy CVaaS token
tags: [build, phase1]
net_put:
src: "./cv-onboarding-token"
dest: "/tmp/cv-onboarding-token"
- name: "Configuring TerminAttr on {{ inventory_hostname }}"
eos_command:
commands:
- "enable"
- "config"
- "daemon TerminAttr"
- "exec /usr/bin/TerminAttr -cvaddr=apiserver.cv-staging.corp.arista.io:443 -cvcompression=gzip -cvvrf=MGMT -taillogs -cvauth=token-secure,/tmp/cv-onboarding-token -smashexcludes=ale,flexCounter,hardware,kni,pulse,strata -ingestexclude=/Sysdb/cell/1/agent,/Sysdb/cell/2/agent -cvgnmi"
- "shut"
- "no shut"
- name: "CVaaS - upload configlets, create container hierarchy, move devices to their target containers and assign configlets"
hosts: cvaas
connection: local
gather_facts: no
vars:
CVP_CONFIGLETS:
terminattr_config_cvaas: "{{lookup('file', 'terminattr.cfg')}}"
tasks:
- name: "Push config to {{inventory_hostname}}"
tags: [cvaas, phase2]
arista.cvp.cv_configlet_v3:
configlets: "{{hostvars['cv_server'].facts_devices.data.cvp_configlets}}"
state: present
delegate_to: cvaas
- name: "Push new TerminAttr configlet"
tags: [cvaas, phase2]
arista.cvp.cv_configlet_v3:
configlets: "{{CVP_CONFIGLETS}}"
state: present
delegate_to: cvaas
- name: "Append TerminAttr config to device facts"
tags: [cvaas, phase2]
set_fact:
new_devices_var: |
{% set res = hostvars['cv_server'].facts_devices.data.cvp_devices -%}
{% for device in res -%}
{% do device.configlets.append("terminattr_config_cvaas") -%}
{%- endfor %}
{{ res }}
- name: "Generate container hierarchy"
tags: [cvaas, phase2]
arista.cvp.cv_container_v3:
topology: "{{hostvars['cv_server'].facts_devices.data.cvp_containers}}"
state: present
- name: "Move devices to target containers and assign configlets"
tags: [cvaas, phase2]
arista.cvp.cv_device_v3:
devices: "{{new_devices_var}}"
state: present
search_key: fqdn
delegate_to: cvaas