When using terraform to create a swarm on DigitalOcean, how can I pass the join-token to a worker node? I can create a simple 2-node swarm; a manager node and a worker node; but there does not seem to be an efficient way to pass the join-token to the worker node:

*Code snippet manager node*
provisioner "remote-exec" {
     inline = [
      "sudo ufw allow 2377,7946/tcp",
      "sudo ufw allow 4789,7946/udp",
      "sudo docker swarm init --advertise-addr ${digitalocean_droplet.manager1.ipv4_address_private}"
      ]
  }

*Code snippet worker node*
    provisioner "remote-exec" {
     inline = [
      "sudo ufw allow 2377,7946/tcp",
      "sudo ufw allow 4789,7946/udp",
      "docker swarm join --token ${var.worker_token} ${digitalocean_droplet.manager1.ipv4_address_private}:2377"
      ]
  }

If I issue this command: docker swarm join-token -q worker I can display the token, but cannot save it to the variable ${var.worker_token}
Example: “docker swarm join-token worker -q > ${var.worker_token}”

Does anyone have some insight on how to assign the output of a command to a variable and reference it later in the terraform plan?

Thank you

edited by bobbyiliev
Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!