add instructions to add a module to terraform
This commit is contained in:
68
README.md
68
README.md
@@ -28,7 +28,6 @@ cp terraform.tfvars.example
|
|||||||
```
|
```
|
||||||
fill with your secrets (do no push this file)
|
fill with your secrets (do no push this file)
|
||||||
|
|
||||||
|
|
||||||
## Usefull commands
|
## Usefull commands
|
||||||
```sh
|
```sh
|
||||||
opentofu.tofu init
|
opentofu.tofu init
|
||||||
@@ -51,11 +50,17 @@ ssh-add ~/.ssh/id_ed25519
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Add new service
|
## Add new service
|
||||||
### Create base module
|
### Create backup folder on proxmox host
|
||||||
|
```sh
|
||||||
|
mkdir /main/backups/<service-name>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Create a module
|
||||||
```sh
|
```sh
|
||||||
mkdir modules/apps/<module-hostname>
|
mkdir modules/apps/<module-hostname>
|
||||||
```
|
```
|
||||||
Example
|
|
||||||
|
Example tree
|
||||||
```sh
|
```sh
|
||||||
modules/apps/bookshelf/
|
modules/apps/bookshelf/
|
||||||
├── cloud-init
|
├── cloud-init
|
||||||
@@ -71,7 +76,7 @@ modules/apps/bookshelf/
|
|||||||
└── .env
|
└── .env
|
||||||
```
|
```
|
||||||
|
|
||||||
#### main.tf
|
#### `modules/apps/<service-name>/main.tf`
|
||||||
|
|
||||||
```hcl
|
```hcl
|
||||||
module "vm" {
|
module "vm" {
|
||||||
@@ -98,37 +103,45 @@ module "vm" {
|
|||||||
domain = var.domain
|
domain = var.domain
|
||||||
ssh_key = var.ssh_public_key
|
ssh_key = var.ssh_public_key
|
||||||
proxmox_host_ip = var.proxmox_host_ip
|
proxmox_host_ip = var.proxmox_host_ip
|
||||||
environment-setup-script = indent(6, file("${path.module}/lib/scripts/env.sh"))
|
environment-setup-script = indent(6, file("${path.module}/../common/scripts/env.sh"))
|
||||||
env-file-content = indent(6, file("${path.module}/.env"))
|
env-file-content = indent(6, file("${path.module}/.env"))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Add inside templatefile object scripts content to upload with `cloud-init`.
|
Add inside `templatefile()` object scripts content to upload with `cloud-init` :
|
||||||
|
- Backups scripts
|
||||||
#### variables.tf
|
- Backups services
|
||||||
|
- Install scripts
|
||||||
|
- Application services
|
||||||
|
|
||||||
|
#### `modules/apps/<service-name>/variables.tf`
|
||||||
```hcl
|
```hcl
|
||||||
variable "name" {
|
variable "name" {
|
||||||
|
description = "Virtual Machine name"
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "vm_id" {
|
variable "vm_id" {
|
||||||
|
description = "Virtual Machine id"
|
||||||
type = number
|
type = number
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "node_name" {
|
variable "node_name" {
|
||||||
|
description = "Proxmox node name"
|
||||||
type = string
|
type = string
|
||||||
default = "mop"
|
default = "mop"
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "cores" {
|
variable "cores" {
|
||||||
|
description = "Number of CPU cores for this virtual machine"
|
||||||
type = number
|
type = number
|
||||||
default = 2
|
default = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "memory" {
|
variable "memory" {
|
||||||
|
description = "Memory RAM for this virtual machine"
|
||||||
type = number
|
type = number
|
||||||
default = 2048
|
default = 2048
|
||||||
}
|
}
|
||||||
@@ -140,41 +153,45 @@ variable "balloon" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
variable "template_id" {
|
variable "template_id" {
|
||||||
|
description = "Virtual machine template ID"
|
||||||
type = number
|
type = number
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "ssh_public_key" {
|
variable "ssh_public_key" {
|
||||||
type = string
|
|
||||||
description = "Public SSH key for cloud-init user"
|
description = "Public SSH key for cloud-init user"
|
||||||
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "hostname" {
|
variable "hostname" {
|
||||||
description = "VM hostname"
|
description = "Virtual Machine hostname"
|
||||||
type = string
|
type = string
|
||||||
default = "test"
|
default = "test"
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "domain" {
|
variable "domain" {
|
||||||
description = "VM domain"
|
description = "Virtual Machine domain"
|
||||||
type = string
|
type = string
|
||||||
default = ""
|
default = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "disk_size" {
|
variable "disk_size" {
|
||||||
|
description = "Disk size for the virtual machine"
|
||||||
type = number
|
type = number
|
||||||
default = 10
|
default = 10
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "proxmox_host_ip" {
|
variable "proxmox_host_ip" {
|
||||||
|
description = "Proxmox host base ip"
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "vm_ip_address" {
|
variable "vm_ip_address" {
|
||||||
|
description = "Virtual machine ip"
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### output.tf
|
#### `modules/apps/<service-name>/output.tf`
|
||||||
|
|
||||||
```hcl
|
```hcl
|
||||||
output "traefik_service" {
|
output "traefik_service" {
|
||||||
@@ -187,11 +204,10 @@ output "traefik_service" {
|
|||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
This `traefik_serive` variable `output.tf` supports multiple service for one VM.
|
||||||
|
|
||||||
This output supports multiple service for one vm.
|
#### `cloud-init/service.yaml`
|
||||||
|
##### Base users, groups and ssh-key
|
||||||
#### cloud-init/service.yaml
|
|
||||||
|
|
||||||
```hcl
|
```hcl
|
||||||
#cloud-config
|
#cloud-config
|
||||||
hostname: ${hostname}
|
hostname: ${hostname}
|
||||||
@@ -212,27 +228,29 @@ disable_root: true
|
|||||||
|
|
||||||
package_update: true
|
package_update: true
|
||||||
package_upgrade: false
|
package_upgrade: false
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Backup setup
|
||||||
|
```hcl
|
||||||
packages:
|
packages:
|
||||||
- git
|
|
||||||
- nfs-common
|
- nfs-common
|
||||||
|
|
||||||
mounts:
|
mounts:
|
||||||
- [ "192.168.1.12:/main/backups", "/backups", "nfs", "defaults,_netdev,x-systemd.requires=network-online.target", "0", "0" ]
|
- [ "192.168.1.12:/main/backups", "/backups", "nfs", "defaults,_netdev,x-systemd.requires=network-online.target", "0", "0" ]
|
||||||
|
```
|
||||||
|
|
||||||
|
`nfs-common`: NFS mount package for `/main/backups` mount point.
|
||||||
|
`mounts`: adds NFS mount point to `/etc/fstab` file.
|
||||||
|
|
||||||
|
##### Environment variables for scripts
|
||||||
|
```hcl
|
||||||
write_files:
|
write_files:
|
||||||
- path: /opt/bookshelf/env.sh
|
- path: /opt/<service-name>/env.sh
|
||||||
permissions: "0644"
|
permissions: "0644"
|
||||||
content: |
|
content: |
|
||||||
${environment-setup-script}
|
${environment-setup-script}
|
||||||
- path: /opt/bookshelf/bookshelf.env
|
- path: /opt/<service-name>/<service-name>.env
|
||||||
permissions: "0644"
|
permissions: "0644"
|
||||||
content: |
|
content: |
|
||||||
${env-file-content}
|
${env-file-content}
|
||||||
|
|
||||||
runcmd:
|
|
||||||
- ls /
|
|
||||||
|
|
||||||
final_message: |
|
|
||||||
Base system ready for ${hostname}
|
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user