12/08/2018

SaltStack Pillar as Terraform external data source - Python

SaltStack is more than a configuration management tool, SaltStack Pillar could have a lot of data which may be needed somewhere else, e.g. Terraform!

So to avoid duplications, and after reading Terraform documentation "External Data Source", I wrote a Python script that works as external data source for Terraform. It simply reads from SaltStack Pillar, and returns the values in Terraform via "external" data source.

You can query any Pillar key from SaltStack and get the value in Terraform. And here we have 2 possibilities, this Pillar key has a string value, or it's a dictionary of strings (Terraform supports strings only as external data source values). But at the end all keys will be returned and accessible directly.

How to use

Let's have an example. So after clone the script, let's create Terraform conf:
cat << EOF > external_source.tf
data "external" "pillar" {
  program = ["python", "${path.module}/pillar_reader.py"]
  query = {
    # The value of this key is a string.
    baz = "foo:baz"
    # The value of this key is a dict of strings.
    foo_bar = "foo:bar"
  }
}
EOF
So if we have a Pillar as like this:
foo:
  baz: "value"
  bar:
    key01: "value01"
    key02: "value02"
Based on previous example, we can access the keys as following:
${data.external.pillar.result.baz}
${data.external.pillar.result.key01}
${data.external.pillar.result.key02}
${data.external.pillar.result.key03}

And that's it! It's a simple and easy bridge between SaltStack and Terraform!
More details are in the repo: terraform-external-data-source-saltstack-pillar.

Enjoy!

Powered by Blogger.

Hello, my name is Ahmed AbouZaid and this is my "lite" technical blog!

I'm a passionate DevOps engineer, Cloud/Kubernetes specialist, Free/Open source geek, and an author.

I believe in self CI/CD (Continuous Improvements/Development), also that "the whole is greater than the sum of its parts".

DevOps transformation, automation, data, and metrics are my preferred areas. And I like to help both businesses and people to grow.

Popular Posts