Passer au contenu principal

Terraform

Pour créer une machine automatiquement il suffit de ce connecter a notre XOCE en SSH sur une base RockyLinux 9  et d'installer Terraform

dnf install dnf-plugins-core -y
dnf config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
dnf install terraform -y

Une fois Terraform installer, on installe une machine RockyLinux 9 minimal. Une fois la VM installer on installe cloud-init

dnf install cloud-init

On éteint la VM et on la Converti en Template dans l'onglet Advanced. 

Sur la machine XOCE on créer un dossier Terraform et dedans HTTPD. On créer 2 fichier, un provider.tf et un vm.tf et on met dedans : 

#####Provider.tf#####
# Instruct terraform to download the provider on `terraform init`
terraform {
  required_providers {
    xenorchestra = {
      source = "terra-farm/xenorchestra"
      version = "0.23.3"
    }
  }
}

# Configure the XenServer Provider
provider "xenorchestra" {
  # Must be ws or wss
  url      = "ws://IP DE XOCE"
  username = "COMPTE XOCE"
  password = "MOT DE PASSE XOCE"
  insecure = true
}
####vm.tf####
data "xenorchestra_pool" "pool" {
  name_label = "NOM DU POOL XCP"
}

data "xenorchestra_template" "vm_template" {
  name_label = "NOM DE LA VM TEMPLATE CREER"
}

data "xenorchestra_sr" "sr" {
  name_label = "NOM DU STOCKAGE DES VM"
  pool_id = data.xenorchestra_pool.pool.id
}

data "xenorchestra_network" "network" {
  name_label = "NOM DE L'INTERFACE RESEAU"
  pool_id = data.xenorchestra_pool.pool.id
}

resource "xenorchestra_cloud_config" "demo" {
  name = "HTTPD + Wordpress"
  template = <<EOF
#cloud-config
fqdn: Wordpress
packages:
  - httpd
  - mariadb-server
  - php
  - php-fpm
  - php-cli
  - php-mysqlnd
  - php-zip
  - php-devel
  - php-gd
  - php-mbstring
  - php-curl
  - php-xml 
  - php-pear
  - php-bcmath
  - php-json
  - wget
  - unzip
runcmd:
 - sudo firewall-cmd --add-service=http --permanent
 - sudo firewall-cmd --add-service=https --permanent
 - sudo firewall-cmd --reload
 - sudo systemctl enable --now httpd mariadb php-fpm
 - wget https://fr.wordpress.org/latest-fr_FR.zip
 - unzip latest-fr_FR.zip -d /var/www/html
 - cp -R /var/www/html/wordpress/* /var/www/html/
 - rm -rf /var/www/html/wordpress
 - chmod -R apache:apache /var/www/html
 - sudo restorecon -Rv /var/www/html
 - sudo setsebool -P httpd_can_network_connect 1
 - sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html(/.*)?"
EOF
}

resource "xenorchestra_vm" "vm" {
  memory_max = 2147483648
  cpus = 1
  name_label = "HTTPD + Wordpress"
  template = data.xenorchestra_template.vm_template.id
  cloud_config = xenorchestra_cloud_config.demo.template

  network {
    network_id = data.xenorchestra_network.network.id
  }

  disk {
    sr_id = data.xenorchestra_sr.sr.id
    name_label = "wordpresshttpd"
    size = 32000
  }

    tags = [
      "RockyLinux",
    ]
}