-
Notifications
You must be signed in to change notification settings - Fork 349
/
vnet_peering.yml
85 lines (82 loc) · 2.85 KB
/
vnet_peering.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
# Description
# ===========
# This playbook creates 2 virtual networks, then connect them with virtual netwok peering.
# This playbook requires ansible >= 2.8, or role azure.azure_preview_modules.
---
- hosts: localhost
tasks:
- name: Prepare random postfix
set_fact:
rpfx: "{{ 1000 | random }}"
run_once: yes
- name: Connect virtual networks with virtual network peering
hosts: localhost
connection: local
vars:
resource_group: "{{ resource_group_name }}"
resource_group_secondary: "{{ resource_group_name }}2"
vnet_name1: "myVnet{{ rpfx }}"
vnet_name2: "myVnet{{ rpfx }}2"
peering_name: peer1
location: eastus2
# Uncomment role reference if you're using ansible version < 2.8.
# At same time, install the role by ansible-galaxy install azure.azure_preview_modules
# roles:
# - azure.azure_preview_modules
tasks:
- name: Create a resource group
azure_rm_resourcegroup:
name: "{{ resource_group }}"
location: "{{ location }}"
- name: Create secondary resource group
azure_rm_resourcegroup:
name: "{{ resource_group_secondary }}"
location: "{{ location }}"
- name: Create first virtual network
azure_rm_virtualnetwork:
resource_group: "{{ resource_group }}"
name: "{{ vnet_name1 }}"
address_prefixes: "10.0.0.0/16"
- name: Add subnet
azure_rm_subnet:
resource_group: "{{ resource_group }}"
name: "{{ vnet_name1 }}"
address_prefix: "10.0.0.0/24"
virtual_network: "{{ vnet_name1 }}"
- name: Create second virtual network
azure_rm_virtualnetwork:
resource_group: "{{ resource_group_secondary }}"
name: "{{ vnet_name2 }}"
address_prefixes: "10.1.0.0/16"
- name: Add subnet
azure_rm_subnet:
resource_group: "{{ resource_group_secondary }}"
name: "{{ vnet_name2 }}"
address_prefix: "10.1.0.0/24"
virtual_network: "{{ vnet_name2 }}"
- name: Initial vnet peering
azure_rm_virtualnetworkpeering:
resource_group: "{{ resource_group }}"
name: "{{ peering_name }}"
virtual_network: "{{ vnet_name1 }}"
remote_virtual_network:
resource_group: "{{ resource_group_secondary }}"
name: "{{ vnet_name2 }}"
allow_virtual_network_access: true
allow_forwarded_traffic: true
- name: Connect vnet peering
azure_rm_virtualnetworkpeering:
resource_group: "{{ resource_group_secondary }}"
name: "{{ peering_name }}"
virtual_network: "{{ vnet_name2 }}"
remote_virtual_network:
resource_group: "{{ resource_group }}"
name: "{{ vnet_name1 }}"
allow_virtual_network_access: true
allow_forwarded_traffic: true
- name: Delete vnet peering
azure_rm_virtualnetworkpeering:
resource_group: "{{ resource_group }}"
name: "{{ peering_name }}"
virtual_network: "{{ vnet_name1 }}"
state: absent