Browse Source

centos node

main
Nicolas Massé 5 years ago
parent
commit
ba055fa09e
  1. 14
      centos/README.md
  2. 20
      centos/ansible/prepare.yaml
  3. 3
      centos/ansible/requirements.yml
  4. 48
      centos/centos.tf
  5. 47
      centos/main.tf
  6. 13
      centos/network.tf
  7. 3
      centos/provider.tf
  8. 37
      centos/templates/cloud-init.cfg
  9. 8
      centos/templates/inventory
  10. 4
      centos/templates/network-config.cfg
  11. 40
      centos/variables.tf

14
centos/README.md

@ -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
```

20
centos/ansible/prepare.yaml

@ -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:

3
centos/ansible/requirements.yml

@ -0,0 +1,3 @@
collections: []
#- collection1
#- collection2

48
centos/centos.tf

@ -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] }
}

47
centos/main.tf

@ -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
}
}

13
centos/network.tf

@ -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
}
}

3
centos/provider.tf

@ -0,0 +1,3 @@
provider "libvirt" {
uri = "qemu:///system"
}

37
centos/templates/cloud-init.cfg

@ -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" ]

8
centos/templates/inventory

@ -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'

4
centos/templates/network-config.cfg

@ -0,0 +1,4 @@
version: 2
ethernets:
eth0:
dhcp4: true

40
centos/variables.tf

@ -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…
Cancel
Save