diff --git a/.gitignore b/.gitignore index 35f8a67..8287572 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,15 @@ crash.log # variables -terraform.tfvars \ No newline at end of file +terraform.tfvars +*.swp +master.yml +slave.yml +hosts.master +hosts.slave +ns*.json +tfplan +config +install_key.sh +modules/ns/resources/install_ansible.sh +*.tmp diff --git a/README.md b/README.md index c893e1e..781b256 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # :cloud: vSphere Terraform Ubuntu-cloud OVA/OVF This repository contians an example how to deploy an cloud-init configurable Ubuntu-cloud OVA to VMware vSphere using Terraform -**It is [part of a blog post](https://shorez.de/2018/deploying-ubuntu-cloud-images-to-vsphere-using-terraform/) , consider reading it for further instructions** +~~**It is [part of a blog post](https://shorez.de/2018/deploying-ubuntu-cloud-images-to-vsphere-using-terraform/) , consider reading it for further instructions**~~ +I do not have a blog anymore, so the [post is included in this repository](post/README.md) # License :book: Released into the public domain under terms of the [UNLICENSE](/LICENSE). diff --git a/alternative/README.md b/alternative/README.md new file mode 100644 index 0000000..557a2bd --- /dev/null +++ b/alternative/README.md @@ -0,0 +1,20 @@ +# vSphere terraform unify os for customize ip hostname setting + +The original repository using ova package, the alternative way using ISO pre-install VM +convert VM to template for customize. + +For pre-install VM step: + +1. Just install VM by ISO in normal way. +2. Left most option default. +3. Check below critiria, ensure you setup VM template correct. + +Some point you may need to know before using vSphere plugin + +1. vmware tool is required for clone customize +2. perl is required(if you search you will see a lot of user failed because perl +3. please aware the support matrix, some essential OS may failed the guestOS check. + +Note: if you are using CentOS, sometime may failed because /etc/redhat-release not match. +There is a trick hint: for me using **Red Hat Enterprise Linux Server release 7.0 (Maipo)** +, it will work and not effect function. diff --git a/alternative/terraform.tf b/alternative/terraform.tf new file mode 100644 index 0000000..9c0b1bd --- /dev/null +++ b/alternative/terraform.tf @@ -0,0 +1,79 @@ +provider "vsphere" { + user = "${var.user}" + password = "${var.password}" + vsphere_server = "${var.vsphere_server}" + + allow_unverified_ssl = true +} + +data "vsphere_datacenter" "dc" { + name = "${var.datacenter}" +} + +data "vsphere_datastore" "datastore" { + name = "${var.datastore}" + datacenter_id = "${data.vsphere_datacenter.dc.id}" +} + +data "vsphere_resource_pool" "pool" { + name = "${var.resource_pool}" + datacenter_id = "${data.vsphere_datacenter.dc.id}" +} + +data "vsphere_network" "network" { + name = "${var.network}" + datacenter_id = "${data.vsphere_datacenter.dc.id}" +} + +data "vsphere_virtual_machine" "template" { + name = "${var.template}" + datacenter_id = "${data.vsphere_datacenter.dc.id}" +} + +resource "vsphere_virtual_machine" "vm" { + count = "${var.vm_numbers}" + name = "${var.name}-${count.index+1}" + resource_pool_id = "${data.vsphere_resource_pool.pool.id}" + datastore_id = "${data.vsphere_datastore.datastore.id}" + + num_cpus = 4 + memory = 8192 + guest_id = "${data.vsphere_virtual_machine.template.guest_id}" + + scsi_type = "${data.vsphere_virtual_machine.template.scsi_type}" + + network_interface { + network_id = "${data.vsphere_network.network.id}" + adapter_type = "${data.vsphere_virtual_machine.template.network_interface_types[0]}" + } + + disk { + label = "disk0" + size = "${data.vsphere_virtual_machine.template.disks.0.size}" + eagerly_scrub = "${data.vsphere_virtual_machine.template.disks.0.eagerly_scrub}" + thin_provisioned = "${data.vsphere_virtual_machine.template.disks.0.thin_provisioned}" + } + + cdrom { + client_device = true + } + + clone { + template_uuid = "${data.vsphere_virtual_machine.template.id}" + customize { + linux_options { + host_name = "${var.name}${count.index+1}" + domain = "${var.virtual_machine_domain}" + } + + network_interface { + ipv4_address = "${cidrhost(var.virtual_machine_network_address, var.virtual_machine_ip_address_start + count.index)}" + ipv4_netmask = "${element(split("/", var.virtual_machine_network_address), 1)}" + } + + ipv4_gateway = "${var.virtual_machine_network_gateway}" + dns_suffix_list = ["${var.virtual_machine_domain}"] + dns_server_list = ["${var.virtual_machine_dns_servers}"] + } + } +} diff --git a/alternative/terraform.tfvars.example b/alternative/terraform.tfvars.example new file mode 100644 index 0000000..319b32c --- /dev/null +++ b/alternative/terraform.tfvars.example @@ -0,0 +1,38 @@ +# General vCenter data +user = "FILL_ME_IN" # VC User + +password = "FILL_ME_IN" # vSphere Password + +vsphere_server = "FILL_ME_IN" # VC URL (IP, hostname or FQDN) + +datacenter = "FILL_ME_IN" # vSphere datacenter + +datastore = "FILL_ME_IN" # vSphere datastore + +resource_pool = "FILL_ME_IN" # vSphere Resourcepool + +# Virtual Machine configuration +name = "FILL_ME_IN" # name of the virtual machine + +template = "FILL_ME_IN" # chosen name of the template + +network = "FILL_ME_IN" # network for the VM to reside in + +cpus = 2 # CPU cores of the VM + +memory = 1024 # Memory of the VM in Mb + +vm_numbers = 4 # VM numbers you want to create + +virtual_machine_network_address = "FILL_ME_IN" # VM cidr ex: 192.168.1.0/24 + +virtual_machine_ip_address_start = "FILL_ME_IN" # ex: 100 + +virtual_machine_domain = "FILL_ME_IN" # domain ex: example.com + +virtual_machine_network_gateway = "FILL_ME_IN" # ex: 192.168.1.254 + +virtual_machine_dns_servers = [ + "8.8.8.8", + "8.8.4.4", +] diff --git a/alternative/variables.tf b/alternative/variables.tf new file mode 100644 index 0000000..e0adda2 --- /dev/null +++ b/alternative/variables.tf @@ -0,0 +1,62 @@ +# General vCenter data +# vCenter / ESXi Username +variable "user" {} + +# vCenter / ESXi Password +variable "password" {} + +# vCenter / ESXi Endpoint +variable "vsphere_server" {} + +# vCenter / ESXi Datacenter +variable "datacenter" {} + +# vCenter / ESXi Datastore +variable "datastore" {} + +# vCenter / ESXi ResourcePool +variable "resource_pool" {} + +# Virtual Machine configuration +# VM Name +variable "name" {} + +# Name of OVA template (chosen in import process) +variable "template" {} + +# VM Network +variable "network" {} + +# VM Number of CPU's +variable "cpus" {} + +# VM Memory in MB +variable "memory" {} +# VM numbers +variable "vm_numbers" {} + +// The network address for the virtual machines, in the form of 10.0.0.0/24. +variable "virtual_machine_network_address" { + type = "string" +} + +variable "virtual_machine_network_gateway" { + type = "string" +} + +variable "virtual_machine_domain" { + type = "string" +} + +// The last octect that serves as the start of the IP addresses for the virtual +// machines. Given the default value here of 100, if the network address is +// 10.0.0.0/24, the 3 virtual machines will be assigned addresses 10.0.0.100, +// 10.0.0.101, and 10.0.0.102. +variable "virtual_machine_ip_address_start" { + type = "string" +} + +// The DNS servers for the network the virtual machines reside in. +variable "virtual_machine_dns_servers" { + type = "list" +} diff --git a/post/README.md b/post/README.md new file mode 100644 index 0000000..b30981e --- /dev/null +++ b/post/README.md @@ -0,0 +1,178 @@ +**Attention**: I have lost the images for this post. As I do not own vCenter at the moment, I can not reproduce them. Sorry! + +# Deploying Ubuntu Cloud Images to vSphere using Terraform + +It's fairly easy to deploy cloud-init configurable Ubuntu Cloud OVA Images to vSphere (standalone ESXi or vCenter, using Terraform. The following post illustrates how to achieve this, all project files are provided on [GitHub here](https://github.com/sh0rez/vSphere-terraform_ubuntu-cloud-ova). + + +The real benefit of this is probably the way the final deployment works: On the one hand it is fully declarative (thank's Terraform) but on the other all configuration of the actual Ubuntu OS is done by cloud-init, so the image is ready to boot, no installation required, just by a single `terraform apply`. + +## Ubuntu Cloud Image? OVA? +#### Ubuntu Cloud Image +Canonical (the company behind Ubuntu) is distributing special kinds of the OS called "cloud images", available for download at at [cloud-images.ubuntu.com](https://cloud-images.ubuntu.com/). They are special compared to the regular Ubuntu Server: Being targeted at the cloud (most notably here OpenStack), they don't require actual installation to the hard drive. Rather they are in some kind of "virgin" state, ready to boot but unusable due to a lack of appropriate configuration. + +This configuration must be done by the administrator using a tool called [cloud-init](https://cloudinit.readthedocs.io/en/latest/). Basically, in some way a `.yaml` file is injected into the system at boot time, declaring the desired state of the OS. `cloud-init` then takes the needed steps to reach that state. Luckily, we do not need to know how this works in detail here, vSphere does the hard part of injecting the configuration. We just need to provide it. + +#### OVA +Ok. So what's to OVA? +OVA (or OVF) is a standardized, open format for storing Virtual Machines, mostly used in the worlds of VMware and VirtualBox. For people wanting to know how this actually works, there is an excellent [Wikipedia Entry](https://en.wikipedia.org/wiki/Open_Virtualization_Format) about this. + +While other popular platforms like OpenStack prefer `.qcow2` images, `.ova` is the perfect fit for our vSphere, also because Canonical provides an [Ubuntu OVA](https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.ova) which is configurable using cloud-init. + +## Getting started +There are several steps needed to get started: + +1. Access to vSphere. This may be vCenter or standalone ESXi, but keep in mind the pro or trial version is needed to access the API needed for Terraform. The free tier does not work here :( +2. Terraform. Installation is covered here, the tested version at the time of writing this article is `v0.11.7` + +## Uploading the OVA (once!) +Before creating actual instances, the template (OVA) must be uploaded to vSphere. + +First, head over to [cloud-images.ubuntu.com](https://cloud-images.ubuntu.com/), select your version of Ubuntu (sticking with `16.04 Xenial` release here, altough there should be no difference when using `18.04 Bionic` or any other Ubuntu version), select the **`current`** release (or if you know what you're doing something else) and download the file called something similar to `VERSION-server-cloudimg-amd64.ova` + + +![ubuntu-cloud-ova-download](/ubuntu-cloud-ova-download.png) + + +Once we have this file, we can upload it to our vSphere. This is shown using vCenter 6.7 here: + +**1.** First, right click the datacenter or single host and select `Deploy OVF Template`: + + +![deploy-ova-vcenter](/deploy-ova-vcenter.png) + + +After that, upload the downloaded OVA File and hit next: + + +![deploy_ova_step1](/deploy_ova_step1.png) + + +**2.** Once uploaded, select where to store the template and name it appropriately. We need that name later on. + +**3, 4, 5.** Select your compute resource in step 3 and review your setup in step 4, continue with step 5, storage. The only thing here to do is select `Thin Provision` as disk format, as it saves (a lot of) storage. + + +![deploy_ova_step5](/deploy_ova_step5.png) + + +**6. & 7.** What you select in steps 6 and 7 doesn't matter as it is going to be overwritten by Terraform anyways. Just leave the default values here, that's fine. + +**8.** Finally, in step 8 review the selection and hit import. + +**WARNING:** After the import succeeds, **don't power on the template**. This will break cloud-init. + +## Terraforming Ubuntu Instances +We are done with the 'manual' part here. Let's dive in to the world of IAC. + +Let's talk quickly how this is going to work. So: vSphere provides cloud-init support using a feature called `vApp properties`. I'm unable to find further documentation about this but it works so let's don't care. + +Terraform also supports this with little hassle: The cloud-init data needs to be base-64 encoded for vApp but Terraform can do this easily using built in functions. Finally, I decided to load an external `cloud-init.yml` file into the base64 function and submit this to vSphere. + +The full example is available on [GitHub](https://github.com/sh0rez/vSphere-terraform_ubuntu-cloud-ova). + +The basic procedure of creating a virtual machine on vSphere using Terraform is [perfectly described in the official docs](https://www.terraform.io/docs/providers/vsphere/index.html). To use the benefits of the Ubuntu-Cloud OVA, there are important key aspects that matter: + +1. Cloning from the template. The new VM is just a clone of the template VM with several settings overriden. +```js +data "vsphere_virtual_machine" "template" { + name = "TEMPLATE_VM_NAME_HERE" + datacenter_id = "${data.vsphere_datacenter.dc.id}" +} + +resource "vsphere_virtual_machine" "vm" { + clone { + template_uuid = "${data.vsphere_virtual_machine.template.id}" + } +} +``` + + +2. vApp properties: To push the values from the cloud-init.yml file to the new VM, base64 encode the file: +```js +resource "vsphere_virtual_machine" "vm" { + clone { + template_uuid = "${data.vsphere_virtual_machine.template.id}" + } + vapp { + properties { + user-data = "${base64encode(file("cloud-init.yml"))}" + } + } +} +``` + +3. cloud-init.yml: This file is where the actual configuration magic happens. Explaining cloud-init is out of scope here, but consider reading their documentation, it provides handy examples: [Here](https://cloudinit.readthedocs.io/en/latest/topics/examples.html#) + +The rest of the `terraform.tf` is basically just the first example from the official docs. Just take a look at the GitHub repo, it's all self explanatory. + +### Configuring +First, checkout the example project from GitHub: +```shell +$ git clone https://github.com/sh0rez/vSphere-terraform_ubuntu-cloud-ova +$ cd vSphere-terraform_ubuntu-cloud-ova +``` + +Then go ahead and move the `terraform.tfvars.example` to `terraform.tfvars` and edit it with the editor of your choice: +```shell +$ mv terraform.tfvars.example terraform.tfvars + +# using vi here, take whatever you prefer +$ vi terraform.tfvars +``` + +This file is full of placeholders, waiting to be customized: + + +**General vSphere configuration:** +* `user`: Any user account present at your vSphere instance suitable for Terraform + +* `password`: The appropriate password for the chosen user + +* `vsphere_server`: Your vSphere instance. May be `IP-Address`, `FQDN` or `Hostname`. Has to resolve on the machine intended for using Terraform (probably your local machine) + +* `datacenter`: The vSphere datacenter to deploy to + +* `datastore`: The datastore to store the disk on + +* `resource_pool`: The Resource-Pool to use + + +**VM Settings:** +* `name`: The name of the VM + +* `template`: The name of the template. This MUST be the name you chose during importing the OVA. + +* `network`: The network for the VM to reside in + +* `cpus`: CPU-Cores of the VM. Just a number (e.g. `2`, `4`, etc.) + +* `memory` = Memory of the VM in Mb (e.g. `1024`, `2048`, etc.) + +### Deploying +Now as all neccessary data got filled in, there's nothing in the way to create the first VM. + +Just enter to get an overview of the intended changes: +```shell +$ terraform plan +``` + +Review the changes and if they are fine (they should be) continue applying: +```shell +$ terraform apply +# enter 'yes' to approve +``` + + +This takes around 1 minute and completes with a nice green message. Your new code-declarated VM is ready! Hooray 🎉. Just take a look at it in the web-interface: + + +![terraformed_vm_final](/terraformed_vm_final.png) + + +It's just another virtual machine. + +## Next steps +How to continue? Here are a few tips to get further on the topic: +1. Get familiar with cloud-init. To unleash the whole power of this project, you need to know how to configure the VM. Good examples are in the [official docs](https://cloudinit.readthedocs.io/en/latest/topics/examples.html) +2. Read the [Terraform vSphere provider docs](https://www.terraform.io/docs/providers/vsphere/index.html). Every capability of the provider is explained there. diff --git a/terraform-esx-okd/README.md b/terraform-esx-okd/README.md new file mode 100644 index 0000000..8d31a14 --- /dev/null +++ b/terraform-esx-okd/README.md @@ -0,0 +1,65 @@ +# Add openshift install using terraform trigger ansible +Need install ubuntu 16.04 LTS template +Trick: Add this line **After=dbus.service** in /lib/systemd/system/open-vm-tools.service [Unit] section + +# vSphere terraform unify os for customize ip hostname setting +This repository fork from sh0rez/vSphere-terraform_ubuntu-cloud-ova + +The original repository using ova package, this repository using ISO pre-install +template for customize. + +Some point you may need to know before using vSphere plugin + +1. vmware tool is required for clone customize +2. perl is required(if you search you will see a lot of user failed because perl +3. please aware the support matrix, some essential OS may failed the guestOS check. + +Note: if you are using CentOS, sometime may failed because /etc/redhat-release not match. +There is a trick hint: for me using **Red Hat Enterprise Linux Server release 7.0 (Maipo)** +, it will work and not effect function. +======= +~~**It is [part of a blog post](https://shorez.de/2018/deploying-ubuntu-cloud-images-to-vsphere-using-terraform/) , consider reading it for further instructions**~~ +I do not have a blog anymore, so the [post is included in this repository](post/README.md) + +# OKD template + +Change terraform.tfvars.example for your setting than cp to terraform.tfvars + +Install terraform via below website + +https://www.terraform.io/downloads.html + +After install terraform execute to construct your okd set +``` +terraform init +make +``` + +Use below command to destroy okd set +``` +make destroy +``` + +**if you deploy failed** +go to master + +``` +cd openshift-ansible +ansible-playbook -i inventory.ini playbooks/deploy_cluster.yml +``` + +Take a look into openshift-ansible.log wait the deploy complete +Using below command add a cluster-admin user +``` +htpasswd /etc/origin/master/htpasswd +oc adm policy add-cluster-role-to-user cluster-admin +``` + +for lab template password +okd node/master password: root/abc=123 +nameserver password: elsvent/password + + +# License :book: +Released into the public domain under terms of the [UNLICENSE](/LICENSE). +Any PR will be welcome.:) diff --git a/terraform-esx-okd/main.tf b/terraform-esx-okd/main.tf new file mode 100644 index 0000000..f22d366 --- /dev/null +++ b/terraform-esx-okd/main.tf @@ -0,0 +1,59 @@ + +module "infra" { + source = "./modules/infra" + memory = "${var.memory}" + cpus = "${var.cpus}" + name = "${var.name}" + datastore = "${var.datastore}" + resource_pool = "${var.resource_pool}" + vsphere_server = "${var.vsphere_server}" + user = "${var.user}" + template_centos = "${var.template_centos}" + template_ubuntu = "${var.template_ubuntu}" + vm_numbers = "${var.nodes}" + password = "${var.password}" + datacenter = "${var.datacenter}" + network = "${var.network}" + virtual_machine_network_address = "${var.virtual_machine_network_address}" + virtual_machine_ip_address_start = "${var.virtual_machine_ip_address_start}" + virtual_machine_network_gateway = "${var.virtual_machine_network_gateway}" + virtual_machine_domain = "${var.virtual_machine_domain}" +} + +module "ns" { + source = "./modules/ns" + ns_ip_list = "${module.infra.ns_ip_list}" + ns_hostname_list = "${module.infra.ns_hostname_list}" + master_ip_list = "${module.infra.master_ip_list}" + master_hostname_list = "${module.infra.master_hostname_list}" + node_ip_list = "${module.infra.node_ip_list}" + node_hostname_list = "${module.infra.node_hostname_list}" + virtual_machine_domain= "${var.virtual_machine_domain}" + public_hostname = "${var.name}" + network_prefix = "${var.virtual_machine_network}" +} + +module "openshift" { + source = "./modules/openshift" + public_hostname = "${var.name}" + master_hostname = "${module.infra.master_hostname_list[0]}" + master_ip = "${module.infra.master_ip_list[0]}" + node_hostname_list = "${module.infra.node_hostname_list}" + node_ip_list = "${module.infra.node_ip_list}" + domain = "${var.virtual_machine_domain}" + + +} + +output "dns_server_ip" { + value = "${module.infra.dns_server_ip}" +} +output "master_ip" { + value = "${module.infra.master_ip}" +} +output "node_ip" { + value = "${module.infra.node_ip}" +} +output "console address" { + value = "https://${var.name}.${var.virtual_machine_domain}:8443" +} diff --git a/terraform-esx-okd/makefile b/terraform-esx-okd/makefile new file mode 100644 index 0000000..aca2de6 --- /dev/null +++ b/terraform-esx-okd/makefile @@ -0,0 +1,16 @@ +all: infra ns openshift + +infra: + terraform apply -target=module.infra -auto-approve + +ns: infra + terraform apply -target=module.ns -auto-approve + +openshift: ns + terraform apply -target=module.openshift -auto-approve + +osd: + terraform destroy -target=module.openshift -auto-approve + +destroy: + terraform destroy -auto-approve diff --git a/terraform-esx-okd/modules/infra/01-var.tf b/terraform-esx-okd/modules/infra/01-var.tf new file mode 100644 index 0000000..5df05ea --- /dev/null +++ b/terraform-esx-okd/modules/infra/01-var.tf @@ -0,0 +1,60 @@ +# General vCenter data +# vCenter / ESXi Username +variable "user" {} + +# vCenter / ESXi Password +variable "password" {} + +# vCenter / ESXi Endpoint +variable "vsphere_server" {} + +# vCenter / ESXi Datacenter +variable "datacenter" {} + +# vCenter / ESXi Datastore +variable "datastore" {} + +# vCenter / ESXi ResourcePool +variable "resource_pool" {} + +# Virtual Machine configuration +# VM Name +variable "name" {} + +# Name of OVA template (chosen in import process) +variable "template_centos" {} +variable "template_ubuntu" {} + +# VM Network +variable "network" {} + +# VM Number of CPU's +variable "cpus" {} + +# VM Memory in MB +variable "memory" { +} +# VM numbers +variable "vm_numbers" { +} + +// The network address for the virtual machines, in the form of 10.0.0.0/24. +variable "virtual_machine_network_address" { + type = "string" +} + +variable "virtual_machine_network_gateway" { + type = "string" +} + +variable "virtual_machine_domain" { + type = "string" +} + +// The last octect that serves as the start of the IP addresses for the virtual +// machines. Given the default value here of 100, if the network address is +// 10.0.0.0/24, the 3 virtual machines will be assigned addresses 10.0.0.100, +// 10.0.0.101, and 10.0.0.102. +variable "virtual_machine_ip_address_start" { + type = "string" +} diff --git a/terraform-esx-okd/modules/infra/02-vc.tf b/terraform-esx-okd/modules/infra/02-vc.tf new file mode 100644 index 0000000..02d0135 --- /dev/null +++ b/terraform-esx-okd/modules/infra/02-vc.tf @@ -0,0 +1,26 @@ +provider "vsphere" { + user = "${var.user}" + password = "${var.password}" + vsphere_server = "${var.vsphere_server}" + + allow_unverified_ssl = true +} + +data "vsphere_datacenter" "dc" { + name = "${var.datacenter}" +} + +data "vsphere_datastore" "datastore" { + name = "${var.datastore}" + datacenter_id = "${data.vsphere_datacenter.dc.id}" +} + +data "vsphere_resource_pool" "pool" { + name = "${var.resource_pool}" + datacenter_id = "${data.vsphere_datacenter.dc.id}" +} + +data "vsphere_network" "network" { + name = "${var.network}" + datacenter_id = "${data.vsphere_datacenter.dc.id}" +} diff --git a/terraform-esx-okd/modules/infra/03-template.tf b/terraform-esx-okd/modules/infra/03-template.tf new file mode 100644 index 0000000..3afb73b --- /dev/null +++ b/terraform-esx-okd/modules/infra/03-template.tf @@ -0,0 +1,9 @@ +data "vsphere_virtual_machine" "template_centos" { + name = "${var.template_centos}" + datacenter_id = "${data.vsphere_datacenter.dc.id}" +} + +data "vsphere_virtual_machine" "template_ubuntu" { + name = "${var.template_ubuntu}" + datacenter_id = "${data.vsphere_datacenter.dc.id}" +} diff --git a/terraform-esx-okd/modules/infra/04-ns.tf b/terraform-esx-okd/modules/infra/04-ns.tf new file mode 100644 index 0000000..49541c9 --- /dev/null +++ b/terraform-esx-okd/modules/infra/04-ns.tf @@ -0,0 +1,46 @@ +resource "vsphere_virtual_machine" "ns" { + count = "2" + name = "${var.name}-ns-${count.index+1}" + resource_pool_id = "${data.vsphere_resource_pool.pool.id}" + datastore_id = "${data.vsphere_datastore.datastore.id}" + + num_cpus = 1 + memory = 1024 + guest_id = "${data.vsphere_virtual_machine.template_ubuntu.guest_id}" + + scsi_type = "${data.vsphere_virtual_machine.template_ubuntu.scsi_type}" + + network_interface { + network_id = "${data.vsphere_network.network.id}" + adapter_type = "${data.vsphere_virtual_machine.template_ubuntu.network_interface_types[0]}" + } + + disk { + label = "disk0" + size = "${data.vsphere_virtual_machine.template_ubuntu.disks.0.size}" + eagerly_scrub = "${data.vsphere_virtual_machine.template_ubuntu.disks.0.eagerly_scrub}" + thin_provisioned = "${data.vsphere_virtual_machine.template_ubuntu.disks.0.thin_provisioned}" + } + + cdrom { + client_device = true + } + + clone { + template_uuid = "${data.vsphere_virtual_machine.template_ubuntu.id}" + customize { + linux_options { + host_name = "${var.name}ns${count.index+1}" + domain = "localhost" + } + + network_interface { + ipv4_address = "${cidrhost(var.virtual_machine_network_address, var.virtual_machine_ip_address_start + count.index+1)}" + ipv4_netmask = "${element(split("/", var.virtual_machine_network_address), 1)}" + } + + ipv4_gateway = "${var.virtual_machine_network_gateway}" + dns_server_list = ["8.8.8.8", "8.8.4.4"] + } + } +} diff --git a/terraform-esx-okd/modules/infra/05-master.tf b/terraform-esx-okd/modules/infra/05-master.tf new file mode 100644 index 0000000..eef2b4d --- /dev/null +++ b/terraform-esx-okd/modules/infra/05-master.tf @@ -0,0 +1,48 @@ +resource "vsphere_virtual_machine" "master" { + count = "1" + name = "${var.name}-master" + resource_pool_id = "${data.vsphere_resource_pool.pool.id}" + datastore_id = "${data.vsphere_datastore.datastore.id}" + + num_cpus = 4 + memory = 16384 + guest_id = "${data.vsphere_virtual_machine.template_centos.guest_id}" + + scsi_type = "${data.vsphere_virtual_machine.template_centos.scsi_type}" + + network_interface { + network_id = "${data.vsphere_network.network.id}" + adapter_type = "${data.vsphere_virtual_machine.template_centos.network_interface_types[0]}" + } + + disk { + label = "disk0" + size = "${data.vsphere_virtual_machine.template_centos.disks.0.size}" + eagerly_scrub = "${data.vsphere_virtual_machine.template_centos.disks.0.eagerly_scrub}" + thin_provisioned = "${data.vsphere_virtual_machine.template_centos.disks.0.thin_provisioned}" + } + + cdrom { + client_device = true + } + + clone { + template_uuid = "${data.vsphere_virtual_machine.template_centos.id}" + customize { + linux_options { + host_name = "${var.name}master" + domain = "${var.virtual_machine_domain}" + } + + network_interface { + ipv4_address = "${cidrhost(var.virtual_machine_network_address, var.virtual_machine_ip_address_start + count.index)}" + ipv4_netmask = "${element(split("/", var.virtual_machine_network_address), 1)}" + } + + ipv4_gateway = "${var.virtual_machine_network_gateway}" + dns_suffix_list = ["${var.virtual_machine_domain}"] + dns_server_list = ["${cidrhost(var.virtual_machine_network_address, var.virtual_machine_ip_address_start+1)}", + "${cidrhost(var.virtual_machine_network_address, var.virtual_machine_ip_address_start+2)}"] + } + } +} diff --git a/terraform-esx-okd/modules/infra/06-node.tf b/terraform-esx-okd/modules/infra/06-node.tf new file mode 100644 index 0000000..5159a1e --- /dev/null +++ b/terraform-esx-okd/modules/infra/06-node.tf @@ -0,0 +1,48 @@ +resource "vsphere_virtual_machine" "node_vm" { + count = "${var.vm_numbers}" + name = "${var.name}-${count.index+1}" + resource_pool_id = "${data.vsphere_resource_pool.pool.id}" + datastore_id = "${data.vsphere_datastore.datastore.id}" + + num_cpus = 4 + memory = 8192 + guest_id = "${data.vsphere_virtual_machine.template_centos.guest_id}" + + scsi_type = "${data.vsphere_virtual_machine.template_centos.scsi_type}" + + network_interface { + network_id = "${data.vsphere_network.network.id}" + adapter_type = "${data.vsphere_virtual_machine.template_centos.network_interface_types[0]}" + } + + disk { + label = "disk0" + size = "${data.vsphere_virtual_machine.template_centos.disks.0.size}" + eagerly_scrub = "${data.vsphere_virtual_machine.template_centos.disks.0.eagerly_scrub}" + thin_provisioned = "${data.vsphere_virtual_machine.template_centos.disks.0.thin_provisioned}" + } + + cdrom { + client_device = true + } + + clone { + template_uuid = "${data.vsphere_virtual_machine.template_centos.id}" + customize { + linux_options { + host_name = "${var.name}${count.index+1}" + domain = "${var.virtual_machine_domain}" + } + + network_interface { + ipv4_address = "${cidrhost(var.virtual_machine_network_address, var.virtual_machine_ip_address_start + count.index+3)}" + ipv4_netmask = "${element(split("/", var.virtual_machine_network_address), 1)}" + } + + ipv4_gateway = "${var.virtual_machine_network_gateway}" + dns_suffix_list = ["${var.virtual_machine_domain}"] + dns_server_list = ["${cidrhost(var.virtual_machine_network_address, var.virtual_machine_ip_address_start+1)}", + "${cidrhost(var.virtual_machine_network_address, var.virtual_machine_ip_address_start+2)}"] + } + } +} diff --git a/terraform-esx-okd/modules/infra/99-output.tf b/terraform-esx-okd/modules/infra/99-output.tf new file mode 100644 index 0000000..2c3d35e --- /dev/null +++ b/terraform-esx-okd/modules/infra/99-output.tf @@ -0,0 +1,31 @@ +output "dns_server_ip" { + value = "${join(",", vsphere_virtual_machine.ns.*.default_ip_address)}" +} +output "master_ip" { + value = "${vsphere_virtual_machine.master.default_ip_address}" +} +output "node_ip" { + value = "${join(",", vsphere_virtual_machine.node_vm.*.default_ip_address)}" +} + +output "ns_ip_list" { + value = "${vsphere_virtual_machine.ns.*.default_ip_address}" +} + +output "master_ip_list" { + value = "${vsphere_virtual_machine.master.*.default_ip_address}" +} + +output "node_ip_list" { + value = "${vsphere_virtual_machine.node_vm.*.default_ip_address}" +} + +output "ns_hostname_list" { + value = "${vsphere_virtual_machine.ns.*.clone.0.customize.0.linux_options.0.host_name}" +} +output "master_hostname_list" { + value = "${vsphere_virtual_machine.master.*.clone.0.customize.0.linux_options.0.host_name}" +} +output "node_hostname_list" { + value = "${vsphere_virtual_machine.node_vm.*.clone.0.customize.0.linux_options.0.host_name}" +} diff --git a/terraform-esx-okd/modules/ns/00-vars.tf b/terraform-esx-okd/modules/ns/00-vars.tf new file mode 100644 index 0000000..a265923 --- /dev/null +++ b/terraform-esx-okd/modules/ns/00-vars.tf @@ -0,0 +1,22 @@ +variable "ns_ip_list" { + type = "list" +} +variable "ns_hostname_list" { + type = "list" +} +variable "master_ip_list" { + type = "list" +} +variable "master_hostname_list" { + type = "list" +} +variable "node_ip_list" { + type = "list" +} +variable "node_hostname_list" { + type = "list" +} + +variable "virtual_machine_domain" {} +variable "public_hostname" {} +variable "network_prefix" {} diff --git a/terraform-esx-okd/modules/ns/01-ns-a-record.tf b/terraform-esx-okd/modules/ns/01-ns-a-record.tf new file mode 100644 index 0000000..9815977 --- /dev/null +++ b/terraform-esx-okd/modules/ns/01-ns-a-record.tf @@ -0,0 +1,30 @@ +locals { + ips = "${concat(var.ns_ip_list, var.master_ip_list, var.node_ip_list)}" + hosts = "${concat(var.ns_hostname_list, var.master_hostname_list,var.node_hostname_list)}" +} + +data "template_file" "ns"{ + count = "${length(local.ips)}" + template = "${file("${path.module}/resources/tpl.json")}" + vars { + ips = "${element(local.ips, count.index)}" + hosts = "${element(local.hosts, count.index)}" + } +} + +data "template_file" "wrapper" { + template = <