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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
---
lastproofread: '2026-03-19'
---
(terraformaz)=
# Deploy VyOS on Microsoft Azure with Terraform and Ansible
You can use Terraform to quickly deploy VyOS-based infrastructure
on Microsoft Azure (hereafter referred to as *Azure*) and remove
infrastructure when it's no longer needed.
Additionally, you can use Ansible for provisioning.
On this page you'll learn how to:
- Create the necessary files for Terraform and Ansible.
- Use Terraform to create a single instance on Azure and use Ansible for
provisioning.
## Prepare to deploy VyOS with Terraform on Azure
To create a single instance and install your configuration using
Terraform, Ansible, and Azure, follow these steps:
### Azure
- Create an [Azure account](https://azure.microsoft.com/).
### Terraform
```{eval-rst}
1. Create an UNIX or Windows instance.
2. Download and install
`Terraform <https://developer.hashicorp.com/terraform/install>`__.
3. Create the folder for example ``/root/azvyos/``.
.. code-block:: none
mkdir /root/azvyos
.. stop_vyoslinter
4. Copy all files into your Terraform project "/root/azvyos"
(``vyos.tf``, ``var.tf``, ``terraform.tfvars``). For more details, see
`Structure of files in Terraform for Azure <#structure-of-files-in-terraform-for-azure>`_.
.. start_vyoslinter
5. Log in to Azure using the command:
.. code-block:: none
az login
6. Run the following commands to initialize Terraform:
.. code-block:: none
cd /<your folder>
terraform init
```
### Ansible
1. Create an UNIX instance either locally or in the cloud.
2. Download and install Ansible
3. Create a folder, for example `/root/az/`.
4. Copy all files into your Ansible project `/root/az/` (`ansible.cfg`,
`instance.yml`, `all`). For more details, see
[Structure of files in Ansible for Azure](#structure-of-files-in-ansible-for-azure)
### Deploy with Terraform
Run the following commands on your Terraform instance:
```none
cd /<your folder>
terraform plan
terraform apply
yes
```
After executing all the commands, your VyOS instance is deployed to
Azure with your configuration.
If you need to delete the instance, run the following command:
```none
terraform destroy
```
## Structure of files in Terraform for Azure
```none
.
├── vyos.tf # The main script
├── var.tf # File for the Terraform version.
└── terraform.tfvars # Values for all variables (passwords,
# login, IP addresses, etc.)
```
## File contents of Terraform for Azure
`vyos.tf`
```none
##############################################################################
# HashiCorp Guide to Using Terraform on Azure
# This Terraform configuration will create the following:
# Resource group with a virtual network and subnet
# A VyOS server without SSH key (only login+password)
##############################################################################
# Choose a provider
provider "azurerm" {
features {}
}
# Create a resource group. In Azure, every resource belongs to a
# resource group.
resource "azurerm_resource_group" "azure_vyos" {
name = "${var.resource_group}"
location = "${var.location}"
}
# The next resource is a Virtual Network.
resource "azurerm_virtual_network" "vnet" {
name = "${var.virtual_network_name}"
location = "${var.location}"
address_space = ["${var.address_space}"]
resource_group_name = "${var.resource_group}"
}
# Build a subnet to run your VMs.
resource "azurerm_subnet" "subnet" {
name = "${var.prefix}subnet"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
resource_group_name = "${var.resource_group}"
address_prefixes = ["${var.subnet_prefix}"]
}
##############################################################################
# Build a VyOS VM from the Marketplace.
# To find the necessary image, use the command:
#
# az vm image list --offer vyos --all
#
# Now that you have a network, you can deploy a VyOS server.
# An Azure Virtual Machine has several components. In this example,
# you build a security group, a network interface, a public IP
# address, a storage account, and finally the VM itself. Terraform
# handles all the dependencies automatically, and each resource is
# named with user-defined variables.
##############################################################################
# Security group to allow inbound access on port 22 (SSH)
resource "azurerm_network_security_group" "vyos-sg" {
name = "${var.prefix}-sg"
location = "${var.location}"
resource_group_name = "${var.resource_group}"
security_rule {
name = "SSH"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "${var.source_network}"
destination_address_prefix = "*"
}
}
# A network interface.
resource "azurerm_network_interface" "vyos-nic" {
name = "${var.prefix}vyos-nic"
location = "${var.location}"
resource_group_name = "${var.resource_group}"
ip_configuration {
name = "${var.prefix}ipconfig"
subnet_id = "${azurerm_subnet.subnet.id}"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${azurerm_public_ip.vyos-pip.id}"
}
}
# Add a public IP address.
resource "azurerm_public_ip" "vyos-pip" {
name = "${var.prefix}-ip"
location = "${var.location}"
resource_group_name = "${var.resource_group}"
allocation_method = "Dynamic"
}
# Build a virtual machine. This is a standard VyOS instance from
# Marketplace.
resource "azurerm_virtual_machine" "vyos" {
name = "${var.hostname}-vyos"
location = "${var.location}"
resource_group_name = "${var.resource_group}"
vm_size = "${var.vm_size}"
network_interface_ids = ["${azurerm_network_interface.vyos-nic.id}"]
delete_os_disk_on_termination = "true"
# To find information about the plan, use the command:
# az vm image list --offer vyos --all
plan {
publisher = "sentriumsl"
name = "vyos-1-3"
product = "vyos-1-2-lts-on-azure"
}
storage_image_reference {
publisher = "${var.image_publisher}"
offer = "${var.image_offer}"
sku = "${var.image_sku}"
version = "${var.image_version}"
}
storage_os_disk {
name = "${var.hostname}-osdisk"
managed_disk_type = "Standard_LRS"
caching = "ReadWrite"
create_option = "FromImage"
}
os_profile {
computer_name = "${var.hostname}"
admin_username = "${var.admin_username}"
admin_password = "${var.admin_password}"
}
os_profile_linux_config {
disable_password_authentication = false
}
}
data "azurerm_public_ip" "example" {
depends_on = ["azurerm_virtual_machine.vyos"]
name = "vyos-ip"
resource_group_name = "${var.resource_group}"
}
output "public_ip_address" {
value = data.azurerm_public_ip.example.ip_address
}
# IP of AZ instance copied to a file ip.txt in the local system.
resource "local_file" "ip" {
content = data.azurerm_public_ip.example.ip_address
filename = "ip.txt"
}
# Connect to the Ansible control node via SSH
resource "null_resource" "nullremote1" {
depends_on = ["azurerm_virtual_machine.vyos"]
connection {
type = "ssh"
user = "root"
password = var.password
host = var.host
}
# Copy the ip.txt file to the Ansible control node from the local
# system
provisioner "file" {
source = "ip.txt"
destination = "/root/az/ip.txt"
}
}
resource "null_resource" "nullremote2" {
depends_on = ["azurerm_virtual_machine.vyos"]
connection {
type = "ssh"
user = "root"
password = var.password
host = var.host
}
# Run the Ansible playbook on the remote Linux OS
provisioner "remote-exec" {
inline = [
"cd /root/az/",
"ansible-playbook instance.yml"
]
}
}
```
`var.tf`
```none
##############################################################################
# Variables File
#
# Default values for all variables used in Terraform code.
##############################################################################
variable "resource_group" {
description = "The name of your Azure Resource Group."
default = "my_resource_group"
}
variable "prefix" {
description = "This prefix will be included in the name of some resources."
default = "vyos"
}
variable "hostname" {
description = "Virtual machine hostname. Used for local hostname, DNS, and storage-related names."
default = "vyos_terraform"
}
variable "location" {
description = "The region where the virtual network is created."
default = "centralus"
}
variable "virtual_network_name" {
description = "The name for your virtual network."
default = "vnet"
}
variable "address_space" {
description = "The address space that is used by the virtual network. You can supply more than one address space. Changing this forces a new resource to be created."
default = "10.0.0.0/16"
}
variable "subnet_prefix" {
description = "The address prefix to use for the subnet."
default = "10.0.10.0/24"
}
variable "storage_account_tier" {
description = "Defines the storage tier. Valid options are Standard and Premium."
default = "Standard"
}
variable "storage_replication_type" {
description = "Defines the replication type to use for this storage account. Valid options include LRS, GRS etc."
default = "LRS"
}
# The most cost-effective size
variable "vm_size" {
description = "Specifies the size of the virtual machine."
default = "Standard_B1s"
}
variable "image_publisher" {
description = "Name of the publisher of the image (az vm image list)"
default = "sentriumsl"
}
variable "image_offer" {
description = "Name of the offer (az vm image list)"
default = "vyos-1-2-lts-on-azure"
}
variable "image_sku" {
description = "Image SKU to apply (az vm image list)"
default = "vyos-1-3"
}
variable "image_version" {
description = "Version of the image to apply (az vm image list)"
default = "1.3.3"
}
variable "admin_username" {
description = "Administrator user name"
default = "vyos"
}
variable "admin_password" {
description = "Administrator password"
type = string
sensitive = true
}
variable "source_network" {
description = "Allow access from this network prefix. Defaults to '*'."
default = "*"
}
variable "password" {
description = "pass for Ansible"
type = string
sensitive = true
}
variable "host"{
description = "IP of my Ansible"
}
```
`terraform.tfvars`
```none
password = "" # password for Ansible SSH
host = "" # IP of my Ansible
```
## Structure of files in Ansible for Azure
```none
.
├── group_vars
└── all
├── ansible.cfg
└── instance.yml
```
## File contents of Ansible for Azure
`ansible.cfg`
```none
[defaults]
inventory = /root/az/ip.txt
host_key_checking= False
remote_user=vyos
```
`instance.yml`
```none
##############################################################################
# About tasks:
# "Wait 300 seconds, but only start checking after 60 seconds" - Tries
# to make SSH connection every 60 seconds until 300 seconds.
# "Configure general settings for the VyOS hosts group" - Provision
# the Azure VyOS node.
# Add all necessary commands for VyOS under the block "lines:"
##############################################################################
- name: integration of terraform and ansible
hosts: all
gather_facts: 'no'
tasks:
- name: "Wait 300 seconds, but only start checking after 60 seconds"
wait_for_connection:
delay: 60
timeout: 300
- name: "Configure general settings for the VyOS hosts group"
vyos_config:
lines:
- set system name-server xxx.xxx.xxx.xxx
save:
true
```
`group_vars/all`
```none
ansible_connection: ansible.netcommon.network_cli
ansible_network_os: vyos.vyos.vyos
# user and password gets from terraform variables "admin_username" and "admin_password" in the file /root/azvyos/var.tf
ansible_user: vyos
ansible_ssh_pass: "{{ vault_vyos_ssh_pass }}"
```
## Source files on GitHub
All files related to deploying VyOS on Azure with Terraform and Ansible
can be found in the [vyos-automation] repository.
[vyos-automation]: <https://github.com/vyos/vyos-automation/tree/main/TerraformCloud/Azure_terraform_ansible_single_vyos_instance-main>
|