Coder Social home page Coder Social logo

terraform-salt's Introduction

About

This script allows terraform to connect to a SaltStack master and generate keys, prior to launching the hosts so that the keys can be injected into cloud-init.

Example

  • Create a unix account on the SaltStack master called terraform-salt.
  • Allow it to be used to auth on the master:
external_auth:      
  pam:
    terraform-salt:
      - '@wheel'
  • Update the pam_user & pam_passwd variables in salt_generate_key.py
  • Enable the REST CherryPy API:
rest_cherrypy:
  port: 8080
  ssl_crt: /usr/local/etc/ssl/corp/cert.pem
  ssl_key: /usr/local/etc/ssl/corp/key.pem
  • Restart the SaltStack Master
  • Restart the SaltStack API
  • In terraform add a external data source for creating VMs:
data "external" "salt-key" {
  program = ["python", "salt_generate_key.py"]
  query = {
    host = var.hostname
    site = var.site
  }
}
  • In terraform add a provisioner to the VMs as part of the vm resource for cleaning up:
  provisioner "local-exec" {
    command = "python salt_generate_key.py -d ${self.name_label}"
    when = destroy
  }
  • In terraform add a data block for the template file that cloud-init will use. Note that this supports overrides for specific hostnames.
data "template_file" "cloudinit" {
  template = fileexists( join("", ["cloud-config-", replace(var.hostname, "/\\d+$/", ""), ".tpl"] ) ) ? join("", [ file("cloud-config.tpl"), file( join("", ["cloud-config-", replace(var.hostname, "/\\d+$/", ""), ".tpl"] ) ) ] ) : file("cloud-config.tpl")
  vars = {
    hostname = var.hostname
    site     = var.site
    salt_private_key = data.external.salt-key.result.salt_private_key
    salt_public_key  = data.external.salt-key.result.salt_public_key
  }
}
  • Create the cloud-init template file in the top level of the terraform directory: cloud-config.tpl:
#cloud-config
hostname: ${hostname}.${site}.internal
salt_minion:
  conf:
    log_level_logfile: info
    startup_states: highstate
  grains:
    site: ${site}
  public_key: |
${salt_public_key}
  private_key: |
${salt_private_key}
  • In terraform pass in the cloudinit template file to the VM:
    • For XenOrchestra:
      resource "xenorchestra_vm" "test" {
        ...
        cloud_config = data.template_file.cloudinit.rendered
      }
      
    • For VMWare vsphere:
      resource vsphere_virtual_machine "test" {
        ...
        extra_config = {
          "guestinfo.metadata"          = base64encode(data.template_file.cloudinit.rendered)
          "guestinfo.metadata.encoding" = "base64"
        }
      }
      

terraform-salt's People

Contributors

brd avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.