1
votes

I have a use case where i am provisioning a GOMaster and GOServer using terraform and then registering goserver with newly provisioned gomaster.

Following is the terraform recipe i am using. Terraform is also installed on docker server itself and i am executing this recipe from there.

#Configure the Docker provider
    provider "docker" {
        host = "tcp://${var.dockerIP}:2376/"
    }

# Create the network
    resource "docker_network" "private_network" {
        name = "${var.customer_name}_network"
    }

     resource "docker_container" "goagent" {
         image = "${docker_image.goagent.latest}"
         name = "${var.customer_name}_goagent"
         hostname = "${var.customer_name}_goagent"
         command = [ "/bin/sh", "-c", "/usr/bin/supervisord" ]
         network_mode = "bridge"
         networks = [ "${docker_network.private_network.name}" ]
         provisioner "remote-exec" {
             connection {
                 user = "root"
                 password = "example"
             }
             inline = [
                  "sed -i -e 's/GO_SERVER=go-server/GO_SERVER=${var.customer_name}_goserver' /etc/defaut/go-agent",
                  "sed -i -e 's/DAEMON=N/DAEMON=Y' /etc/defaut/go-agent",
                  "/etc/init.d/go-agent restart"
             ]
        }
     }

# Create a container
    resource "docker_container" "goservertest" {
    image = "${docker_image.goserver.latest}"
    name = "${var.customer_name}_goserver"
    hostname = "${var.customer_name}_goserver"
    network_mode = "bridge"
    networks = [ "${docker_network.private_network.name}" ]
    ports{
        internal = "8153"
    external = "8003"
    }
    ports{
    internal = "8154"
        external = "8002"    
    }
}

resource "docker_image" "goserver" {
    name = "gocd/gocd-server:latest"
}

resource "docker_image" "goagent" {
name = "gocd/gocd-agent:latest"
}

Remote exec part in goagent resource is not working. My assumption was that it logs in to provisioned goagent and modifies the files in that according to sed commands specified. But instead it is doing ssh to docker host and modifying files. Am i missing something? DO we have to specify ipaddress or hostname in connection block for the remote device?

1
what is the terraform version ? what is the output for the remote_exec ? - darthShadow

1 Answers

0
votes

If you want this to apply to the provisioned goagent host and not to the docker container then remove the provisional block from resource "docker_container" and move it to the resource area where your goagent host was provisioned.

https://www.terraform.io/docs/provisioners/connection.html