10 changed files with 217 additions and 0 deletions
@ -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: |
|||
@ -0,0 +1,3 @@ |
|||
collections: [] |
|||
#- win.collection1 |
|||
#- win.collection2 |
|||
@ -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 = <<EOT |
|||
set -e |
|||
ansible-galaxy install -r requirements.yml |
|||
ansible-playbook -i inventory prepare.yaml -e tf_action=start |
|||
EOT |
|||
|
|||
} |
|||
|
|||
provisioner "local-exec" { |
|||
working_dir = "${path.module}/ansible" |
|||
when = destroy |
|||
command = <<EOT |
|||
set -e |
|||
ansible-playbook -i inventory prepare.yaml -e tf_action=stop |
|||
EOT |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
resource "libvirt_network" "lab_net" { |
|||
name = var.network_name |
|||
mode = "nat" |
|||
domain = var.network_domain |
|||
addresses = [var.network_ip_range] |
|||
autostart = true |
|||
dns { |
|||
enabled = true |
|||
} |
|||
dhcp { |
|||
enabled = true |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
# Windows 10 unattended install with packer |
|||
|
|||
## Prerequisites |
|||
|
|||
* CentOS Stream 8 |
|||
|
|||
## Installation |
|||
|
|||
Install packer. |
|||
|
|||
```sh |
|||
cat > 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 |
|||
``` |
|||
@ -0,0 +1,3 @@ |
|||
provider "libvirt" { |
|||
uri = "qemu:///system" |
|||
} |
|||
@ -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 |
|||
@ -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" |
|||
} |
|||
@ -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 |
|||
} |
|||
} |
|||
Loading…
Reference in new issue