terraform-provider-proxmox/README.md

224 lines
5.4 KiB
Markdown
Raw Normal View History

2019-05-21 04:51:59 +00:00
[![Build Status](https://travis-ci.com/Telmate/terraform-provider-proxmox.svg?branch=master)](https://travis-ci.com/Telmate/terraform-provider-proxmox)
2018-07-29 01:39:11 +00:00
# Proxmox 4 Terraform
2017-02-09 04:53:24 +00:00
Terraform provider plugin for proxmox
2017-02-09 21:36:31 +00:00
## Working prototype
2017-02-09 21:36:31 +00:00
2017-02-09 04:53:24 +00:00
2019-01-08 00:03:09 +00:00
## Go Install
2017-02-09 21:39:14 +00:00
2017-02-09 04:53:24 +00:00
```
2019-01-08 00:03:09 +00:00
go install github.com/Telmate/terraform-provider-proxmox/cmd/terraform-provider-proxmox
go install github.com/Telmate/terraform-provider-proxmox/cmd/terraform-provisioner-proxmox
2017-02-09 04:53:24 +00:00
```
2019-01-08 00:03:09 +00:00
Note: this plugin is both a provider and provisioner in one, which is why it needs two install commands.
2017-02-09 04:53:24 +00:00
2019-01-08 00:03:09 +00:00
## Build local source
2019-01-08 00:03:09 +00:00
Requires https://github.com/Telmate/proxmox-api-go
2019-01-08 00:03:09 +00:00
```
export GOPATH=`pwd`
make setup
2019-01-08 00:03:09 +00:00
make
2019-01-08 21:28:43 +00:00
make install
2019-01-08 00:03:09 +00:00
```
Recommended ISO builder https://github.com/Telmate/terraform-ubuntu-proxmox-iso
## Credentials
```bash
# Credentials and URL optionally defined in the environment
export PM_API_URL="https://xxxx.com:8006/api2/json"
export PM_USER=user@pam
export PM_PASS=password
```
2019-08-02 12:01:52 +00:00
If a 2FA OTP code is required
```bash
# Optional 2FA OTP code
export PM_OTP=otpcode
```
2017-02-09 21:36:31 +00:00
## Run
```
terraform init
terraform plan
2017-02-09 21:36:31 +00:00
terraform apply
```
### Sample file
main.tf:
```
provider "proxmox" {
pm_tls_insecure = true
/*
// Credentials here or environment
pm_api_url = "https://proxmox-server01.example.com:8006/api2/json"
pm_password = "secret"
pm_user = "terraform-user@pve"
2019-08-02 12:01:52 +00:00
//Optional
pm_otp = "otpcode"
*/
2017-02-09 21:36:31 +00:00
}
2018-07-13 17:25:37 +00:00
/* Uses cloud-init options from Proxmox 5.2 */
resource "proxmox_vm_qemu" "cloudinit-test" {
name = "tftest1.xyz.com"
desc = "tf description"
target_node = "proxmox1-xx"
clone = "ci-ubuntu-template"
# The destination resource pool for the new VM
pool = "pool0"
storage = "local"
cores = 3
sockets = 1
memory = 2560
disk_gb = 4
nic = "virtio"
bridge = "vmbr0"
ssh_user = "root"
ssh_private_key = <<EOF
2018-07-13 17:25:37 +00:00
-----BEGIN RSA PRIVATE KEY-----
private ssh key root
-----END RSA PRIVATE KEY-----
EOF
os_type = "cloud-init"
ipconfig0 = "ip=10.0.2.99/16,gw=10.0.2.2"
2018-07-13 17:25:37 +00:00
sshkeys = <<EOF
2018-07-13 17:25:37 +00:00
ssh-rsa AAAAB3NzaC1kj...key1
ssh-rsa AAAAB3NzaC1kj...key2
EOF
provisioner "remote-exec" {
inline = [
"ip a"
]
}
2018-07-13 17:25:37 +00:00
}
/* Uses custom eth1 user-net SSH portforward */
resource "proxmox_vm_qemu" "prepprovision-test" {
name = "tftest1.xyz.com"
desc = "tf description"
target_node = "proxmox1-xx"
clone = "terraform-ubuntu1404-template"
# The destination resource pool for the new VM
pool = "pool0"
cores = 3
sockets = 1
memory = 2560
2019-08-05 09:00:26 +00:00
scsihw = "lsi"
network {
id = 0
model = "virtio"
}
network {
id = 1
model = "virtio"
bridge = "vmbr1"
}
disk {
id = 0
type = virtio
storage = local-lvm
storage_type = lvm
size = 4G
backup = true
}
preprovision = true
ssh_forward_ip = "10.0.0.1"
ssh_user = "terraform"
ssh_private_key = <<EOF
2017-02-11 00:06:14 +00:00
-----BEGIN RSA PRIVATE KEY-----
2017-08-01 23:00:55 +00:00
private ssh key terraform
2017-02-11 00:06:14 +00:00
-----END RSA PRIVATE KEY-----
EOF
2017-08-01 23:00:55 +00:00
os_type = "ubuntu"
os_network_config = <<EOF
auto eth0
iface eth0 inet dhcp
EOF
provisioner "remote-exec" {
inline = [
"ip a"
]
}
provisioner "proxmox" {
action = "sshbackward"
}
2017-02-09 21:36:31 +00:00
}
```
2017-08-01 23:00:55 +00:00
### Provider usage
You can start from either an ISO or clone an existing VM.
Optimally, you could create a VM resource you will use a clone base with an ISO, and make the rest of the VM resources depend on that base "template" and clone it.
Interesting parameters:
2018-07-29 01:39:11 +00:00
**preprovision** - to enable or disable internal pre-provisioning (e.g. if you already have another way to provision VMs). Conflicts with: `ssh_forward_ip`, `ssh_user`, `ssh_private_key`, `os_type`, `os_network_config`.
**os_type** -
2018-07-13 17:25:37 +00:00
* cloud-init - from Proxmox 5.2
* ubuntu -(https://github.com/Telmate/terraform-ubuntu-proxmox-iso)
* centos - (TODO: centos iso template)
**ssh_forward_ip** - should be the IP or hostname of the target node or bridge IP. This is where proxmox will create a port forward to your VM with via a user_net. (for pre-cloud-init provisioning)
### Cloud-Init
2017-08-01 23:00:55 +00:00
Cloud-init VMs must be cloned from a cloud-init ready template.
2018-07-13 17:25:37 +00:00
See: https://pve.proxmox.com/wiki/Cloud-Init_Support
2017-02-09 21:36:31 +00:00
2018-07-13 17:25:37 +00:00
* ciuser - User name to change ssh keys and password for instead of the images configured default user.
* cipassword - Password to assign the user.
* searchdomain - Sets DNS search domains for a container.
* nameserver - Sets DNS server IP address for a container.
* sshkeys - public ssh keys, one per line
* ipconfig0 - [gw=<GatewayIPv4>] [,gw6=<GatewayIPv6>] [,ip=<IPv4Format/CIDR>] [,ip6=<IPv6Format/CIDR>]
* ipconfig1 - optional, same as ipconfig0 format
2017-02-09 21:36:31 +00:00
2018-07-13 17:25:37 +00:00
### Preprovision (internal alternative to Cloud-Init)
2017-08-01 23:00:55 +00:00
There is a pre-provision phase which is used to set a hostname, intialize eth0, and resize the VM disk to available space. This is done over SSH with the ssh_forward_ip, ssh_user and ssh_private_key.
Disk resize is done if the file /etc/auto_resize_vda.sh exists. Source: https://github.com/Telmate/terraform-ubuntu-proxmox-iso/blob/master/auto_resize_vda.sh
### Provisioner usage
Remove the temporary net1 adapter.
Inside the VM this usually triggers the routes back to the provisioning machine on net0.
```
provisioner "proxmox" {
action = "sshbackward"
}
2017-08-01 23:00:55 +00:00
```
Replace the temporary net1 adapter with a new persistent net1.
```
provisioner "proxmox" {
action = "reconnect"
net1 = "virtio,bridge=vmbr0,tag=99"
}
2017-08-01 23:00:55 +00:00
```
If net1 needs a config other than DHCP you should prior to this use provisioner "remote-exec" to modify the network config.