-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathansible_cli_templates.yml
203 lines (172 loc) · 6.42 KB
/
ansible_cli_templates.yml
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
191
192
193
194
195
196
197
198
199
200
201
202
203
---
- name: Ansible CLI Templates
hosts: localhost
connection: local
gather_facts: false
vars_files:
- credentials.yml
- params.yml
vars:
dnac_host: "{{dnac_host}}"
dnac_username: "{{username}}"
dnac_password: "{{password}}"
device_name: "{{device_name}}"
cli_template: "{{cli_template}}"
dnac_login: &dnac_login
dnac_host: "{{dnac_host}}"
dnac_username: "{{dnac_username}}"
dnac_password: "{{dnac_password}}"
dnac_version: 2.2.2.3
dnac_verify: False
dnac_debug: False
tasks:
- name: Get start timestamp from the system
shell: "date +%Y-%m-%dT%H-%M-%S"
register: timestamp
delegate_to: localhost
- name: Print playbook start timestamp
ansible.builtin.debug:
msg: "Playbook start time: {{timestamp.stdout}}"
- name: "Verify if existing project with the name: {{project_name}}"
cisco.dnac.configuration_template_project_info:
<<: *dnac_login
name: "{{project_name}}"
register: project_info
- name: Project not found
ansible.builtin.debug:
msg: "Project with the name: {{project_name}}, not found"
when: project_info.dnac_response == []
- name: Create new project
cisco.dnac.configuration_template_project:
<<: *dnac_login
name: "{{project_name}}"
register: create_project_info
when: project_info.dnac_response == []
- name: Print create project task id
ansible.builtin.debug:
msg: "Create a new project, task id is: {{create_project_info.dnac_response.response.taskId}}"
when: project_info.dnac_response == []
- name: Sleep for 10 seconds to create project and continue with play
wait_for:
timeout: 10
delegate_to: localhost
when: project_info.dnac_response == []
- name: Get the project id
cisco.dnac.configuration_template_project_info:
<<: *dnac_login
name: "{{project_name}}"
register: project_info
- name: Parse project id
set_fact:
project_id: "{{project_info.dnac_response[0].id}}"
- name: Print project id
ansible.builtin.debug:
msg: "Project id is: {{project_id}}"
- name: "Create new CLI Template with the name: {{template_name}}-{{timestamp.stdout}}"
cisco.dnac.configuration_template_create:
<<: *dnac_login
name: "{{template_name}}-{{timestamp.stdout}}"
author: Ansible
deviceTypes:
- productFamily: Routers
- productFamily: Switches and Hubs
softwareType: IOS-XE
softwareVariant: XE
templateContent: "{{cli_template}}"
projectId: "{{project_id}}"
projectName: "{{project_name}}"
language: JINJA
register: create_template_response
- name: Parse the create template task id
set_fact:
task_id: "{{create_template_response.dnac_response.response.taskId}}"
- name: Print create template task id
ansible.builtin.debug:
msg: "Create a new template, task id is: {{task_id}}"
- name: Sleep for 10 seconds to create template and continue with play
wait_for:
timeout: 10
delegate_to: localhost
- name: Get the template id
cisco.dnac.task_info:
<<: *dnac_login
taskId: "{{task_id}}"
register: task_info
- name: Parse the template id
set_fact:
template_id: "{{task_info.dnac_response.response.data}}"
- name: Print template id
ansible.builtin.debug:
msg: "Template id is: {{template_id}}}"
- name: Commit template
cisco.dnac.configuration_template_version_create:
<<: *dnac_login
comments: First Commit by Ansible
templateId: "{{template_id}}"
register: commit_template
- name: Sleep for 5 seconds to commit template and continue with play
wait_for:
timeout: 5
delegate_to: localhost
- name: "Verify if the device {{device_name}} is managed by Cisco DNA Center"
cisco.dnac.network_device_info:
<<: *dnac_login
hostname: "{{device_name}}"
register: device_info
- name: Device not found
ansible.builtin.debug:
msg: "Device with the name: {{device_name}}, not found"
when: device_info.dnac_response.response == []
- name: "Deploy template {{template_name}} to device {{device_name}}"
cisco.dnac.configuration_template_deploy_v2:
<<: *dnac_login
forcePushTemplate: true
isComposite: false
templateId: "{{template_id}}"
targetInfo:
- id: "{{device_name}}"
type: "MANAGED_DEVICE_HOSTNAME"
register: template_deployment_task
when: device_info.dnac_response.response != []
- name: Parse the deployment task id
set_fact:
deployment_task_id: "{{template_deployment_task.dnac_response.response.taskId}}"
when: device_info.dnac_response.response != []
- name: Print template deployment task id
ansible.builtin.debug:
msg: "Deployment task id: {{deployment_task_id}}"
when: device_info.dnac_response.response != []
- name: Sleep for 10 seconds to deploy template and continue with play
wait_for:
timeout: 10
delegate_to: localhost
when: device_info.dnac_response.response != []
- name: Verify the template deployment result
cisco.dnac.task_info:
<<: *dnac_login
taskId: "{{deployment_task_id}}"
register: deployment_task_info
when: device_info.dnac_response.response != []
- name: Parse the deployment task result
set_fact:
deployment_task_result: "{{deployment_task_info.dnac_response.response.isError}}"
when: device_info.dnac_response.response != []
- name: Print successful template deployment result
ansible.builtin.debug:
msg: "Template deployment was successful"
when:
- device_info.dnac_response.response != []
- deployment_task_result == False
- name: Print unsuccessful template deployment result
ansible.builtin.debug:
msg: "Template deployment was not successful"
when:
- device_info.dnac_response.response != []
- deployment_task_result == True
- name: Get end timestamp from the system
shell: "date +%Y-%m-%dT%H-%M-%S"
register: timestamp
delegate_to: localhost
- name: Print timestamp
ansible.builtin.debug:
msg: "Playbook end time: {{timestamp.stdout}}"