How to set environment variables with Terraform provider

Hi,

I’m unable to set environment variables on containers using the terraform rancher provider. Is this a known issue or can anyone point me to a working example please?

Terraform v0.11.1, provider.rancher v1.2.0
Rancher|v1.6.15, Rancher Compose|v0.12.5|

sample terraform file below;

resource “rancher_stack” “test” {
name = "test
description = “test”
start_on_create = true
environment_id = “${rancher_environment.dev.id}”

docker_compose = <<EOF
version: ‘2’
services:
test:
image: ciaranfinnegan/frisbee
stdin_open: true
tty: true
ports:
- 80:3000/tcp
labels:
io.rancher.container.pull_image: always
io.rancher.scheduler.global: ‘true’
started: $STARTED
EOF

rancher_compose = <<EOF
version: ‘2’
services:
happy:
start_on_create: true
EOF

finish_upgrade = true

environment {
MONGO_URL = “mongodb://x:y@ds149743.mlab.com:49743/db01”
STARTED = “${timestamp()}”
}

}

Hi,

I’m currently working on a Terraform deployment, but not set env vars yet. I’ll have a test tonight and let you know how I get on.

Andy

Ah, sorry, I’ve approached it a different way:

First: create the host via terraform, including copying the pre-written docker-compose file:

 # Create the rancher host
resource "digitalocean_droplet" "rancher-server" {
  image         = "coreos-stable"
  name          = "rancher-server"
  region        = "lon1"
  size          = "2gb"
  ssh_keys      = ["${digitalocean_ssh_key.terraform_local.id}"]

  tags   = ["${digitalocean_tag.core.id}","${digitalocean_tag.rancher.id}"]

  # Copy the docker-compose files
  provisioner "file" {
    source      = "./files/docker-compose.yml"
    destination = "~/docker-compose.yml"

    connection {
        type     = "ssh"
        user     = "core"
        private_key = "${file("${var.local_ssh_key_path_private}")}"
      }
  }
}

Docker-compose is as standard:

version: "2"
services:
  rancher:
    container_name: rancher-server
    image: rancher/server:v1.6.15
    ports:
       - "8080:8080"
    volumes:
      - x:y
    environment:
      - NAME=value
      - NAME2=value2
    restart: unless-stopped

I’m then using ansible for a few bits that couldn’t get working via terraform due to permission issues. I’ve just stuck the “docker-compose up -d” command as another step for ansible to execute, as it also installs docker-compose itself on the coreos host.

Hope that helps,
Andy

1 Like