Skip to content

Commit 64cedcc

Browse files
authored
Read k3s_token from secrets.yml file (#540)
* read k3s_secret from secrets.yml * override secrets path for stackhpc terraform * remove templating of k3s_secret to terraform dir * remove k3s_secret var from stackhpc env * clarify stackhpc override for tf secrets in stackhpc
1 parent 5f7e48f commit 64cedcc

File tree

6 files changed

+68
-24
lines changed

6 files changed

+68
-24
lines changed

ansible/roles/passwords/tasks/main.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,3 @@
66
dest: "{{ openhpc_passwords_output_path }}"
77
delegate_to: localhost
88
run_once: true
9-
10-
- name: Get templated passwords from target environment
11-
# inventory group/host vars created in a play cannot be accessed in the same play, even after meta: refresh_inventory
12-
ansible.builtin.include_vars:
13-
file: "{{ openhpc_passwords_output_path }}"
14-
15-
- name: Template k3s token to terraform
16-
template:
17-
src: k3s-token.auto.tfvars.json.j2
18-
dest: "{{ lookup('env', 'APPLIANCES_ENVIRONMENT_ROOT') }}/terraform/k3s-token.auto.tfvars.json"
19-
delegate_to: localhost
20-
run_once: true

environments/.stackhpc/terraform/main.tf

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ variable "volume_backed_instances" {
5454
default = false
5555
}
5656

57-
variable "k3s_token" {
58-
type = string
59-
}
60-
6157
data "openstack_images_image_v2" "cluster" {
6258
name = var.cluster_image[var.os_version]
6359
most_recent = true
@@ -73,7 +69,9 @@ module "cluster" {
7369
key_pair = "slurm-app-ci"
7470
cluster_image_id = data.openstack_images_image_v2.cluster.id
7571
control_node_flavor = var.control_node_flavor
76-
k3s_token = var.k3s_token
72+
# have to override default, as unusually the actual module path and secrets
73+
# are not in the same environment for stackhpc
74+
inventory_secrets_path = "${path.module}/../inventory/group_vars/all/secrets.yml"
7775

7876
login_nodes = {
7977
login-0: var.other_node_flavor

environments/skeleton/{{cookiecutter.environment}}/terraform/compute.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module "compute" {
2424

2525
key_pair = var.key_pair
2626
environment_root = var.environment_root
27-
k3s_token = var.k3s_token
27+
k3s_token = local.k3s_token
2828
control_address = [for n in openstack_compute_instance_v2.control["control"].network: n.fixed_ip_v4 if n.access_network][0]
2929
security_group_ids = [for o in data.openstack_networking_secgroup_v2.nonlogin: o.id]
3030
}

environments/skeleton/{{cookiecutter.environment}}/terraform/nodes.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ resource "openstack_compute_instance_v2" "control" {
7676

7777
metadata = {
7878
environment_root = var.environment_root
79-
k3s_token = var.k3s_token
79+
k3s_token = local.k3s_token
8080
}
8181

8282
user_data = <<-EOF
@@ -125,7 +125,7 @@ resource "openstack_compute_instance_v2" "login" {
125125

126126
metadata = {
127127
environment_root = var.environment_root
128-
k3s_token = var.k3s_token
128+
k3s_token = local.k3s_token
129129
control_address = [for n in openstack_compute_instance_v2.control["control"].network: n.fixed_ip_v4 if n.access_network][0]
130130
}
131131

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
""" opentofu external data program to load inventory string variables from
3+
a (possibly vault-encrypted) secrets file.
4+
5+
Example usage:
6+
7+
data "external" "example" {
8+
program = [this_file]
9+
10+
query = {
11+
path = "${path.module}/../inventory/group_vars/all/secrets.yml"
12+
}
13+
}
14+
15+
The external data resource's result attribute then contains a mapping of
16+
variable names to values.
17+
18+
NB: Only keys/values where values are strings are returned, in line with
19+
the external program protocol.
20+
21+
NB: This approach is better than e.g. templating inventory vars as the
22+
inventory doesn't need to be valid, which is helpful when opentofu will
23+
template out hosts/groups.
24+
"""
25+
26+
import sys, json, subprocess, yaml
27+
input = sys.stdin.read()
28+
secrets_path = json.loads(input)['path']
29+
30+
with open(secrets_path) as f:
31+
header = f.readline()
32+
if header.startswith('$ANSIBLE_VAULT'):
33+
cmd = ['ansible-vault', 'view', secrets_path]
34+
ansible = subprocess.run(cmd, capture_output=True, text=True)
35+
contents = ansible.stdout
36+
else:
37+
contents = f.read()
38+
39+
data = yaml.safe_load(contents)
40+
41+
output = {}
42+
for k, v in data.items():
43+
if isinstance(v, str):
44+
output[k] = v
45+
print(json.dumps(output))

environments/skeleton/{{cookiecutter.environment}}/terraform/variables.tf

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,20 @@ variable "root_volume_size" {
140140
default = 40
141141
}
142142

143-
variable "k3s_token" {
144-
description = "K3s cluster authentication token, set automatically by Ansible"
145-
type = string
146-
}
143+
variable "inventory_secrets_path" {
144+
description = "Path to inventory secrets.yml file. Default is standard cookiecutter location."
145+
type = string
146+
default = ""
147+
}
148+
149+
data "external" "inventory_secrets" {
150+
program = ["${path.module}/read-inventory-secrets.py"]
151+
152+
query = {
153+
path = var.inventory_secrets_path == "" ? "${path.module}/../inventory/group_vars/all/secrets.yml" : var.inventory_secrets_path
154+
}
155+
}
156+
157+
locals {
158+
k3s_token = data.external.inventory_secrets.result["vault_k3s_token"]
159+
}

0 commit comments

Comments
 (0)