In a previous post I had covered how to deploy applications to a Kubernetes cluster using Terraform scripts.
I found Terraform scripts to be very useful in deploying resources
to a Kubernetes cluster. Terraform uses blocks of code to declare
cluster resources and specify configurations for the resources.
Terraform also provides a set of plugins which can be used to
provision multiple providers (Kubernetes, AWS, Azure, GCP, etc.).
Drawbacks Using Terraform
I'm in the process of using scripts to provision resources and build-out a Kubernetes cluster.
As part of the cluster build-out, certain packages needed to be
installed to each node that was going to exist on the cluster.
I had previously outlined a manual process on how to build out the cluster.
This process has since moved into a pattern defined as configuration-as-code.
Additionally, certain namespaces needed to be created and populated before others could be rolled out.
Specifying dependencies using Terraform alone could get messy.
Ansible to the Rescue
I had previously worked with Ansible in past home-lab projects. After a little bit of research, I found that Ansible was capable of running Terraform scripts.
Using the Terraform Ansible module, it is possible to define the Terraform project files as demonstrated below:
terraform.tf
provider "kubernetes" {}
provider "helm" {}
ingress.tf
resource "kubernetes_namespace" "ns_kong" {
metadata {
name = "kong"
}
}
resource "helm_release" "rel_kong_ingress" {
repository = "https://charts.konghq.com/"
chart = "kong"
name = "kong"
namespace = "kong"
}
These files can be run inside of an Ansible role using the following Ansible script:
ingress/tasks/main.yml
---
- name: Prepare Terraform Scripts
environment:
KUBECONFIG: "/home/{{ ansible_user }}/kubeconfig"
block:
- copy: src="{{ item }}" dest=terraform/ingress
with_items:
- terraform.tf
- ingress.tf
- terraform:
project_path: terraform/ingress
state: planned
plan_file: plan.out
- terraform:
project_path: terraform/ingress
state: present
plan_file: plan.out
Additionally, variables can be specified to Terraform using a map under the variables
parameter from the Terraform module:
- name: Deploy Terraform
terraform:
...
variables:
tf_variable: "ansible-value"
Top comments (1)
I'd be interested if there was a way to call it from a deployment script. i.e. Ansible plugin.