Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

279
Visualizações
Can Terraform set a variable from a remote_exec command?

I'm trying to build a Docker Swarm cluster in AWS using Terraform. I've successfully got a Swarm manager started, but I'm trying to work out how best to pass the join key to the workers (which will be created after the manager).

I'd like some way of running the docker swarm join-token worker -q command that can be set to a Terraform variable. That way, the workers can have a remote_exec command something like docker swarm join ${var.swarm_token} ${aws_instance.swarm-manager.private_ip}

How can I do this?

My config is below:

resource "aws_instance" "swarm-manager" {
  ami = "${var.manager_ami}"
  instance_type = "${var.manager_instance}"
  tags = {
    Name = "swarm-manager${count.index + 1}"
  }

  provisioner "remote-exec" {
    inline = [
      "sleep 30",
      "docker swarm init --advertise-addr ${aws_instance.swarm-manager.private_ip}"
      "docker swarm join-token worker -q" // This is the value I want to store as a variable/output/etc
    ]
  }
}

Thanks

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can use an external data source in supplement to your remote provisioning script.

This can shell into your swarm managers and get the token after they are provisioned.

If you have N swarm managers, you'll probably have to do it all at once after the managers are created. External data sources return a map of plain strings, so using keys that enable you to select the right result for each node is required, or return the whole set as a delimited string, and use element() and split() to get the right item.



resource "aws_instance" "swarm_manager" {
  ami = "${var.manager_ami}"
  instance_type = "${var.manager_instance}"
  tags = {
    Name = "swarm-manager${count.index + 1}"
  }
  provisioner "remote-exec" {
    inline = [
      "sleep 30",
      "docker swarm init --advertise-addr ${aws_instance.swarm-manager.private_ip}"
    ]
  }
}

data "external" "swarm_token" {
  program = ["bash", "${path.module}/get_swarm_tokens.sh"]
  query = {
    swarms = ["${aws_instance.swarm_manager.*.private_ip}"]
  }
}

resource "aws_instance" "swarm_node" {
  count = "${var.swarm_size}"
  ami = "${var.node_ami}"
  tags = {
    Name = "swarm-node-${count.index}"
  }
  provisioner "remote-exec" {
    inline = [
      "# Enrol me in the right swarm, distributed over swarms available",
      "./enrol.sh ${element(split("|", data.swarm_token.result.tokens), count.index)}"
    ]
  }
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda