-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
129 lines (110 loc) · 3.73 KB
/
main.tf
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
terraform {
# required_version = ">= 0.12"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
}
}
}
##- Hypervisor
provider "libvirt" {
uri = "qemu:///system"
}
## exemple de definicio de recursos i dades parametritzat per <nom_projecte> <nom_usuari> <comptador>
## remot
#provider "libvirt" {
# alias = "server2"
# uri = "qemu+ssh://root@192.168.100.10/system"
#}
data "template_file" "user_data" {
template = "${file("${path.module}/cloud_init.cfg")}"
vars = {
hostname = var.nom_host
fqdn = "${var.nom_host}.${var.nom_domini}"
}
}
##- tot i que no tinga interfícies estàtiques ho introduim per homogeneitzar
## -> abans < ## data "template_file" "meta_data" {
data "template_file" "network_config" {
template = "${file("${path.module}/network_config.cfg")}"
}
##- Storage -----------------------------------------------------
# Use CloudInit to add the instance
resource "libvirt_cloudinit_disk" "cloud_init" {
name = "tf-${var.nom_host}-cloudinit.iso"
# user_data = "${data.template_file.user_data.rendered}"
user_data = data.template_file.user_data.rendered
network_config = data.template_file.network_config.rendered
}
resource "libvirt_volume" "centos7_qcow2" {
name = "tf-${var.nom_host}-${var.account}-${count.index + 1}.qcow2"
pool = "default"
format = "qcow2"
##- sempre actualitzat
#source = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2"
##- local al compte (possibles duplicats: malbaratament d'espai)
#source = "../terraform2/CentOS-7-x86_64-GenericCloud.qcow2"
##- centralitzat (requereix que un compte administratiu la situe allí)
#source = "/opt/terraform/images/CentOS-7-x86_64-GenericCloud.qcow2"
##- linked clone!!!! estalvia espai - possible perill de que desaparega la base
##- genial si se disposa de 4 bases: ubuntu/centos x server/desktop
##> alternatiu < base_volume_name = "centos7.qcow2"
##base_volume_name = "centos7.qcow2"
base_volume_name = "CentOS-7-x86_64-GenericCloud.qcow2"
base_volume_pool = "default"
##- ready for multi-instancies
count = var.nombre_instancies
}
##- networking - pendent-------------------------------------------------
##- https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/examples/v0.12/format/libvirt.tf
#
##- si els requeriments de xarxa requereixen de configuracions complexes
## (i.e. rangs concrets, reserva de hosts, tftp/pxe) aleshores millor definir
## la xarxa fora i comentar aquesta
##
resource "libvirt_network" "net" {
name = "tf-${var.nom_host}-${var.account}"
domain = var.nom_domini
mode = "nat"
addresses = ["10.0.100.0/24"]
}
##- compute -------------------------------------------------------------
# Define KVM domain to create
resource "libvirt_domain" "dom" {
name = "tf-${var.nom_host}-${var.account}-${count.index + 1}"
count = var.nombre_instancies
memory = "1024"
vcpu = 1
network_interface {
## network_name = "default"
network_name = "tf-${var.nom_host}-${var.account}"
mac = "00:11:22:33:88:${count.index}"
## no engega :( wait_for_lease = true
}
boot_device {
###- si cal arrancar per xarxa...
# dev = ["network","hd"]
dev = ["hd","network"]
}
disk {
volume_id = element(libvirt_volume.centos7_qcow2.*.id, count.index)
}
cloudinit = libvirt_cloudinit_disk.cloud_init.id
console {
type = "pty"
target_type = "serial"
target_port = "0"
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
}
# Output Server IP
output "ip" {
value = libvirt_domain.dom.*.network_interface.0.addresses[0]
#### nope - value = libvirt_domain.dom[count.index].network_interface.0.addresses[0]
#value = libvirt_domain.dom
#-- value = "${libvirt_domain.dom}"
}