josenk / terraform-provider-esxi

Terraform-provider-esxi plugin
GNU General Public License v3.0
538 stars 154 forks source link

Not able to login in this customized case #215

Closed chunji08 closed 3 months ago

chunji08 commented 9 months ago

I was playing the example cases you provided.

  1. I was able to run case: example/06 OVF Properties to create a vm, and was able to login on the console using: root/ubuntu1 or ubuntu/ubuntu2
  2. I was able to run case example/08 Networking cloud init, which have an IP address setup.

Now, I want to have a combined case, in which login credential is OK, and IP is also being setup.

So based on your original code under example/08 Networking cloud init

1) here is my change on variable.tf file


variable "esxi_hostname" {
  default = "10.106.6.8"
}

variable "esxi_hostport" {
  default = "22"
}

variable "esxi_hostssl" {
  default = "443"
}

variable "esxi_username" {
  default = "root"
}

variable "esxi_password" { # Unspecified will prompt
}

variable "vmIP" {
  default = "10.10.10.10/24"
}

variable "vmGateway" {
  default = "10.10.10.1"
}

variable "nameserver" {
  default = "8.8.8.8"
}

variable "vm_hostname" {
  default = "vmtest101"
}

variable "ovf_file" {
  #  A local file downloaded from https://cloud-images.ubuntu.com
  default = "/home/chji/kube/images/ubuntu-18.04-server-cloudimg-amd64.ova"
}

in which : a) 10.106.6.8 is my VMWARE box, and it been ssh-enabled. b) the ova file is from my local dir.

2) here is my update on the main.tf

provider "esxi" {
  esxi_hostname = var.esxi_hostname
  esxi_hostport = var.esxi_hostport
  esxi_hostssl  = var.esxi_hostssl
  esxi_username = var.esxi_username
  esxi_password = var.esxi_password
}

resource "esxi_portgroup" "myportgroup" {
  name = "My Port Group"
  vswitch = esxi_vswitch.myvswitch.name
}

data "template_file" "userdata_default" {
  template = file("userdata.tpl")
  vars = {
    HOSTNAME = var.vm_hostname
    HELLO    = "Hello ESXi World!"
  }
}

resource "esxi_guest" "vmtest01" {
  guest_name = "vmtest01"

  disk_store = "6T-Local"
  network_interfaces {
    virtual_network = esxi_portgroup.myportgroup.name  # Connecting to the above portgroup
  }

  guestinfo = {
    "metadata.encoding" = "gzip+base64"
    "metadata"          = base64gzip(data.template_file.cloud-metadata.rendered)
  }

  ovf_source        = var.ovf_file

  #
  #  Specify ovf_properties specific to the source ovf/ova.
  #    Use ovftool <filename>.ova to get details of which ovf_properties are available.
  #
  # ovf_properties {
  #  key = "hostname"
  #   value = "firstboot"
  # }

  ovf_properties {
    key = "user-data"
    value = base64encode(data.template_file.userdata_default.rendered)
  }

}

  data "template_file" "cloud-metadata" {
     template = file("metadata.tpl")
     vars = {
       ipAddress = var.vmIP
       gateway = var.vmGateway
       nameserver = var.nameserver
    }
}

3) I have also copied the example/06 OVF Properties/userdata.tpl copied under examples/08 Networking cloud init dir.

So, during the run-time, the terraform-init, terraform-plan, and terraform-apply are running smoothly, and here is the message I got when I am running terraform show command:

terraform show
# data.template_file.cloud-metadata:
data "template_file" "cloud-metadata" {
    id       = "6454c3f9a19f48fb5ca311b9d049ce352c7ec88eefc94d3aa03435c2f411084a"
    rendered = <<-EOT
        network:
            version: 2
            ethernets:
                ens192:
                    dhcp4: false
                    addresses:
                        - 10.10.10.10/24
                    gateway4: 10.10.10.1
                    nameservers:
                        addresses:
                            - 8.8.8.8

        # example
        # network:
        #     version: 2
        #     ethernets:
        #         ens192:
        #             dhcp4: false
        #             addresses:
        #                 - 10.10.10.1/24
        #             gateway4: 10.10.10.254
        #             nameservers:
        #                 addresses:
        #                     - 8.8.8.8
    EOT
    template = <<-EOT
        network:
            version: 2
            ethernets:
                ens192:
                    dhcp4: false
                    addresses:
                        - ${ipAddress}
                    gateway4: ${gateway}
                    nameservers:
                        addresses:
                            - ${nameserver}

        # example
        # network:
        #     version: 2
        #     ethernets:
        #         ens192:
        #             dhcp4: false
        #             addresses:
        #                 - 10.10.10.1/24
        #             gateway4: 10.10.10.254
        #             nameservers:
        #                 addresses:
        #                     - 8.8.8.8
    EOT
    vars     = {
        "gateway"    = "10.10.10.1"
        "ipAddress"  = "10.10.10.10/24"
        "nameserver" = "8.8.8.8"
    }
}

# data.template_file.userdata_default:
data "template_file" "userdata_default" {
    id       = "205000d03ad83a5e45f818babec7cf976e611a3d822f1d837240e256bccc6710"
    rendered = <<-EOT
        #cloud-config

        #  Install stuff
        packages:
         - ntp
         - ntpdate
         - curl

        # Override ntp with chrony configuration on Ubuntu
        ntp:
          enabled: true
          ntp_client: chrony  # Uses cloud-init default chrony configuration

        # Configure ubuntu user security
        users:
          - name: ubuntu
            sudo: [ "ALL=(ALL) NOPASSWD:ALL" ]
            ssh-authorized-keys:
              - ...

        #  Change some default passwords
        chpasswd:
          list: |
            root:ubuntu1
            ubuntu:ubuntu2
          expire: False

        #  Write to a log file (useing variables set in terraform) and show the ip on the console.
        runcmd:
          - date >/root/cloudinit.log
          - hostnamectl set-hostname vmtest101
          - echo Hello ESXi World! >>/root/cloudinit.log
          - echo "Done cloud-init" >>/root/cloudinit.log
          - ip a >/dev/tty1
    EOT
    template = <<-EOT
        #cloud-config

        #  Install stuff
        packages:
         - ntp
         - ntpdate
         - curl

        # Override ntp with chrony configuration on Ubuntu
        ntp:
          enabled: true
          ntp_client: chrony  # Uses cloud-init default chrony configuration

        # Configure ubuntu user security
        users:
          - name: ubuntu
            sudo: [ "ALL=(ALL) NOPASSWD:ALL" ]
            ssh-authorized-keys:
              - ssh-rsa ...

        #  Change some default passwords
        chpasswd:
          list: |
            root:ubuntu1
            ubuntu:ubuntu2
          expire: False

        #  Write to a log file (useing variables set in terraform) and show the ip on the console.
        runcmd:
          - date >/root/cloudinit.log
          - hostnamectl set-hostname ${HOSTNAME}
          - echo ${HELLO} >>/root/cloudinit.log
          - echo "Done cloud-init" >>/root/cloudinit.log
          - ip a >/dev/tty1
    EOT
    vars     = {
        "HELLO"    = "Hello ESXi World!"
        "HOSTNAME" = "vmtest101"
    }
}

# esxi_guest.vmtest01:
resource "esxi_guest" "vmtest01" {
    boot_disk_type         = "thin"
    boot_firmware          = "bios"
    disk_store             = "6T-Local"
    guest_name             = "vmtest01"
    guest_shutdown_timeout = 20
    guest_startup_timeout  = 120
    guestinfo              = {
        "metadata"          = "..."
        "metadata.encoding" = "gzip+base64"
    }
    guestos                = "ubuntu-64"
    id                     = "115"
    ip_address             = "10.10.10.10"
    memsize                = "1024"
    numvcpus               = "2"
    ovf_properties_timer   = 90
    ovf_source             = "/home/chji/kube/images/ubuntu-18.04-server-cloudimg-amd64.ova"
    power                  = "on"
    resource_pool_name     = "/"
    virthwver              = "10"

    network_interfaces {
        nic_type        = "e1000"
        virtual_network = "My Port Group"
    }

    ovf_properties {
        key   = "user-data"
        value = "..."
    }
}

# esxi_portgroup.myportgroup:
resource "esxi_portgroup" "myportgroup" {
    id      = "My Port Group"
    name    = "My Port Group"
    vlan    = 0
    vswitch = "My vSwitch"
}

# esxi_vswitch.myvswitch:
resource "esxi_vswitch" "myvswitch" {
    forged_transmits    = false
    id                  = "My vSwitch"
    link_discovery_mode = "listen"
    mac_changes         = false
    mtu                 = 1500
    name                = "My vSwitch"
    ports               = 128
    promiscuous_mode    = false
}

Outputs:

ip = [
    "10.10.10.10",
]

But, when I was to try to login on the console of this created VM , I was not able to do it with either of these these 2 accounts, root/ubuntu or ubuntu/ubuntu2.

Any idea what could be wrong ?

Thanks for the help.

Chun Ji

josenk commented 5 months ago

Looks like the plugin is functional. Userdata stuff in the examples are not supported here. You'll have to check the userdata logs to get root cause.

josenk commented 3 months ago

abandoned