Skip to content

Commit

Permalink
added OCI support and version bumped providers
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-p committed Mar 13, 2024
1 parent 279fd6a commit 726feb8
Show file tree
Hide file tree
Showing 34 changed files with 539 additions and 128 deletions.
36 changes: 20 additions & 16 deletions ansible/defaults/template_info.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ sshkey_folder: /home/justin-p/.ssh/
## For all the options you can define per host look at the terraform.tfvars.j2 template.
## For example you overwrite the size of the VM or the OS by adding it to this map.
#
# host_list: {
# digitalocean: [
# { "name": "host01",
# "tags": "[\"nginx\"]"
# },
# { "name": "host02",
# "tags": "[\"mysql\"]"
# },
# ],
# hetzner: [
# { "name": "host03",
# "labels": "{postfix = \"\"}"
# }
# ]
# }
#
#host_list: {
# digitalocean: [
# { "name": "host01",
# "tags": "[\"nginx\"]"
# },
# { "name": "host02",
# "tags": "[\"mysql\"]"
# },
# ],
# hetzner: [
# { "name": "host03",
# "labels": "{postfix = \"\"}"
# }
# ],
# oci: [
# { "name": "host04",
# "freeform_tags": "{postfix = \"\"}"
# },
# ]
#}

host_list: {
digitalocean: [
Expand Down
44 changes: 27 additions & 17 deletions ansible/defaults/template_info.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
---
project_name: project_name
sshkey_folder: /home/user/.ssh/
sshkey_folder: /home/justin-p/.ssh/
## Example on how to define a host_list map to create multiple hosts and/or create hosts on multiple providers.
## Tags/lables will be used as ansible groups so you can easily apply certain playbooks/roles against specific hosts.
## This means that the host will be added ansible groups with the same name of the tags/label added below.
## For all the options you can define per host look at the terraform.tfvars.j2 template.
## For example you overwrite the size of the VM or the OS by adding it to this map.
#
# host_list: {
# digitalocean: [
# { "name": "host01",
# "tags": "[\"nginx\"]"
# },
# { "name": "host02",
# "tags": "[\"mysql\"]"
# },
# ],
# hetzner: [
# { "name": "host03",
# "labels": "{postfix = \"\"}"
# }
# ]
# }
#
#host_list: {
# digitalocean: [
# { "name": "host01",
# "tags": "[\"nginx\"]"
# },
# { "name": "host02",
# "tags": "[\"mysql\"]"
# },
# ],
# hetzner: [
# { "name": "host03",
# "labels": "{postfix = \"\"}"
# },
# { "name": "host04",
# "labels": "{postfix = \"\"}"
# },
# ],
# oci: [
# { "name": "host05",
# "freeform_tags": "{postfix = \"\"}"
# },
# { "name": "host06",
# "freeform_tags": "{postfix = \"\"}"
# },
# ]
#}

host_list: {
digitalocean: [
Expand Down
1 change: 1 addition & 0 deletions ansible/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- name: Bootstrap host with bare bones config
ansible.builtin.import_tasks: tasks/bootstrap/main.yml
tags: ['never', 'create', 'bootstrap']
become: true

## add your plays/tasks here
- hosts: all:or_use_a_group_name
Expand Down
12 changes: 12 additions & 0 deletions ansible/playbooks/terraform/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@
with_dict: "{{ terraform_infra.outputs['hetzner_vms'].value }}"
when: terraform_infra.outputs['hetzner_vms'].value['you_should_never_use_this_host_name']['vm'] is not defined

- name: Add each deployed oci host to ansible inventory
ansible.builtin.add_host:
hostname: "{{ item.value['vm'][0]['display_name'] }}"
ansible_host: "{{ item.value['vm'][0]['public_ip'] }}"
groups: "{{ item.value['vm'][0]['freeform_tags'].keys() }}"
cloudprovider: oci
ipv4: "{{ item.value['vm'][0]['public_ip'] }}"
group: "{{ item.value['vm'][0]['freeform_tags'].keys() }}"
changed_when: false
with_dict: "{{ terraform_infra.outputs['oci_vms'].value }}"
when: terraform_infra.outputs['oci_vms'].value['you_should_never_use_this_host_name']['vm'] is not defined

post_tasks:
- name: Remove 'terraform.tfvars' file
ansible.builtin.file:
Expand Down
39 changes: 38 additions & 1 deletion ansible/templates/terraform.tfvars.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
project_name = "{{ project_name }}"
root_ssh_key_path = "{{ root_private_key_path }}"
root_ssh_private_key_path = "{{ root_private_key_path }}"
root_ssh_public_key_path = "{{ root_public_key_path }}"

{% if tokens['digitalocean'] is defined %}digitalocean_token = "{{ tokens['digitalocean'] }}"
{% endif %}
Expand Down Expand Up @@ -84,3 +85,39 @@ hetzner_servers = {
}
{% endif %}
}

{% if host_list['oci'][0]['name'] is defined %}oci_enabled = true
{% endif %}
{% if host_list['oci'][0]['name'] is not defined %}oci_enabled = false
{% endif %}

oci_servers = {
{% if host_list['oci'][0]['name'] is defined %} {% for dict_item in host_list['oci'] %}
"{{dict_item['name']}}" = {
{% if 'availability_domain' in dict_item %} availability_domain = "{{dict_item['availability_domain']}}"
{% endif %}
{% if 'instance_display_name' in dict_item %} instance_display_name = "{{dict_item['instance_display_name']}}"
{% endif %}
{% if 'shape' in dict_item %} shape = "{{dict_item['shape']}}"
{% endif %}
{% if 'freeform_tags' in dict_item %} freeform_tags = {{dict_item['freeform_tags']}}
{% endif %}
{% if 'source_id' in dict_item %} source_id = "{{dict_item['source_id']}}"
{% endif %}
{% if 'source_type' in dict_item %} source_type = "{{dict_item['source_type']}}"
{% endif %}
{% if loop.nextitem is not defined %}
}
{% endif %}
{% if loop.nextitem is defined %}
},
{% endif %}
{% endfor %}{% endif %}

{% if host_list['oci'][0]['name'] is not defined %}
"you_should_never_use_this_host_name" = {
name = "you_should_never_use_this_host_name"
labels = {ansible = ""}
}
{% endif %}
}
143 changes: 85 additions & 58 deletions terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions terraform/digitalocean.tf
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ module "digitalocean_ssh_key" {
source = "./digitalocean/ssh_key"
module_enabled = var.digitalocean_enabled

root_username = var.root_username
root_ssh_key_path = var.root_ssh_key_path
root_username = var.root_username
root_ssh_private_key_path = var.root_ssh_private_key_path
}

module "digitalocean_vm" {
source = "./digitalocean/vm"
module_enabled = var.digitalocean_enabled
for_each = var.digitalocean_servers

root_username = var.root_username
root_ssh_key_path = var.root_ssh_key_path
root_username = var.root_username
root_ssh_private_key_path = var.root_ssh_private_key_path

server_hostname = each.value.hostname
server_tags = each.value.tags
Expand Down
2 changes: 1 addition & 1 deletion terraform/digitalocean/project/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.19.0"
version = "~> 2.36.0"
}
}
required_version = ">= 1.0.8"
Expand Down
2 changes: 1 addition & 1 deletion terraform/digitalocean/project/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ variable "project_name" {

variable "project_description" {
description = "Description of the new to the DigitalOcean Project"
default = "Server deployed with Terraform and Ansible template"
default = "Server deployed with Anster"
}

variable "digitalocean_droplets" {
Expand Down
2 changes: 1 addition & 1 deletion terraform/digitalocean/ssh_key/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ resource "digitalocean_ssh_key" "main" {
count = var.module_enabled ? 1 : 0 # only run if this variable is true

name = "${var.project_name}-${var.root_username}"
public_key = file("${var.root_ssh_key_path}.pub")
public_key = file("${var.root_ssh_private_key_path}.pub")
}
2 changes: 1 addition & 1 deletion terraform/digitalocean/ssh_key/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.19.0"
version = "~> 2.36.0"
}
}
required_version = ">= 1.0.8"
Expand Down
Loading

0 comments on commit 726feb8

Please sign in to comment.