From 181e227a6b2a6e3960dec2d4368e2fc3601fa850 Mon Sep 17 00:00:00 2001 From: Nicolas MASSE Date: Sat, 3 Apr 2021 22:25:55 +0200 Subject: [PATCH] windows lab install --- windows/ansible/prepare.yaml | 11 +++++++ windows/ansible/requirements.yml | 3 ++ windows/main.tf | 51 ++++++++++++++++++++++++++++++++ windows/network.tf | 13 ++++++++ windows/packer/README.md | 46 ++++++++++++++++++++++++++++ windows/provider.tf | 3 ++ windows/templates/inventory | 18 +++++++++++ windows/terraform.tfvars | 0 windows/variables.tf | 40 +++++++++++++++++++++++++ windows/windows.tf | 32 ++++++++++++++++++++ 10 files changed, 217 insertions(+) create mode 100644 windows/ansible/prepare.yaml create mode 100644 windows/ansible/requirements.yml create mode 100644 windows/main.tf create mode 100644 windows/network.tf create mode 100644 windows/packer/README.md create mode 100644 windows/provider.tf create mode 100644 windows/templates/inventory create mode 100644 windows/terraform.tfvars create mode 100644 windows/variables.tf create mode 100644 windows/windows.tf diff --git a/windows/ansible/prepare.yaml b/windows/ansible/prepare.yaml new file mode 100644 index 0000000..29d0d9b --- /dev/null +++ b/windows/ansible/prepare.yaml @@ -0,0 +1,11 @@ +- name: Sample playbook + hosts: all + gather_facts: no + tasks: + - name: Wait for the WinRM port to open + wait_for: + port: '{{ ansible_port }}' + host: '{{ ansible_host }}' + delegate_to: localhost + + - win_ping: diff --git a/windows/ansible/requirements.yml b/windows/ansible/requirements.yml new file mode 100644 index 0000000..ad8bbab --- /dev/null +++ b/windows/ansible/requirements.yml @@ -0,0 +1,3 @@ +collections: [] +#- win.collection1 +#- win.collection2 diff --git a/windows/main.tf b/windows/main.tf new file mode 100644 index 0000000..badf724 --- /dev/null +++ b/windows/main.tf @@ -0,0 +1,51 @@ +terraform { + required_version = ">= 0.13" + required_providers { + libvirt = { + source = "dmacvicar/libvirt" + version = ">=0.6.3" + } + local = { + source = "hashicorp/local" + version = ">=2.0.0" + } + template = { + source = "hashicorp/template" + version = ">=2.2.0" + } + } +} + +locals { + windows_machines = { for i in libvirt_domain.win_machine : i.name => i.network_interface.0.addresses[0] } +} + +output "machines" { + value = local.windows_machines +} + +resource "local_file" "ansible-inventory" { + content = templatefile("${path.module}/templates/inventory", { windows_machines = local.windows_machines, network_domain = var.network_domain }) + filename = "ansible/inventory" + file_permission = "0644" + + provisioner "local-exec" { + working_dir = "${path.module}/ansible" + command = < hashicorp.repo <<"EOF" +[hashicorp] +name=Hashicorp Stable - $basearch +baseurl=https://rpm.releases.hashicorp.com/RHEL/8/$basearch/stable +enabled=1 +gpgcheck=1 +gpgkey=https://rpm.releases.hashicorp.com/gpg +EOF +sudo dnf config-manager --add-repo hashicorp.repo +sudo dnf -y install packer +``` + +Install Qemu / KVM. + +```sh +sudo dnf install qemu-kvm +``` + +## Build + +Fetch the Qemu Guest tools. + +```sh +curl -Lo virtio-win.iso https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso +``` + +```sh +sudo /usr/bin/packer build windows_10.json +``` + +Store the built image in the libvirt default pool. + +```sh +sudo cp windows_10-qemu/windows_10 /var/lib/libvirt/images/windows-10.qcow2 +``` diff --git a/windows/provider.tf b/windows/provider.tf new file mode 100644 index 0000000..21b377c --- /dev/null +++ b/windows/provider.tf @@ -0,0 +1,3 @@ +provider "libvirt" { + uri = "qemu:///system" +} \ No newline at end of file diff --git a/windows/templates/inventory b/windows/templates/inventory new file mode 100644 index 0000000..3d4c167 --- /dev/null +++ b/windows/templates/inventory @@ -0,0 +1,18 @@ +[windows] +%{for host, ip in windows_machines~} +${host}.${network_domain} ansible_host=${ip} +%{endfor~} + +[windows:vars] +ansible_user=vagrant +ansible_password=vagrant +ansible_connection=winrm +ansible_winrm_server_cert_validation=ignore + +# HTTP +ansible_winrm_scheme=http +ansible_port=5985 + +# HTTPS +#ansible_winrm_scheme=https +#ansible_port=5986 diff --git a/windows/terraform.tfvars b/windows/terraform.tfvars new file mode 100644 index 0000000..e69de29 diff --git a/windows/variables.tf b/windows/variables.tf new file mode 100644 index 0000000..c9be515 --- /dev/null +++ b/windows/variables.tf @@ -0,0 +1,40 @@ + +variable "windows_machine_count" { + type = number + default = 1 +} + +variable "pool_name" { + type = string + default = "default" +} + +variable "volume_format" { + type = string + default = "qcow2" +} + +variable "windows_hostname_format" { + type = string + default = "win-%02d" +} + +variable "windows_image" { + type = string + default = "windows-10" +} + +variable "network_name" { + type = string + default = "lab" +} + +variable "network_domain" { + type = string + default = "sample.lab" +} + +variable "network_ip_range" { + type = string + default = "10.10.0.0/24" +} diff --git a/windows/windows.tf b/windows/windows.tf new file mode 100644 index 0000000..236e4e3 --- /dev/null +++ b/windows/windows.tf @@ -0,0 +1,32 @@ +resource "libvirt_volume" "win_disk" { + name = "${format(var.windows_hostname_format, count.index + 1)}.${var.volume_format}" + count = var.windows_machine_count + format = var.volume_format + pool = var.pool_name + base_volume_name = "${var.windows_image}.${var.volume_format}" +} + +resource "libvirt_domain" "win_machine" { + count = var.windows_machine_count + name = format(var.windows_hostname_format, count.index + 1) + vcpu = "2" + memory = "2048" + + cpu = { + mode = "host-passthrough" + } + + disk { + volume_id = element(libvirt_volume.win_disk.*.id, count.index) + } + + network_interface { + network_id = libvirt_network.lab_net.id + hostname = format(var.windows_hostname_format, count.index + 1) + + # When creating the domain resource, wait until the network interface gets + # a DHCP lease from libvirt, so that the computed IP addresses will be + # available when the domain is up and the plan applied. + wait_for_lease = true + } +}