From 090ecaef1e5b9293c97ca2d3c57afc9bf5928bcb Mon Sep 17 00:00:00 2001 From: Markus Katharina Brechtel Date: Sun, 5 Jul 2020 02:32:44 +0200 Subject: [PATCH] new deployment code --- .gitignore | 8 +++++--- Taskfile.yml | 12 ++++++++++++ common.yaml | 4 +--- deploy.yaml | 19 +++++++++++++++++++ etcd.tf | 7 ------- inventory | 1 + ansible_inventory.tf => inventory.tf | 24 ++++++++++++++++++++++-- inventory_input.tf | 19 ------------------- 8 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 Taskfile.yml create mode 100644 deploy.yaml delete mode 100644 etcd.tf create mode 100644 inventory rename ansible_inventory.tf => inventory.tf (77%) delete mode 100644 inventory_input.tf diff --git a/.gitignore b/.gitignore index 720e01e..e3cb315 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ .terraform -*.auto.tfvars -*.tfstate -*.tfstate.backup +terraform-info.json +provider_hetzner.auto.tfvars inventory.json +terraform.tfstate +terraform.tfstate.backup +inventory.auto.tfvars.json diff --git a/Taskfile.yml b/Taskfile.yml new file mode 100644 index 0000000..caec003 --- /dev/null +++ b/Taskfile.yml @@ -0,0 +1,12 @@ +version: '2' + +tasks: + default: + cmds: + - ansible-playbook -i inventory -i inventory.json deploy.yaml + - terraform init + - terraform apply -auto-approve + - ansible-playbook -i inventory -i inventory.json common.yaml + wordpress: + cmds: + - ansible-playbook -i inventory -i inventory.json wordpress.yaml diff --git a/common.yaml b/common.yaml index 3ad8528..e267a8c 100644 --- a/common.yaml +++ b/common.yaml @@ -1,8 +1,6 @@ --- -- hosts: - - controllers - - nameservers +- hosts: vmst remote_user: root roles: - role: common diff --git a/deploy.yaml b/deploy.yaml new file mode 100644 index 0000000..49f0890 --- /dev/null +++ b/deploy.yaml @@ -0,0 +1,19 @@ +--- + +- hosts: localhost + vars: + vm_hosts: "{ + {% for hostname in groups.vms %} + {{hostname|to_json}}: {{ + { + 'provider': hostvars[hostname].provider, + 'server_type': hostvars[hostname].server_type, + } | to_json }}, + {% endfor %} }" + tasks: + - name: terraform inventory vars + copy: + content: + vm_hosts: "{{ vm_hosts }}" + groups: {} + dest: "{{playbook_dir}}/inventory.auto.tfvars.json" diff --git a/etcd.tf b/etcd.tf deleted file mode 100644 index 86d61da..0000000 --- a/etcd.tf +++ /dev/null @@ -1,7 +0,0 @@ -#terraform { -# backend "etcdv3" { -# endpoints = ["localhost:2379"] -# lock = true -# prefix = "testing/terraform-state/" -# } -#} diff --git a/inventory b/inventory new file mode 100644 index 0000000..c5c9834 --- /dev/null +++ b/inventory @@ -0,0 +1 @@ +[vms] diff --git a/ansible_inventory.tf b/inventory.tf similarity index 77% rename from ansible_inventory.tf rename to inventory.tf index c4a17b2..6b28948 100644 --- a/ansible_inventory.tf +++ b/inventory.tf @@ -1,3 +1,23 @@ +variable "vm_hosts" { + default = { + #"hostname.example.com" = { + # provider = "hetzner" + # groups = [ "testgroup" ] + # volumes = { + # data = { size = 32 } + # } + #} + } +} + +variable "groups" { + default = { + #testgroup = { + # testvar1 = "bla" + #} + } +} + variable "ansible_inventory_filename" { type = string default = "inventory.json" @@ -28,7 +48,7 @@ locals { },{ for providername, provider in local.providers: "provider_${providername}" => { hosts = { - for hostname, host in local.vm_hosts_with_groups: + for hostname, host in var.vm_hosts: hostname => local.providers[host.provider].hostvars[hostname] if host.provider == providername } @@ -43,7 +63,7 @@ output "ansible_inventory" { value = local.ansible_inventory } -resource "local_file" "foo" { +resource "local_file" "ansible_inventory" { content = jsonencode(local.ansible_inventory) filename = var.ansible_inventory_filename file_permission = "0644" diff --git a/inventory_input.tf b/inventory_input.tf deleted file mode 100644 index 9092006..0000000 --- a/inventory_input.tf +++ /dev/null @@ -1,19 +0,0 @@ -variable "vm_hosts" { - default = { - "controller.testing.thengo.net" = { - provider = "hetzner" - groups = [ "controllers" ] - #volumes = { - # data = { size = 32 } - #} - } - #"ns.testing.thengo.net" = { provider = "hetzner", groups = [ "nameservers" ] } - } -} - -variable "groups" { - default = { - reverse_proxies = { - } - } -}