Terraform null资源执行顺序

时间:2017-06-09 19:30:43

标签: terraform

问题:

我试图在Digital Ocean上构建一个Docker Swarm集群,由3" manager"组成。节点和许多工作节点。工作节点的数量与此问题并不特别相关。我试图对Docker Swarm配置模块进行模块化,因此它没有专门连接到digitalocean提供程序,而是可以接收一个ip地址列表来反对配置集群。

为了配置主节点,需要将第一个节点置于群集模式,这将生成其他主节点将用于加入第一个节点的连接密钥。 " null_resource" s用于对主节点执行远程配置器,但是,我无法弄清楚dafuq如何确保第一个主节点在完成另一个" null_resource"之前完成它的东西(" docker swarm init ...")。 provisioner针对需要加入第一个节点的其他主节点执行。它们都是平行运行的,并且可以预见,它不起作用。

此外,试图弄清楚如何收集第一个节点生成的join-token并使其可供其他节点使用。我考虑过与Consul这样做,并将连接令牌存储为密钥,并将该密钥存储在其他节点上 - 但这并不理想......因为确保Consul集群仍然存在问题已经准备好并准备就绪(这样的问题也是如此)。

main.tf

variable "master_count" { default = 3 }

# master nodes
resource "digitalocean_droplet" "master_nodes" {
  count               = "${var.master_count}"
  ... etc, etc
}

module "docker_master" {
  source          = "./docker/master"
  private_ip      = "${digitalocean_droplet.master_nodes.*.ipv4_address_private}"
  public_ip       = "${digitalocean_droplet.master_nodes.*.ipv4_address}"
  instances       = "${var.master_count}"
}

搬运工/主/ main.tf

variable "instances" {}
variable "private_ip" { type = "list" }
variable "public_ip" { type = "list" }


# Act only on the first item in the list of masters...
resource "null_resource" "swarm_master" {
  count = 1

  # Just to ensure this gets run every time
  triggers {
    version = "${timestamp()}"
  }

  connection {
    ...
    host = "${element(var.public_ip, 0)}"
  }

  provisioner "remote-exec" {
    inline = [<<EOF
      ... install docker, then ...

      docker swarm init --advertise-addr ${element(var.private_ip, 0)}

      MANAGER_JOIN_TOKEN=$(docker swarm join-token manager -q)
      # need to do something with the join token, like make it available
      # as an attribute for interpolation in the next "null_resource" block
    EOF
    ]
  }
}


# Act on the other 2 swarm master nodes (*not* the first one)
resource "null_resource" "other_swarm_masters" {
  count = "${var.instances - 1}"

  triggers {
    version = "${timestamp()}"
  }

  # Host key slices the 3-element IP list and excludes the first one
  connection {
    ...
    host = "${element(slice(var.public_ip, 1, length(var.public_ip)), count.index)}"
  }

  provisioner "remote-exec" {
    inline = [<<EOF
      SWARM_MASTER_JOIN_TOKEN=$(consul kv get docker/swarm/manager/join_token)
      docker swarm join --token ??? ${element(var.private_ip, 0)}:2377
    EOF
    ]
  }

  ##### THIS IS THE MEAT OF THE QUESTION ###
  # How do I make this "null_resource" block not run until the other one has
  # completed and generated the swarm token output? depends_on doesn't
  # seem to do it :(
}

从阅读github问题来看,我觉得这不是一个不常见的问题...但它踢我的屁股。任何建议表示赞赏!

1 个答案:

答案 0 :(得分:0)

@ victor-m的评论是正确的。如果您使用null_resource并在任何前者的属性上具有以下触发器,则它们将按顺序执行。

resource "null_resource" "first" {
  provisioner "local-exec" {
    command = "echo 'first' > first"
  }
}

resource "null_resource" "second" {
  triggers = {
    order = "${null_resource.first.id}"
  }

  provisioner "local-exec" {
    command = "echo 'second' >> first"
  }
}

resource "null_resource" "third" {
  triggers = {
    order = "${null_resource.second.id}"
  }

  provisioner "local-exec" {
    command = "echo 'third' >> first"
  }
}
$ terraform apply

null_resource.first: Creating...
null_resource.first: Provisioning with 'local-exec'...
null_resource.first (local-exec): Executing: ["/bin/sh" "-c" "echo 'first' > first"]
null_resource.first: Creation complete after 0s [id=3107778766090269290]
null_resource.second: Creating...
null_resource.second: Provisioning with 'local-exec'...
null_resource.second (local-exec): Executing: ["/bin/sh" "-c" "echo 'second' >> first"]
null_resource.second: Creation complete after 0s [id=3159896803213063900]
null_resource.third: Creating...
null_resource.third: Provisioning with 'local-exec'...
null_resource.third (local-exec): Executing: ["/bin/sh" "-c" "echo 'third' >> first"]
null_resource.third: Creation complete after 0s [id=6959717123480445161]

Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

为确定起见,请保存新文件,这是预期的输出结果

$ cat first
first
second
third