Skip to content

Commit

Permalink
Merge pull request Telmate#149 from godiedelrio/cloud-init-custom-con…
Browse files Browse the repository at this point in the history
…fig-file

Cloud init custom config file
  • Loading branch information
ggongaware committed Mar 3, 2020
2 parents 4403acf + 8297ca5 commit b17db9b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,84 @@ EOF
}
}
/* Null resource that generates a cloud-config file per vm */
data "template_file" "user_data" {
count = var.vm_count
template = file("${path.module}/files/user_data.cfg")
vars = {
pubkey = file("~/.ssh/id_rsa.pub")
hostname = "vm-${count.index}"
fqdn = "vm-${count.index}.${var.domain_name}"
}
}
resource "local_file" "cloud_init_user_data_file" {
count = var.vm_count
content = data.template_file.user_data[count.index].rendered
filename = "${path.module}/files/user_data_${count.index}.cfg"
}
resource "null_resource" "cloud_init_config_files" {
count = var.vm_count
connection {
type = "ssh"
user = "${var.pve_user}"
password = "${var.pve_password}"
host = "${var.pve_host}"
}
provisioner "file" {
source = local_file.cloud_init_user_data_file[count.index].filename
destination = "/var/lib/vz/snippets/user_data_vm-${count.index}.yml"
}
}
/* Configure cloud-init User-Data with custom config file */
resource "proxmox_vm_qemu" "cloudinit-test" {
depends_on = [
null_resource.cloud_init_config_files,
]
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
-----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"
/*
sshkeys and other User-Data parameters are specified with a custom config file.
In this example each VM has its own config file, previously generated and uploaded to
the snippets folder in the local storage in the Proxmox VE server.
*/
cicustom = "user=local:snippets/user_data_vm-${count.index}.yml"
provisioner "remote-exec" {
inline = [
"ip a"
]
}
}
/* Uses custom eth1 user-net SSH portforward */
resource "proxmox_vm_qemu" "prepprovision-test" {
name = "tftest1.xyz.com"
Expand Down Expand Up @@ -227,6 +305,10 @@ See: https://pve.proxmox.com/wiki/Cloud-Init_Support
* ipconfig0 - [gw=<GatewayIPv4>] [,gw6=<GatewayIPv6>] [,ip=<IPv4Format/CIDR>] [,ip6=<IPv6Format/CIDR>]
* ipconfig1 - optional, same as ipconfig0 format

Alternatively, cloud-init configuration can be customized with config files that reside in a volume in the Proxmox VE server. Use the attribute `cicustom` to indicate the location of these files.

* cicustom - location of cloud-config files that Proxmox will put in the generated cloud-init config iso image, e.g. `cicustom = "user=local:snippets/user_data.yaml"`. For more info about this attribute, see the details of the parameter `--cicustom` in the section "Custom Cloud-Init Configuration" from the [Proxmox Cloud-Init support](https://pve.proxmox.com/wiki/Cloud-Init_Support) page.

### Preprovision (internal alternative to Cloud-Init)

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.
Expand Down
7 changes: 7 additions & 0 deletions proxmox/resource_vm_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ func resourceVmQemu() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"cicustom": {
Type: schema.TypeString,
Optional: true,
},
"searchdomain": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -525,6 +529,7 @@ func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error {
// Cloud-init.
CIuser: d.Get("ciuser").(string),
CIpassword: d.Get("cipassword").(string),
CIcustom: d.Get("cicustom").(string),
Searchdomain: d.Get("searchdomain").(string),
Nameserver: d.Get("nameserver").(string),
Sshkeys: d.Get("sshkeys").(string),
Expand Down Expand Up @@ -730,6 +735,7 @@ func resourceVmQemuUpdate(d *schema.ResourceData, meta interface{}) error {
// Cloud-init.
CIuser: d.Get("ciuser").(string),
CIpassword: d.Get("cipassword").(string),
CIcustom: d.Get("cicustom").(string),
Searchdomain: d.Get("searchdomain").(string),
Nameserver: d.Get("nameserver").(string),
Sshkeys: d.Get("sshkeys").(string),
Expand Down Expand Up @@ -831,6 +837,7 @@ func resourceVmQemuRead(d *schema.ResourceData, meta interface{}) error {
// Cloud-init.
d.Set("ciuser", config.CIuser)
d.Set("cipassword", config.CIpassword)
d.Set("cicustom", config.CIcustom)
d.Set("searchdomain", config.Searchdomain)
d.Set("nameserver", config.Nameserver)
d.Set("sshkeys", config.Sshkeys)
Expand Down

0 comments on commit b17db9b

Please sign in to comment.