11 changed files with 237 additions and 0 deletions
@ -0,0 +1,14 @@ |
|||
# Lab of Centos Machines |
|||
|
|||
Fetch the latest CentOS Stream 8 cloud image. |
|||
|
|||
```sh |
|||
sudo curl -Lo /var/lib/libvirt/images/centos-stream-8.qcow2 http://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-GenericCloud-8-20201217.0.x86_64.qcow2 |
|||
``` |
|||
|
|||
Then, deploy the lab. |
|||
|
|||
```sh |
|||
terraform init |
|||
terraform apply |
|||
``` |
|||
@ -0,0 +1,20 @@ |
|||
- name: Sample playbook |
|||
hosts: all |
|||
become: yes |
|||
gather_facts: no |
|||
pre_tasks: |
|||
- name: Wait for all nodes to become ready |
|||
wait_for_connection: |
|||
delay: 5 |
|||
timeout: 60 |
|||
- name: Wait for cloud-init to finish |
|||
raw: test -f /var/lib/cloud/instance/boot-finished |
|||
retries: 12 |
|||
delay: 5 |
|||
register: result |
|||
until: result.rc == 0 |
|||
changed_when: false |
|||
- name: Gather facts |
|||
setup: |
|||
tasks: |
|||
- ping: |
|||
@ -0,0 +1,3 @@ |
|||
collections: [] |
|||
#- collection1 |
|||
#- collection2 |
|||
@ -0,0 +1,48 @@ |
|||
|
|||
resource "libvirt_cloudinit_disk" "centos_cloudinit" { |
|||
name = "centos-cloudinit.iso" |
|||
user_data = file("${path.module}/templates/cloud-init.cfg") |
|||
network_config = file("${path.module}/templates/network-config.cfg") |
|||
pool = var.pool_name |
|||
} |
|||
|
|||
resource "libvirt_volume" "centos_disk" { |
|||
name = "${format(var.centos_hostname_format, count.index + 1)}.${var.volume_format}" |
|||
count = var.centos_machine_count |
|||
format = var.volume_format |
|||
pool = var.pool_name |
|||
base_volume_name = "${var.centos_image}.${var.volume_format}" |
|||
} |
|||
|
|||
resource "libvirt_domain" "centos_machine" { |
|||
count = var.centos_machine_count |
|||
name = format(var.centos_hostname_format, count.index + 1) |
|||
vcpu = "1" |
|||
memory = "1024" |
|||
cloudinit = libvirt_cloudinit_disk.centos_cloudinit.id |
|||
autostart = true |
|||
|
|||
disk { |
|||
volume_id = element(libvirt_volume.centos_disk.*.id, count.index) |
|||
} |
|||
|
|||
# Makes the tty0 available via `virsh console` |
|||
console { |
|||
type = "pty" |
|||
target_port = "0" |
|||
} |
|||
|
|||
network_interface { |
|||
network_id = libvirt_network.lab_net.id |
|||
hostname = format(var.centos_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 |
|||
} |
|||
} |
|||
|
|||
locals { |
|||
centos_machines = { for i in libvirt_domain.centos_machine : i.name => i.network_interface.0.addresses[0] } |
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
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" |
|||
} |
|||
} |
|||
} |
|||
|
|||
output "machines" { |
|||
value = local.centos_machines |
|||
} |
|||
|
|||
resource "local_file" "ansible_inventory" { |
|||
content = templatefile("${path.module}/templates/inventory", { centos_machines = local.centos_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,3 @@ |
|||
provider "libvirt" { |
|||
uri = "qemu:///system" |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
#cloud-config |
|||
# vim: syntax=yaml |
|||
|
|||
users: |
|||
- name: nicolas |
|||
gecos: Nicolas MASSE |
|||
groups: wheel |
|||
lock_passwd: false |
|||
# Generate encrypted password with "openssl passwd -6" |
|||
#passwd: $6$abc...xyz.0 |
|||
ssh_authorized_keys: |
|||
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPR1tt58X0+vbvsCR12gMAqr+g7vjt1Fx/qqz9EiboIs nicolas.masse@itix.fr |
|||
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFW62WJXI1ZCMfNA4w0dMpL0fsldhbEfULNGIUB0nQui nmasse@redhat.com |
|||
|
|||
packages: |
|||
- net-tools |
|||
- hdparm |
|||
- iptraf |
|||
- iotop |
|||
- vim-enhanced |
|||
- tmux |
|||
- rsync |
|||
- tree |
|||
- unzip |
|||
- tar |
|||
- tcpdump |
|||
- telnet |
|||
- strace |
|||
- bind-utils |
|||
|
|||
runcmd: |
|||
# Enable KVM virsh console access |
|||
- [ "systemctl", "enable", "serial-getty@ttyS0.service" ] |
|||
- [ "systemctl", "start", "--no-block", "serial-getty@ttyS0.service" ] |
|||
- [ "sed", "-i.post-install", "-e", "s/PasswordAuthentication yes/PasswordAuthentication no/", "/etc/ssh/sshd_config" ] |
|||
- [ "systemctl", "restart", "sshd" ] |
|||
- [ "sed", "-i.post-install", "-e", "s/^%wheel\tALL=(ALL)\tALL/%wheel ALL=(ALL) NOPASSWD: ALL/", "/etc/sudoers" ] |
|||
@ -0,0 +1,8 @@ |
|||
[centos] |
|||
%{for host, ip in centos_machines~} |
|||
${host}.${network_domain} ansible_host=${ip} |
|||
%{endfor~} |
|||
|
|||
[centos:vars] |
|||
ansible_user=nicolas |
|||
ansible_ssh_extra_args='-o StrictHostKeyChecking=no' |
|||
@ -0,0 +1,4 @@ |
|||
version: 2 |
|||
ethernets: |
|||
eth0: |
|||
dhcp4: true |
|||
@ -0,0 +1,40 @@ |
|||
|
|||
variable "centos_machine_count" { |
|||
type = number |
|||
default = 1 |
|||
} |
|||
|
|||
variable "pool_name" { |
|||
type = string |
|||
default = "default" |
|||
} |
|||
|
|||
variable "volume_format" { |
|||
type = string |
|||
default = "qcow2" |
|||
} |
|||
|
|||
variable "centos_hostname_format" { |
|||
type = string |
|||
default = "centos-%02d" |
|||
} |
|||
|
|||
variable "centos_image" { |
|||
type = string |
|||
default = "centos-stream-8" |
|||
} |
|||
|
|||
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" |
|||
} |
|||
Loading…
Reference in new issue