diff --git a/templates/variables.tf.j2 b/templates/terraform/variables.tf.j2 similarity index 100% rename from templates/variables.tf.j2 rename to templates/terraform/variables.tf.j2 diff --git a/terraform/cluster.tf b/terraform/cluster.tf new file mode 100644 index 0000000..b12d428 --- /dev/null +++ b/terraform/cluster.tf @@ -0,0 +1,38 @@ +resource "random_shuffle" "netology-gw-subnet-random" { + input = [yandex_vpc_subnet.netology-gw-subnet-a.id, yandex_vpc_subnet.netology-gw-subnet-b.id] + result_count = 1 +} + +resource "yandex_compute_instance" "k8s-cluster" { + for_each = toset(["control", "node01", "node2"]) + + name = each.key + + resources { + cores = 2 + memory = 2 + } + + boot_disk { + initialize_params { + image_id = "fd8kdq6d0p8sij7h5qe3" # ubuntu-20-04-lts-v20220822 + size = "20" + } + } + + network_interface { + subnet_id = random_shuffle.netology-gw-subnet-random.result + nat = true + } + + metadata = { + ssh-keys = "ubuntu:${file("~/.ssh/id_rsa.pub")}" + } +} + +output "cluster_ips" { + value = { + internal = values(yandex_compute_instance.k8s-cluster)[*].network_interface.0.ip_address + external = values(yandex_compute_instance.k8s-cluster)[*].network_interface.0.nat_ip_address + } +} diff --git a/terraform_init.yml b/terraform_init.yml index 1b49292..6cced87 100644 --- a/terraform_init.yml +++ b/terraform_init.yml @@ -41,19 +41,20 @@ - "'AlreadyExists' not in bucket_create_result.stderr" - name: Terraform - Create variables.tf ansible.builtin.template: - src: "templates/variables.tf.j2" - dest: "terraform/variables.tf" + src: "{{ playbook_dir }}/templates/terraform/variables.tf.j2" + dest: "{{ playbook_dir }}/terraform/variables.tf" - name: Terraform - init ansible.builtin.command: - chdir: ./terraform + chdir: "{{ playbook_dir }}/terraform" cmd: >- terraform init + -reconfigure -backend-config="bucket={{ terraform_yandex_bucket_name }}" -backend-config="access_key={{ service_account_key_id }}" -backend-config="secret_key={{ service_account_secret }}" - name: Terraform - create workspace ansible.builtin.command: - chdir: ./terraform + chdir: "{{ playbook_dir }}/terraform" cmd: terraform workspace new prod register: terraform_new_workspace_result failed_when: