ChrisAdkin
ChrisAdkin

Reputation: 1326

guidance on how to use a template with complex YAML files that can change

I have a Terraform config that essentially wraps Kubespray, basically a set of Ansible playbooks. Much of the Kubernetes cluster configuration is stored in YAML files. In some respects embedding calls to things such as Perl in provisioners would be the easiest way to substitute variables into these files. This leaves things such as the Terraform template function, and I have done things such as taken YAML files, k8s-cluster.yml for example and turned it into a template file, the problem with this is that if the YAML file changes in the upstream GitHub repo for Kubespray I have to recreate the template file, which is not a brilliant way of doing things. Presuming that other people must have faced this issue, what is the most elegant way of dealing with YAML configurations that can change in this way ?

Upvotes: 1

Views: 225

Answers (1)

ChrisAdkin
ChrisAdkin

Reputation: 1326

The best possible solution I have come up with to date is this, replacing the hard coding with variables is advisable, but it works:

data "http" "k8s_cluster_yml" {
  url = "https://raw.githubusercontent.com/kubernetes-sigs/kubespray/master/inventory/sample/group_vars/k8s_cluster/k8s-cluster.yml"
}

resource "local_file" "k8s_cluster_yml" {
  content  = replace(data.http.k8s_cluster_yml.body, "/kube_version: v[0-9].[0-9][0-9].[0-9]/", "kube_version: v1.22.5")
  filename = "./k8s-cluster.yaml"
}

Upvotes: 1

Related Questions