Initial IaC deploy of RHEL 10 VMs using Terraform and Ansible

This commit is contained in:
Isshi0417 2026-07-02 21:20:55 +09:00
commit 7c988a641d
14 changed files with 735 additions and 0 deletions

View File

@ -0,0 +1,24 @@
{
"name": "ansible-dev-container-codespaces",
"image": "registry.redhat.io/ansible-automation-platform-25/ansible-dev-tools-rhel8:latest",
"containerUser": "root",
"runArgs": [
"--security-opt",
"seccomp=unconfined",
"--security-opt",
"label=disable",
"--cap-add=SYS_ADMIN",
"--cap-add=SYS_RESOURCE",
"--device",
"/dev/fuse",
"--security-opt",
"apparmor=unconfined",
"--hostname=ansible-dev-container"
],
"updateRemoteUserUID": true,
"customizations": {
"vscode": {
"extensions": ["redhat.ansible","redhat.vscode-redhat-account"]
}
}
}

View File

@ -0,0 +1,24 @@
{
"name": "ansible-dev-container-docker",
"image": "registry.redhat.io/ansible-automation-platform-25/ansible-dev-tools-rhel8:latest",
"containerUser": "root",
"runArgs": [
"--security-opt",
"seccomp=unconfined",
"--security-opt",
"label=disable",
"--cap-add=SYS_ADMIN",
"--cap-add=SYS_RESOURCE",
"--device",
"/dev/fuse",
"--security-opt",
"apparmor=unconfined",
"--hostname=ansible-dev-container"
],
"updateRemoteUserUID": true,
"customizations": {
"vscode": {
"extensions": ["redhat.ansible","redhat.vscode-redhat-account"]
}
}
}

View File

@ -0,0 +1,28 @@
{
"name": "ansible-dev-container-podman",
"image": "registry.redhat.io/ansible-automation-platform-25/ansible-dev-tools-rhel8:latest",
"containerUser": "root",
"runArgs": [
"--cap-add=CAP_MKNOD",
"--cap-add=NET_ADMIN",
"--cap-add=SYS_ADMIN",
"--cap-add=SYS_RESOURCE",
"--device",
"/dev/fuse",
"--security-opt",
"seccomp=unconfined",
"--security-opt",
"label=disable",
"--security-opt",
"apparmor=unconfined",
"--security-opt",
"unmask=/sys/fs/cgroup",
"--userns=host",
"--hostname=ansible-dev-container"
],
"customizations": {
"vscode": {
"extensions": ["redhat.ansible","redhat.vscode-redhat-account"]
}
}
}

6
ansible/ansible.cfg Normal file
View File

@ -0,0 +1,6 @@
[defaults]
inventory = inventory.ini
host_key_checking = False
remote_user = sho
private_key_file = ~/.ssh/id_ed25519
interpreter_python = auto_silent

View File

@ -0,0 +1,22 @@
---
- name: Configure Homelab RHEL 10 Nodes
hosts: all
become: true
tasks:
- name: Upgrade all system packages # noqa package-latest
ansible.builtin.dnf:
name: "*"
state: latest
update_cache: true
- name: Install common utility packages
ansible.builtin.dnf:
name:
- vim
- tmux
- git
- bash-completion
- wget
state: present

12
ansible/inventory.ini Normal file
View File

@ -0,0 +1,12 @@
[control]
ansible-control ansible_host=172.30.1.200
[web]
web-portfolio ansible_host=172.30.1.201
[media]
media-stream ansible_host=172.30.1.202
[all:vars]
ansible_user=ansible
ansible_ssh_private_key_file=~/.ssh/id_ed25519

15
ansible/register_rhel.yml Normal file
View File

@ -0,0 +1,15 @@
---
- name: Register homelab vms with Red Hat Subscription Manager
hosts: all
become: true
vars_files:
- vault.yml
tasks:
- name: Register system and auto-attach subscription
community.general.redhat_subscription:
state: present
username: "{{ vault_rh_username }}"
password: "{{ vault_rh_password }}"
auto_attach: true

8
ansible/test_ping.yml Normal file
View File

@ -0,0 +1,8 @@
---
- name: Test connection to homelab vms
hosts: all
gather_facts: false
tasks:
- name: Run ansible ping module
ansible.builtin.ping:

24
terraform/.terraform.lock.hcl generated Normal file
View File

@ -0,0 +1,24 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/dmacvicar/libvirt" {
version = "0.7.6"
constraints = "0.7.6"
hashes = [
"h1:mmbm4vTyC/DCGO4Ed/vbp5AKvy1gmVn/94fzB9VmR08=",
"zh:0bde54f6f658b20b620b875daf106b5b25b1bae4d15408d6c5f06d58360e254d",
"zh:0c97c6930015918b8a34b6d7a2b0c3d17a649c226fcd1874fcba5bbbc0f35972",
"zh:1bdd7aa0011c5f024a09a124836ee9bc8e71b05a6ece810c61824275fd3f695f",
"zh:2b0cc7c794e4caf395d84ffff0b380d17e4b3219a4696264271bfe5059450efe",
"zh:2f8633f7fe07f76c188836ed6f93321ec5fbf5c004bc7699e1741d9b21ed5f37",
"zh:5bf47eed286ce55ed10a5cf657de49a34ab21cc8677c56fef3aab69cdde41a27",
"zh:7dca790fc5fd1d42bc4bc7170be003a7093602026d0f95c8aab84ad551fdf2a4",
"zh:80476b68bc84e3d661d1390025f83879b88f9cdc836de9751af09bd5716089cb",
"zh:82f3e2f3f50176cd6041c8ba36e295cbda1b289ef52ab75b5eceb0f921f64f7b",
"zh:a179b165f3b9bb9a67ebbbf9d73157ded33f02d476b2f58906389dca03b653c9",
"zh:acae54a5d0616f22b3180ddd8e8aad39af664e604394fdacf1f7b337bca2d5b4",
"zh:da4406a2428a9a7e98272c032cb93431c3919253af2fe9934b532d26c0deab09",
"zh:f63dbd8e579ab5268d01ffab4503b8a8e736b70d1a04e4f271559ba8dd133dcd",
"zh:f85c1d9e51a94ecde137435c9d6b0fb7be590437ea8a725334d1577eebbc550c",
]
}

24
terraform/cloud_init.cfg Normal file
View File

@ -0,0 +1,24 @@
#cloud-config
hostname: ${hostname}
fqdn: ${hostname}.lab.local
users:
- name: sho
sudo: ALL=(ALL) NOPASSWD:ALL
groups: wheel
shell: /bin/bash
ssh_authorized_keys:
- ${ssh_key}
- name: ansible
sudo: ALL=(ALL) NOPASSWD:ALL
groups: wheel
shell: /bin/bash
ssh_authorized_keys:
- ${ssh_key}
ssh_pwauth: false
disable_root: true
runcmd:
- systemctl enable --now qemu-guest-agent

103
terraform/main.tf Normal file
View File

@ -0,0 +1,103 @@
terraform {
required_version = ">= 1.0"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.7.6"
}
}
}
provider "libvirt" {
uri = "qemu+ssh://sho@hypervisor.lab.local/system"
}
locals {
ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHwJzj/dCMOC+zJviqO32+/22kABZBdwC+NvyM+7+Vz sho@nobara"
vms = {
"ansible-control" = {
vcpu = 2
memory = 2048
ip = "172.30.1.200"
}
"web-portfolio" = {
vcpu = 2
memory = 2048
ip = "172.30.1.201"
}
"media-stream" = {
vcpu = 2
memory = 4096
ip = "172.30.1.202"
}
}
}
resource "libvirt_volume" "vm_disk" {
for_each = local.vms
name = "${each.key}.qcow"
pool = "images"
base_volume_name = "rhel-10-guest.qcow2"
size = 21474836480
format = "qcow2"
}
resource "libvirt_cloudinit_disk" "commoninit" {
for_each = local.vms
name = "commoninit-${each.key}.raw"
pool = "images"
user_data = templatefile("${path.module}/cloud_init.cfg", {
hostname = each.key
ssh_key = local.ssh_key
})
network_config = templatefile("${path.module}/network_config.cfg", {
ip_address = each.value.ip
})
}
resource "libvirt_domain" "rhel_vm" {
for_each = local.vms
name = each.key
memory = each.value.memory
vcpu = each.value.vcpu
cpu {
mode = "host-passthrough"
}
firmware = "/usr/share/edk2/ovmf/OVMF_CODE.fd"
nvram {
file = "/var/lib/libvirt/qemu/nvram/${each.key}_VARS.fd"
template = "/usr/share/edk2/ovmf/OVMF_VARS.fd"
}
qemu_agent = false
disk {
volume_id = libvirt_volume.vm_disk[each.key].id
}
disk {
file = "/var/lib/libvirt/images/commoninit-${each.key}.raw"
scsi = true
}
network_interface {
bridge = "br0"
}
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
graphics {
type = "vnc"
listen_type = "address"
autoport = true
}
}

View File

@ -0,0 +1,9 @@
version: 2
ethernets:
ens3:
dhcp4: false
addresses:
- ${ip_address}/24
gateway4: 172.30.1.254
nameservers:
addresses: [8.8.8.8, 1.1.1.1]

427
terraform/terraform.tfstate Normal file
View File

@ -0,0 +1,427 @@
{
"version": 4,
"terraform_version": "1.9.8",
"serial": 239,
"lineage": "7d31b4df-3f8e-b168-0c63-49ed8456c3e5",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "libvirt_cloudinit_disk",
"name": "commoninit",
"provider": "provider[\"registry.terraform.io/dmacvicar/libvirt\"]",
"instances": [
{
"index_key": "ansible-control",
"schema_version": 0,
"attributes": {
"id": "/var/lib/libvirt/images/commoninit-ansible-control.raw;88c95925-3a17-4db1-b0eb-8740ba3a2c46",
"meta_data": "",
"name": "commoninit-ansible-control.raw",
"network_config": "version: 2\nethernets:\n ens3:\n dhcp4: false\n addresses:\n - 172.30.1.200/24\n gateway4: 172.30.1.254\n nameservers:\n addresses: [8.8.8.8, 1.1.1.1]",
"pool": "images",
"user_data": "#cloud-config\nhostname: ansible-control\nfqdn: ansible-control.lab.local\n\nusers:\n - name: sho\n sudo: ALL=(ALL) NOPASSWD:ALL\n groups: wheel\n shell: /bin/bash\n ssh_authorized_keys:\n - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHwJzj/dCMOC+zJviqO32+/22kABZBdwC+NvyM+7+Vz sho@nobara\n\n - name: ansible\n sudo: ALL=(ALL) NOPASSWD:ALL\n groups: wheel\n shell: /bin/bash\n ssh_authorized_keys:\n - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHwJzj/dCMOC+zJviqO32+/22kABZBdwC+NvyM+7+Vz sho@nobara\n\nssh_pwauth: false\ndisable_root: true\n\nruncmd:\n - systemctl enable --now qemu-guest-agent"
},
"sensitive_attributes": [],
"private": "bnVsbA=="
},
{
"index_key": "media-stream",
"schema_version": 0,
"attributes": {
"id": "/var/lib/libvirt/images/commoninit-media-stream.raw;9cc2e96a-8546-4f3a-bc30-275817280712",
"meta_data": "",
"name": "commoninit-media-stream.raw",
"network_config": "version: 2\nethernets:\n ens3:\n dhcp4: false\n addresses:\n - 172.30.1.202/24\n gateway4: 172.30.1.254\n nameservers:\n addresses: [8.8.8.8, 1.1.1.1]",
"pool": "images",
"user_data": "#cloud-config\nhostname: media-stream\nfqdn: media-stream.lab.local\n\nusers:\n - name: sho\n sudo: ALL=(ALL) NOPASSWD:ALL\n groups: wheel\n shell: /bin/bash\n ssh_authorized_keys:\n - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHwJzj/dCMOC+zJviqO32+/22kABZBdwC+NvyM+7+Vz sho@nobara\n\n - name: ansible\n sudo: ALL=(ALL) NOPASSWD:ALL\n groups: wheel\n shell: /bin/bash\n ssh_authorized_keys:\n - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHwJzj/dCMOC+zJviqO32+/22kABZBdwC+NvyM+7+Vz sho@nobara\n\nssh_pwauth: false\ndisable_root: true\n\nruncmd:\n - systemctl enable --now qemu-guest-agent"
},
"sensitive_attributes": [],
"private": "bnVsbA=="
},
{
"index_key": "web-portfolio",
"schema_version": 0,
"attributes": {
"id": "/var/lib/libvirt/images/commoninit-web-portfolio.raw;c60b76f9-4107-4e1c-a753-913c08481785",
"meta_data": "",
"name": "commoninit-web-portfolio.raw",
"network_config": "version: 2\nethernets:\n ens3:\n dhcp4: false\n addresses:\n - 172.30.1.201/24\n gateway4: 172.30.1.254\n nameservers:\n addresses: [8.8.8.8, 1.1.1.1]",
"pool": "images",
"user_data": "#cloud-config\nhostname: web-portfolio\nfqdn: web-portfolio.lab.local\n\nusers:\n - name: sho\n sudo: ALL=(ALL) NOPASSWD:ALL\n groups: wheel\n shell: /bin/bash\n ssh_authorized_keys:\n - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHwJzj/dCMOC+zJviqO32+/22kABZBdwC+NvyM+7+Vz sho@nobara\n\n - name: ansible\n sudo: ALL=(ALL) NOPASSWD:ALL\n groups: wheel\n shell: /bin/bash\n ssh_authorized_keys:\n - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHwJzj/dCMOC+zJviqO32+/22kABZBdwC+NvyM+7+Vz sho@nobara\n\nssh_pwauth: false\ndisable_root: true\n\nruncmd:\n - systemctl enable --now qemu-guest-agent"
},
"sensitive_attributes": [],
"private": "bnVsbA=="
}
]
},
{
"mode": "managed",
"type": "libvirt_domain",
"name": "rhel_vm",
"provider": "provider[\"registry.terraform.io/dmacvicar/libvirt\"]",
"instances": [
{
"index_key": "ansible-control",
"schema_version": 0,
"attributes": {
"arch": "x86_64",
"autostart": false,
"boot_device": [],
"cloudinit": null,
"cmdline": null,
"console": [
{
"source_host": "127.0.0.1",
"source_path": "",
"source_service": "0",
"target_port": "0",
"target_type": "serial",
"type": "pty"
}
],
"coreos_ignition": null,
"cpu": [
{
"mode": "host-passthrough"
}
],
"description": "",
"disk": [
{
"block_device": "",
"file": "",
"scsi": false,
"url": "",
"volume_id": "/var/lib/libvirt/images/ansible-control.qcow",
"wwn": ""
},
{
"block_device": "",
"file": "",
"scsi": true,
"url": "",
"volume_id": "/var/lib/libvirt/images/commoninit-ansible-control.raw",
"wwn": "05abcd668288fc20"
}
],
"emulator": "/usr/libexec/qemu-kvm",
"filesystem": [],
"firmware": "/usr/share/edk2/ovmf/OVMF_CODE.fd",
"fw_cfg_name": "opt/com.coreos/config",
"graphics": [
{
"autoport": true,
"listen_address": "127.0.0.1",
"listen_type": "address",
"type": "vnc",
"websocket": 0
}
],
"id": "0ce76487-f402-4540-9253-c7f6eafe1acf",
"initrd": "",
"kernel": "",
"machine": "pc",
"memory": 2048,
"metadata": null,
"name": "ansible-control",
"network_interface": [
{
"addresses": [],
"bridge": "br0",
"hostname": "",
"mac": "52:54:00:56:6E:37",
"macvtap": "",
"network_id": "",
"network_name": "",
"passthrough": "",
"vepa": "",
"wait_for_lease": false
}
],
"nvram": [
{
"file": "/var/lib/libvirt/qemu/nvram/ansible-control_VARS.fd",
"template": "/usr/share/edk2/ovmf/OVMF_VARS.fd"
}
],
"qemu_agent": false,
"running": true,
"timeouts": null,
"tpm": [],
"type": "kvm",
"vcpu": 2,
"video": [],
"xml": []
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjozMDAwMDAwMDAwMDB9fQ==",
"dependencies": [
"libvirt_volume.vm_disk"
]
},
{
"index_key": "media-stream",
"schema_version": 0,
"attributes": {
"arch": "x86_64",
"autostart": false,
"boot_device": [],
"cloudinit": null,
"cmdline": null,
"console": [
{
"source_host": "127.0.0.1",
"source_path": "",
"source_service": "0",
"target_port": "0",
"target_type": "serial",
"type": "pty"
}
],
"coreos_ignition": null,
"cpu": [
{
"mode": "host-passthrough"
}
],
"description": "",
"disk": [
{
"block_device": "",
"file": "",
"scsi": false,
"url": "",
"volume_id": "/var/lib/libvirt/images/media-stream.qcow",
"wwn": ""
},
{
"block_device": "",
"file": "",
"scsi": true,
"url": "",
"volume_id": "/var/lib/libvirt/images/commoninit-media-stream.raw",
"wwn": "05abcd353bba6680"
}
],
"emulator": "/usr/libexec/qemu-kvm",
"filesystem": [],
"firmware": "/usr/share/edk2/ovmf/OVMF_CODE.fd",
"fw_cfg_name": "opt/com.coreos/config",
"graphics": [
{
"autoport": true,
"listen_address": "127.0.0.1",
"listen_type": "address",
"type": "vnc",
"websocket": 0
}
],
"id": "420b3e34-e1d8-41cf-84b9-a89aa511ae7a",
"initrd": "",
"kernel": "",
"machine": "pc",
"memory": 4096,
"metadata": null,
"name": "media-stream",
"network_interface": [
{
"addresses": [],
"bridge": "br0",
"hostname": "",
"mac": "52:54:00:5E:F1:51",
"macvtap": "",
"network_id": "",
"network_name": "",
"passthrough": "",
"vepa": "",
"wait_for_lease": false
}
],
"nvram": [
{
"file": "/var/lib/libvirt/qemu/nvram/media-stream_VARS.fd",
"template": "/usr/share/edk2/ovmf/OVMF_VARS.fd"
}
],
"qemu_agent": false,
"running": true,
"timeouts": null,
"tpm": [],
"type": "kvm",
"vcpu": 2,
"video": [],
"xml": []
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjozMDAwMDAwMDAwMDB9fQ==",
"dependencies": [
"libvirt_volume.vm_disk"
]
},
{
"index_key": "web-portfolio",
"schema_version": 0,
"attributes": {
"arch": "x86_64",
"autostart": false,
"boot_device": [],
"cloudinit": null,
"cmdline": null,
"console": [
{
"source_host": "127.0.0.1",
"source_path": "",
"source_service": "0",
"target_port": "0",
"target_type": "serial",
"type": "pty"
}
],
"coreos_ignition": null,
"cpu": [
{
"mode": "host-passthrough"
}
],
"description": "",
"disk": [
{
"block_device": "",
"file": "",
"scsi": false,
"url": "",
"volume_id": "/var/lib/libvirt/images/web-portfolio.qcow",
"wwn": ""
},
{
"block_device": "",
"file": "",
"scsi": true,
"url": "",
"volume_id": "/var/lib/libvirt/images/commoninit-web-portfolio.raw",
"wwn": "05abcd17e438f15a"
}
],
"emulator": "/usr/libexec/qemu-kvm",
"filesystem": [],
"firmware": "/usr/share/edk2/ovmf/OVMF_CODE.fd",
"fw_cfg_name": "opt/com.coreos/config",
"graphics": [
{
"autoport": true,
"listen_address": "127.0.0.1",
"listen_type": "address",
"type": "vnc",
"websocket": 0
}
],
"id": "fbf831e6-a246-44f6-a375-c257dfbb17a2",
"initrd": "",
"kernel": "",
"machine": "pc",
"memory": 2048,
"metadata": null,
"name": "web-portfolio",
"network_interface": [
{
"addresses": [],
"bridge": "br0",
"hostname": "",
"mac": "52:54:00:16:C8:A2",
"macvtap": "",
"network_id": "",
"network_name": "",
"passthrough": "",
"vepa": "",
"wait_for_lease": false
}
],
"nvram": [
{
"file": "/var/lib/libvirt/qemu/nvram/web-portfolio_VARS.fd",
"template": "/usr/share/edk2/ovmf/OVMF_VARS.fd"
}
],
"qemu_agent": false,
"running": true,
"timeouts": null,
"tpm": [],
"type": "kvm",
"vcpu": 2,
"video": [],
"xml": []
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjozMDAwMDAwMDAwMDB9fQ==",
"dependencies": [
"libvirt_volume.vm_disk"
]
}
]
},
{
"mode": "managed",
"type": "libvirt_volume",
"name": "vm_disk",
"provider": "provider[\"registry.terraform.io/dmacvicar/libvirt\"]",
"instances": [
{
"index_key": "ansible-control",
"schema_version": 0,
"attributes": {
"base_volume_id": null,
"base_volume_name": "rhel-10-guest.qcow2",
"base_volume_pool": null,
"format": "qcow2",
"id": "/var/lib/libvirt/images/ansible-control.qcow",
"name": "ansible-control.qcow",
"pool": "images",
"size": 21474836480,
"source": null,
"xml": []
},
"sensitive_attributes": [],
"private": "bnVsbA=="
},
{
"index_key": "media-stream",
"schema_version": 0,
"attributes": {
"base_volume_id": null,
"base_volume_name": "rhel-10-guest.qcow2",
"base_volume_pool": null,
"format": "qcow2",
"id": "/var/lib/libvirt/images/media-stream.qcow",
"name": "media-stream.qcow",
"pool": "images",
"size": 21474836480,
"source": null,
"xml": []
},
"sensitive_attributes": [],
"private": "bnVsbA=="
},
{
"index_key": "web-portfolio",
"schema_version": 0,
"attributes": {
"base_volume_id": null,
"base_volume_name": "rhel-10-guest.qcow2",
"base_volume_pool": null,
"format": "qcow2",
"id": "/var/lib/libvirt/images/web-portfolio.qcow",
"name": "web-portfolio.qcow",
"pool": "images",
"size": 21474836480,
"source": null,
"xml": []
},
"sensitive_attributes": [],
"private": "bnVsbA=="
}
]
}
],
"check_results": null
}

View File

@ -0,0 +1,9 @@
{
"version": 4,
"terraform_version": "1.9.8",
"serial": 229,
"lineage": "7d31b4df-3f8e-b168-0c63-49ed8456c3e5",
"outputs": {},
"resources": [],
"check_results": null
}