## terraform provisioner ## 1. terraform跟ansible puppet等工具最大的区别就是terraform主要是服务资源的编排(service provisioning tools)工具,比如创建服务器、数据库,存储,负载均衡、网络,但是ansible是配置管理( config management tools )工具,比如配置服务器安装软件,拷贝文件等等 2. terraform也具备配置管理的功能,他是通过provisioner来实现,也可以实现文件远程拷贝,服务器的本地或者远程配置 ## provisioner 类型 ## 1. File Provisioner The file provisioner is used to copy files or directories from the machine executing Terraform to the newly created resource. The file provisioner supports both ssh and winrm type connections. 2. local-exec Provisioner The local-exec provisioner invokes a local executable after a resource is created. This invokes a process on the machine running Terraform, not on the resource. 3. remote-exec Provisioner The remote-exec provisioner invokes a script on a remote resource after it is created. This can be used to run a configuration management tool, bootstrap into a cluster, etc. ## provisoner connection ## ``` provisioner "file" { source = "conf/myapp.conf" destination = "/etc/myapp.conf" connection { type = "ssh" user = "root" password = "${var.root_password}" host = "${var.host}" } } ``` ## Provisioners Without a Resource ## ``` resource "null_resource" "cluster" { # Changes to any instance of the cluster requires re-provisioning triggers = { cluster_instance_ids = "${join(",", aws_instance.cluster.*.id)}" } # Bootstrap script can run on any instance of the cluster # So we just choose the first in this case connection { host = "${element(aws_instance.cluster.*.public_ip, 0)}" } provisioner "remote-exec" { # Bootstrap script called with private_ip of each node in the cluster inline = [ "bootstrap-cluster.sh ${join(" ", aws_instance.cluster.*.private_ip)}", ] } } ```