Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Cloud-Init to set an static IP in the virtual machine. #667

Closed
donkerm opened this issue May 11, 2021 · 2 comments
Closed

Using Cloud-Init to set an static IP in the virtual machine. #667

donkerm opened this issue May 11, 2021 · 2 comments

Comments

@donkerm
Copy link

donkerm commented May 11, 2021

Could somebody tell me the correct way to use cloud-init with this vcd provider?
I tried a lot of possibilities, but I can't get it working.
I can't find this option described in the documentation and maybe it's not possible.
Looking at #510 it should work.?
Please advise?

Terraform Version

Terraform v0.12.18

  • provider.template v2.2.0
  • provider.vcd v3.2.0
    vCloud 10.1

Affected Resource(s)

Static network configuration.

Terraform Configuration Files

In vm.tf
guest_properties = {
"user-data" = base64encode(file("test.yaml"))
}
network {
adapter_type = "VMXNET3"
type = "org"
name = vcd_vapp_org_network.direct-net.org_network_name
ip_allocation_mode = "MANUAL"
ip = "192.168.178.3"
mac = "de:ad:c0:de:ca:fe"
is_primary = true
connected = true
}

In test.yaml

  • content: |
    network:
    version: 2
    renderer: networkd
    ethernets:
    ens192:
    addresses: [192.168.178.3/24]
    gateway4: 192.168.178.1
    dhcp4: false
    nameservers:
    addresses: [8.8.8.8]
    path: /etc/netplan/50-cloud-init.yaml

No error and virtual machine template has Cloud-Init installed together with VMWare tools (latest).

Expected Behavior

Configuring an IP on the interface ens192.

Actual Behavior

No error's but also no configuration has been done.

@Didainius
Copy link
Collaborator

Hello @donkerm,

tl;dr this is a very specific thing and depends on guest VM.

You need to check what datasource does cloud-init use in your case and adjust it. At least some time ago the default was DatasourceOVF which was pretty limited (https://github.com/canonical/cloud-init/blob/master/cloudinit/sources/DataSourceOVF.py)

There is also a more advanced VMware tailored datasource - https://github.com/vmware/cloud-init-vmware-guestinfo
However you would probably need to install it in your guest.

After that - the best option is to probably look at logs in guest machine and see if cloud-init was triggered, which datasource it used and if it got the data.

Another option - you could join VMware {Code} slack space https://code.vmware.com/web/code/join and join vcd-terraform-dev channel. There was a guy sharing how he made it work some time ago.

I will close this for now, but feel free to add details.

@donkerm
Copy link
Author

donkerm commented May 19, 2021

Hi all,

I wanted to help the community and describe what I ran into and how I got this working.
First a Shout out to:
Scott Rosenberg and Dainius, They set me on the correct path to the answer.

The problem that I had was the OVA machine I tried to use.
A standard version of Ubuntu.
First part to make this working correctly is to download the cloud image at:
http://cloud-images.ubuntu.com/

My working configuration:

versions.tf

terraform {
  required_providers {
    vcd = {
      source = "vmware/vcd"
    }
  }
  required_version = ">= 0.13"
}

variables.tf

# vCloud Director Connection Variables
variable "vcd_user" {
    description = "vCloud user"
}

variable "vcd_pass" {
    description = "vCloud pass"
}

variable "vcd_url" {
    description = "vCloud url"
}

variable "vcd_max_retry_timeout" {
    description = "vCloud max retry timeout"
}

variable "vcd_allow_unverified_ssl" {
    description = "vCloud allow unverified ssl"
}

variable "vcd_org" {
    description = "vCloud use certain organisation"
}

variable "vcd_ovdc" {
    description = "vCloud use certain OvDC"
}

main.tf

# Connect VMware vCloud Director Provider
provider "vcd" {
  user                 = var.vcd_user
  password             = var.vcd_pass
  url                  = var.vcd_url
  max_retry_timeout    = var.vcd_max_retry_timeout
  allow_unverified_ssl = var.vcd_allow_unverified_ssl
  org                  = var.vcd_org
  vdc                  = var.vcd_ovdc
}


# Org vAPP
resource "vcd_vapp" "vapp" {
    name = "vAPP_TEST"
    org  = var.vcd_org
    vdc  = var.vcd_ovdc
}

# Org External Network
resource "vcd_vapp_org_network" "direct-net" {
  vapp_name        = vcd_vapp.vapp.name
  org_network_name = "TEST-NETWORK"
  org              = var.vcd_org
  vdc              = var.vcd_ovdc

}

resource "vcd_independent_disk" "disk1" {
  name         = "log_Disk2"
  size_in_mb   = (100 * 1024)
  bus_type     = "SCSI"
  bus_sub_type = "VirtualSCSI"
  org          = var.vcd_org
  vdc          = var.vcd_ovdc

}


resource "vcd_vapp_vm" "TestVM1" {
    org           = var.vcd_org
    vdc           = var.vcd_ovdc
    vapp_name     = vcd_vapp.vapp.name
    name          = "TestVM1"
    computer_name = "TestVM1"
    description   = "Database servers Kubernetes PRD"

    catalog_name  = "Catalog2"
    template_name = "Ubuntu_20.04"

    memory    = 2048
    cpus      = 2
    cpu_cores = 1


  guest_properties = {
    "hostname"    = "TestVM1"
    "password"    = "password"
    "user-data" = base64encode(templatefile("userdata.tmpl",{
    "hostname" = "foo"}))
  }

    network {
        adapter_type       = "VMXNET3"
        type               = "org"
        name               =  vcd_vapp_org_network.direct-net.org_network_name
        ip_allocation_mode = "MANUAL"
        ip                 = "192.168.178.1"
        mac                = "de:ad:c0:de:ca:fe"
        is_primary         =  true
        connected          =  true
    }

        network {
        adapter_type       = "VMXNET3"
        type               = "org"
        name               =  vcd_vapp_org_network.direct-net.org_network_name
        ip_allocation_mode = "MANUAL"
        ip                 = "192.168.178.2"
        mac                = "de:ad:c1:de:ca:fe"
        is_primary         =  false
        connected          =  true
    }

    disk {
        name        = vcd_independent_disk.disk1.name
        bus_number  = 1
        unit_number = 0
    }

}

userdata.tmpl

#cloud-config

preserve_hostname: False
hostname: ${hostname}

write_files:
  - path: /etc/netplan/501-cloud-init.yaml
    content: |
      network:
        version: 2
        renderer: networkd
        ethernets:
          ens256:
            routes:
              - to: 192.44.0.0/14
                via: 192.168.178.9
                metric: 100
            nameservers:
              addresses:
                - 194.150.191.99
                - 194.8.57.12

runcmd:
  - [ netplan, apply ]

Things I ran into:
With new version of Terraform I needed the versions.tf:

Also I needed to:
Put #cloud-config in the top.
I had a TAB in the file somewhere and Cloud-Init broke (So check for correct formatting).

Thanks for the feedback and support.
I hope this post will help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants