0

我是 terraform 的新手,但我创建了一个这样的 openstack 计算实例:

provider "openstack" {
  auth_url    = "https://my-auth/v2.0/" 
  domain_name = "default"                         
  alias       = "alias"                              
  user_name   = "username"
  tenant_name = "tenantname"
  password    = "pwd"
  region      = "region"
}

# Import SSH key pair into openstack project
resource "openstack_compute_keypair_v2" "keypair" {
  provider   = "myprovider"               
  name       = "keypair"                 
  public_key = "${file("~/.ssh/id_rsa.pub")}"
}

# Create a new virtual machine
resource "openstack_compute_instance_v2" "compute_instance" {
  name        = "compute_instance" # Instance Name
  provider    = "myprovider"       # Instance distr
  image_name  = "Centos 7"         # Image name
  flavor_name = "b2-7"             # Machine type name

  key_pair = "${openstack_compute_keypair_v2.keypair.name}"

  network {
    name = "Ext-Net"
  }
}

出于可维护性和灵活性的原因,我想在同一个实例中添加一些“组件”,它可以是任何东西,但在这里我尝试了一个配置文件和远程执行。事实上,当我在我的计算实例中添加这个参数时,我注意到我的计算实例不会被更新。例如:

provider "openstack" {
  auth_url    = "https://my-auth/v2.0/" 
  domain_name = "default"                         
  alias       = "alias"                              
  user_name   = "username"
  tenant_name = "tenantname"
  password    = "pwd"
  region      = "region"
}

resource "openstack_compute_keypair_v2" "keypair" {
  provider   = "myprovider"               
  name       = "keypair"                 
  public_key = "${file("~/.ssh/id_rsa.pub")}"
}

resource "openstack_compute_instance_v2" "compute_instance" {
  name        = "compute_instance" # Instance Name
  provider    = "myprovider"       # Instance distr
  image_name  = "Centos 7"         # Image name
  flavor_name = "b2-7"             # Machine type name

  key_pair = "${openstack_compute_keypair_v2.keypair.name}"

  network {
    name = "Ext-Net"
  }

  # Add a provisionner file on the ressource
  provisioner "file" {
    source      = "foo_scripts/bar-setup.sh"
    destination = "/tmp/bar-setup.sh"

    connection {
      type        = "ssh"
      user        = "user"
      private_key = "${file("~/.ssh/id_rsa")}"
    }
  }

  # execute server setup file
  provisioner "remote-exec" {
    inline = [
      "chmod +x /tmp/bar-setup.sh",
      "sudo bash /tmp/bar-setup.sh",
    ]

    connection {
      type        = "ssh"
      user        = "centos"
      private_key = "${file("~/.ssh/id_rsa")}"
    }
}

事实上,通过在资源上添加 provionner 文件,当我运行命令terraform planterraform apply时,我的实例没有任何变化。我有 infos 消息通知我:

申请完成!资源:添加 0 个,更改 0 个,销毁 0 个。

将我的更改应用于我的计算实例的正确方法是什么。

4

2 回答 2

1

Following Terraform documentation:

Provisioners are used to execute scripts on a local or remote machine as part of resource creation or destruction.

If you want the provisionners to run again, you should destroy (terraform destroy) and create (terraform apply) the resource again.

于 2019-01-04T15:38:25.433 回答
1

Terraform 无法检查本地或远程执行的状态,它不像有一个 API 调用可以告诉您自定义代码发生了什么 - bar-setup.sh。那就像魔法,或者真正的魔法。

Terraforms 用于管理基础设施、实例的配置,而不是真正用于实例上的内容。不可变的内容和重新创建是这里的真正途径。制作一个全新的实例。但是,如果它是你的锤子,有办法。

如果您污染了要更新的资源,则下次再次运行 terraform 时,将重新执行该资源。但请注意我所说的关于 Hammers 的内容。

或者,您可以利用您选择的 CM 工具来管理您的实例的内容 - Chef/Ansible 或通过诸如打包器之类的工具创建 Openstack 使用的图像(即不可变的)并更新它们。我会做后者。

于 2019-01-05T21:44:50.420 回答