-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm1.tf
144 lines (118 loc) · 5.5 KB
/
vm1.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Packer peut etre utilisé pour créer des template à partir de isos, etc. pour vsphere
# https://blog.stephane-robert.info/docs/virtualiser/outils/packer/
# https://docs.vmware.com/fr/VMware-vSphere/8.0/vsphere-vcenter-esxi-management/GUID-302A4F73-CA2D-49DC-8727-81052727A763.html
# https://developer.hashicorp.com/terraform
# https://developer.hashicorp.com/terraform/tutorials/virtual-machine/vsphere-provider
# https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs
# https://registry.terraform.io/providers/ansible/ansible/latest/docs
# https://dteslya.engineer/blog/2019/01/21/automate-windows-vm-creation-and-configuration-in-vsphere-using-packer-terraform-and-ansible-part-2-of-3/
# https://github.com/dteslya/win-iac-lab/tree/master
# https://www.digitalocean.com/community/tutorials/how-to-use-ansible-with-terraform-for-configuration-management
# https://developer.hashicorp.com/terraform/language/resources/provisioners/syntax
# https://developer.hashicorp.com/terraform/cli
# https://github.com/hashicorp/learn-terraform-vsphere
# https://github.com/PXLCloudAndAutomation/TerraformvSphereExamples
# https://garyflynn.com/post/create-your-first-vsphere-terraform-configuration/
# https://vmware-samples.github.io/packer-examples-for-vsphere/
# https://www.osboxes.org/vmware-images/
# https://github.com/89luca89/terrible
# https://developer.vmware.com/apis/vsphere-automation/latest/
# https://ilhicas.com/2019/08/17/Terraform-local-exec-run-always.html
# https://stackoverflow.com/questions/69185881/terraform-vsphere-new-template-re-creating-existing-vms
# https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs/resources/virtual_machine
# https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs/resources/virtual_machine.html#guest_id
# identification OS de la VM "guest_id": https://vdc-repo.vmware.com/vmwb-repository/dcr-public/184bb3ba-6fa8-4574-a767-d0c96e2a38f4/ba9422ef-405c-47dd-8553-e11b619185b2/SDK/vsphere-ws/docs/ReferenceGuide/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html
# OU : https://developer.vmware.com/apis/vsphere-automation/latest/vcenter/api/vcenter/vm/vm/get/
# version vSphere : 6.7
# en insérant vsphere comme provider, on lui fournit en réalité une API
# PREFERE L'UTILISATION DES SENSITIVES VARIABLES : soit avec export soit avec un ficher de variables externe
# https://developer.hashicorp.com/terraform/tutorials/configuration-language/sensitive-variables
# https://developer.hashicorp.com/terraform/tutorials/configuration-language/variables
# https://stackoverflow.com/questions/63829249/encrypt-environment-variable-and-call-it-in-the-terraform-code
# Les variables n'ont pas de valeurs car elles sont exportées dans le script du runner. Il faut juste les déclarées avec le type string
variable "vsphere_user" {
type = string
}
variable "vsphere_password" {
type = string
}
variable "vsphere_domain" {
type = string
}
provider "vsphere" {
user = var.vsphere_user
password = var.vsphere_password
vsphere_server = var.vsphere_domain
allow_unverified_ssl = true
}
data "vsphere_datacenter" "datacenter" {
name = "GAIA"
}
data "vsphere_compute_cluster" "cluster" {
name = "TESTBED"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_datastore" "datastore" {
name = "FREENAS_STORAGE_1_A"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_network" "network" {
name = "STAFF_VM"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
# Specifier le template
data "vsphere_virtual_machine" "template" {
name = "Templates/[TEMPLATE] Debian 11 Update 1 - Stage Ansible Loic" # En réalité : /<nom_datacenter>/dossier1/.../<nom_template>
datacenter_id = data.vsphere_datacenter.datacenter.id
}
# Creation VM
resource "vsphere_virtual_machine" "vm" {
name = "LOIC_VM"
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = 2
memory = 4096
guest_id = "debian9_64Guest" #API VMWARE : ATTENTION VERSION MAX 6.7 && doit être le même que le template (sinon => voir message erreur)
scsi_type = "pvscsi"
network_interface {
network_id = data.vsphere_network.network.id
adapter_type = "vmxnet3"
}
disk {
label = "disk0"
size = "60"
unit_number = 0
}
clone{
template_uuid = data.vsphere_virtual_machine.template.id
}
}
# https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource
# https://ilhicas.com/2019/08/17/Terraform-local-exec-run-always.html
# RUN Ansible playbook à chaque execution terraform
# COMMANDE : $ ansible-playbook ansible/playbooks/entrypoint.yml
# resource "null_resource" "run_playbook" {
# provisioner "local-exec" {
# command = "ansible-playbook ./ansible/playbooks/entrypoint.yml"
# }
# triggers = {
# always_run = "${timestamp()}" # LE TIMESTAMP PERMET D'EXECUTER AU MOMENT "T"
# }
# }
# https://fabianlee.org/2019/03/09/vmware-using-the-govc-cli-to-automate-vcenter-commands/
# https://github.com/vmware/govmomi?tab=readme-ov-file
# # vCenter host
# $ export GOVC_URL=myvcenter.name.com
# # vCenter credentials
# $ export GOVC_USERNAME=myuser
# $ export GOVC_PASSWORD=MyP4ss
# # disable cert validation
# $ export GOVC_INSECURE=true
# resource "null_resource" "power" {
# provisioner "local-exec" {
# command = "govc vm.power -on=true ${vsphere_virtual_machine.vm.name}"
# }
# triggers = {
# always_run = "${timestamp()}" # LE TIMESTAMP PERMET D'EXECUTER AU MOMENT "T"
# }
# }