From 213960dec73da3c22069ba75b542eb487c7dc0d7 Mon Sep 17 00:00:00 2001 From: Ataxya Date: Mon, 25 Apr 2022 13:50:01 +0200 Subject: [PATCH] Initial commit --- LICENSE | 21 +++++++++ README.md | 19 ++++++++ cloud_config.tftpl | 5 ++ cloud_network_config.tftpl | 9 ++++ main.tf | 96 ++++++++++++++++++++++++++++++++++++++ passwd.auto.tfvars.example | 13 ++++++ playbooks/basicpackage.yml | 8 ++++ providers.tf | 31 ++++++++++++ var.tf | 95 +++++++++++++++++++++++++++++++++++++ variables.auto.tfvars | 29 ++++++++++++ 10 files changed, 326 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 cloud_config.tftpl create mode 100644 cloud_network_config.tftpl create mode 100644 main.tf create mode 100644 passwd.auto.tfvars.example create mode 100644 playbooks/basicpackage.yml create mode 100644 providers.tf create mode 100644 var.tf create mode 100644 variables.auto.tfvars diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..901b594 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 AtaxyaNetwork - Cécile MORANGE + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7340031 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +**Netbox + XO + Terraform + Ansible = The best deployment kit** + +How to run:
+cp passwd.auto.tfvars.example passwd.auto.tfvars
+replace all variable to match your infrastructure + +edit cloud_config.tftpl to add your ssh key
+edit cloud_network_config.tftpl to replace eth0 and your gateway (to be changed) + +create a prefix with IP you want to use on netbox
+edit variables.auto.tfvars + +You can add your playbook in the folder playbooks/ and use them in variables.auto.tfvars + +then + +terraform init
+terraform plan
+terraform apply diff --git a/cloud_config.tftpl b/cloud_config.tftpl new file mode 100644 index 0000000..b1c87b0 --- /dev/null +++ b/cloud_config.tftpl @@ -0,0 +1,5 @@ +#cloud-config +hostname: ${hostname} +ssh_authorized_keys: +${ssh_keys} + diff --git a/cloud_network_config.tftpl b/cloud_network_config.tftpl new file mode 100644 index 0000000..b088fd0 --- /dev/null +++ b/cloud_network_config.tftpl @@ -0,0 +1,9 @@ +#cloud-config +version: 1 +config: + - type: physical + name: eth0 + subnets: + - type: static + address: "${ip}" + gateway: "${gateway}" diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..644f4b0 --- /dev/null +++ b/main.tf @@ -0,0 +1,96 @@ +data "netbox_prefix" "test" { + cidr = var.netbox_prefix +} + +resource "netbox_available_ip_address" "test" { + prefix_id = data.netbox_prefix.test.id + description = var.name + dns_name = var.dns_name +} + +output "ip_addr" { + value = netbox_available_ip_address.test.ip_address +} + +data "xenorchestra_sr" "local_storage" { + name_label = var.storage +} + +data "xenorchestra_network" "network" { + name_label = var.network + pool_id = data.xenorchestra_pool.pool.id +} + + +data "xenorchestra_pool" "pool" { + name_label = var.pool +} + +data "xenorchestra_template" "template" { + name_label = var.template +} + + +resource "xenorchestra_vm" "bar" { + memory_max = var.ram * 1024 * 1024 * 1024 + cpus = var.cpu + cloud_config = templatefile("cloud_config.tftpl", { + hostname = var.name + ssh_keys = var.ssh_keys + }) + cloud_network_config = templatefile("cloud_network_config.tftpl", { + ip = "${replace(netbox_available_ip_address.test.ip_address, "var.netmask_netbox", var.netmask)}" + gateway = var.gateway + }) + name_label = var.name + name_description = var.desc + template = data.xenorchestra_template.template.id + + # Prefer to run the VM on the primary pool instance + affinity_host = data.xenorchestra_pool.pool.master + network { + network_id = data.xenorchestra_network.network.id + } + + disk { + sr_id = data.xenorchestra_sr.local_storage.id + name_label = var.name + size = var.disk_size * 1024 * 1024 * 1024 + } + + tags = [ + "Debian", + "Best Distro", + ] + + // Override the default create timeout from 5 mins to 20. + timeouts { + create = "20m" + } +} + + +# Generate inventory file +resource "local_file" "inventory" { + filename = "inventory" + content = <