-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlab_setup.yml
205 lines (205 loc) · 6.42 KB
/
lab_setup.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
204
205
---
#ansible-playbook lab_setup.yml -e "dest_name=testlab count=3" -i inventories/bipod/hosts
- name: "Create a gns3 project and start a base topology"
hosts: localhost
gather_facts: False
tasks:
- name: "Remove network topology"
gns3_project:
url: "{{ gns3_url }}"
project_name: "{{ project_name }}"
state: absent
tags:
- create
- start
- provision
- remove
ignore_errors: yes
- name: "Create network topology"
gns3_project:
url: "{{ gns3_url }}"
project_name: "{{ project_name }}"
state: present
nodes_spec: "{{ gns3_nodes_spec }}"
links_spec: "{{ gns3_links_spec }}"
register: project_facts
tags:
- create
- start
- provision
- duplicate
# - debug: var=project_facts.project.project_id
- name: "Start nodes in the project"
gns3_project:
url: "{{ gns3_url }}"
project_name: "{{ project_name }}"
state: opened
nodes_state: started
nodes_strategy: one_by_one
tags:
- start
- provision
- name: "Get the nodes facts"
gns3_nodes_inventory:
url: "{{ gns3_url }}"
project_name: "{{ project_name }}"
register: nodes_inventory
tags:
- start
- provision
# - debug: var=nodes_inventory
- name: "Pause for 2 minutes to start all nodes"
pause:
minutes: 2
tags:
- start
- provision
- name: "Provision nodes for Ansible management"
hosts: all
gather_facts: False
vars:
ansible_connection: local
ansible_python_interpreter: "{{ ansible_playbook_python }}"
# template: "templates/iosv_default_config.j2"
tasks:
# - debug: var=template
- name: "Retrieving nodes inventory information"
set_fact:
nodes_inventory: "{{ hostvars['localhost']['nodes_inventory']['nodes_inventory'] }}"
tags:
- provision
- name: Generate boilerplate config files
block:
- name: Create build folder
file:
path: "configs/build"
state: directory
- name: Generate config
template:
src: "{{ template }}"
dest: "configs/build/{{ inventory_hostname }}.conf"
tags:
- provision
# - debug: var=hostvars
- name: "Deploy boilerplate configuration"
block:
- name: "Generate configuration variable"
set_fact:
boilerplate_config: "{{ lookup('template', '{{ template }}') }}"
- name: "Push config on iosv_l3 nodes"
when: ansible_network_os == "ios" and image_style == "iosv_l3"
gns3_telnet_console:
remote_addr: "{{ nodes_inventory[inventory_hostname]['server'] }}"
port: "{{ nodes_inventory[inventory_hostname]['console_port'] }}"
send_newline: yes
command: "{{ boilerplate_config.splitlines() }}"
pause: 1
timeout:
general: 180
pre_login: 60
post_login: 60
login_prompt: 30
config_dialog: 30
- name: "Push config on iosv_l2 nodes"
when: ansible_network_os == "ios" and image_style == "iosv_l2"
gns3_telnet_console:
remote_addr: "{{ nodes_inventory[inventory_hostname]['server'] }}"
port: "{{ nodes_inventory[inventory_hostname]['console_port'] }}"
send_newline: yes
login_prompt:
- ">"
user: ""
password: ""
prompts:
- "[#]"
command: "{{ boilerplate_config.splitlines() }}"
pause: 2
timeout:
general: 240
pre_login: 120
post_login: 120
login_prompt: 60
config_dialog: 60
- name: "Push config on openwrt nodes"
when: image_style == "openwrt"
gns3_telnet_console:
remote_addr: "{{ nodes_inventory[inventory_hostname]['server'] }}"
port: "{{ nodes_inventory[inventory_hostname]['console_port'] }}"
send_newline: yes
command: "{{ boilerplate_config.splitlines() }}"
pause: 1
timeout:
general: 240
pre_login: 120
post_login: 60
login_prompt: 30
config_dialog: 30
- name: "Push config on centos nodes"
when: image_style == "centos"
telnet:
host: "{{ nodes_inventory[inventory_hostname]['server'] }}"
port: "{{ nodes_inventory[inventory_hostname]['console_port'] }}"
login_prompt: "{{ login_prompt | default('localhost login:') }}"
user: "{{ ansible_user }}"
password: "{{ ansible_password }}"
send_newline: yes
prompts:
- "[#]"
command: "{{ boilerplate_config.splitlines() }}"
timeout: 30
pause: 2
tags:
- provision
- name: "Pause for 5 minutes to build all nodes"
pause:
minutes: 5
tags:
- provision
- name: "Stop all nodes"
gns3_project:
url: "{{ gns3_url }}"
project_name: "{{ project_name }}"
state: opened
nodes_state: stopped
nodes_strategy: one_by_one
nodes_delay: 3
tags:
- provision
- duplicate
- name: "Close the project"
gns3_project:
url: "{{ gns3_url }}"
project_name: "{{ project_name }}"
state: closed
tags:
- provision
- duplicate
- name: "Duplicate the project"
hosts: localhost
gather_facts: False
vars:
count: 1 # should be defined as extra-vars '-e'
dest_name: '{{ project_name | default("ccna") }}' # should be defined as extra-vars '-e'
tasks:
- setup:
tags:
- duplicate
- startup
- name: "Duplicate the project"
uri:
url: "{{ gns3_url }}:3080/v2/projects/{{ project_facts.project.project_id }}/duplicate"
method: POST
body: >
{"name": "{{ ansible_date_time.date }}-{{ dest_name }}-{{ item }}"}
status_code: 201
body_format: json
with_sequence: count={{ count }} start=1
tags:
- duplicate
- name: "Print projects name"
debug:
msg: |
{"name": "{{ ansible_date_time.date }}-{{ dest_name }}-{{ item }}"}
with_sequence: count={{ count }} start=1
tags:
- duplicate