forked from Azure-Samples/ansible-playbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_create_existingvnet_deployjavaapp.yml
144 lines (129 loc) · 4.23 KB
/
vm_create_existingvnet_deployjavaapp.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
# Description
# ===========
# This plabyook covers below scenario:
# 1. create a VM with below conditions
# - private IP
# - vnet in another resource group
# 1. clone a java application, build it
# 1. install tomcat, deploy the java app to newly created VM
#
# Prequisite
# ===========
# - git on ansible host
# - maven on ansible host
- name: Create Azure VM
hosts: localhost
connection: local
gather_facts: true
vars:
resource_group_vm: "{{ resource_group_name }}"
resource_group_vnet: myVNetRg
vnet_name: myVentName
subnet_name: mysubNet
vm_name: myTesVM
location: eastus
repo_url: https://github.com/yungezz/helloworld.git
workspace: ~/src/helloworld
admin_username: azureuser
admin_password: Password@123
# roles:
# - azure.azure_preview_modules
tasks:
- name: Create a resource group for vnet
azure_rm_resourcegroup:
name: "{{ resource_group_vnet }}"
location: "{{ location }}"
- name: Create virtual network
azure_rm_virtualnetwork:
resource_group: "{{ resource_group_vnet }}"
name: "{{ vnet_name }}"
address_prefixes: "10.0.0.0/16"
- name: Add subnet
azure_rm_subnet:
resource_group: "{{ resource_group_vnet }}"
name: "{{ subnet_name }}"
address_prefix: "10.0.1.0/24"
virtual_network: "{{ vnet_name }}"
- name: Create a resource group for vm
azure_rm_resourcegroup:
name: "{{ resource_group_vm }}"
location: "{{ location }}"
- name: Create Network Security Group that allows SSH
azure_rm_securitygroup:
resource_group: "{{ resource_group_vm }}"
name: "{{ vm_name }}"
rules:
- name: SSH
protocol: Tcp
destination_port_range: 22
access: Allow
priority: 1001
direction: Inbound
direction: Inbound
- name: Tomcat
protocol: Tcp
destination_port_range: 8080
access: Allow
priority: 1002
direction: Inbound
- name: Create VM
azure_rm_virtualmachine:
resource_group: "{{ resource_group_vm }}"
name: "{{ vm_name }}"
vm_size: Standard_DS1_v2
admin_username: "{{ admin_username }}"
admin_password: "{{ admin_password }}"
virtual_network_resource_group: "{{ resource_group_vnet }}"
virtual_network_name: "{{ vnet_name }}"
subnet_name: "{{ subnet_name }}"
public_ip_allocation_method: Disabled
image:
offer: UbuntuServer
publisher: Canonical
sku: 16.04-LTS
version: latest
register: output
- name: add azure vm into host
add_host:
name: new_azure_vm
ansible_host: '{{ output.ansible_facts.azure_vm.properties.networkProfile.networkInterfaces[0].properties.ipConfigurations[0].properties.privateIPAddress }}'
ansible_connection: paramiko # not guaranteed to have sshpass...
ansible_user: "{{ admin_username }}"
ansible_password: "{{ admin_password }}"
ansible_host_key_checking: false
- name: wait for the WinRM port to come online
wait_for:
port: 22
host: '{{ output.ansible_facts.azure_vm.properties.networkProfile.networkInterfaces[0].properties.ipConfigurations[0].properties.privateIPAddress }}'
timeout: 600
- name: Git Clone
git:
repo: "{{ repo_url }}"
dest: "{{ workspace }}"
- name: build sample app
shell: mvn package chdir="{{ workspace }}"
- block:
- name: Update repositories cache and install "tomcat8" package
become: true
apt:
name: tomcat8
update_cache: yes
- name: Install "tomcat8-admin" package
become: true
apt:
name: tomcat8-admin
- name: copy app to azure vm
become: true
copy:
src: "{{ workspace }}/target/demo-0.0.1-SNAPSHOT.war"
dest: /var/lib/tomcat8/webapps/hello.war
force: yes
mode: 0755
- name: Start and enable Tomcat service
become: true
systemd:
name: tomcat8
state: started
enabled: true
daemon_reload: true
delegate_to: new_azure_vm