local-exec provisioner的用途是什么?

huangapple go评论51阅读模式
英文:

What is the usage of local-exec provisioner?

问题

我正在通过书籍了解Terraform和Ansible。 有人可以为我解释以下代码块吗?

provisioner "local-exec" { 
    command = "ansible-playbook -u ubuntu --key-file ansible-key.pem -T 300 -i '${self.public_ip},' app.yml" 
}
英文:

I am getting familiar with Terraform and Ansible through books. Could someone enlighten me about the following block of code?

provisioner "local-exec" { 
    command = "ansible-playbook -u ubuntu --key-file ansible-key.pem -T 300 -i '${self.public_ip},', app.yml" 
  }

答案1

得分: 2

简短的答案是 local-exec 用于在本地计算机上执行操作,而不是远程计算机。

您可以执行多种不同的操作:

  • 将 SSH 密钥写入您的 ~/.ssh 以访问服务器
  • 运行 sleep 30 或类似的操作,确保下一个命令等待一段时间以进行机器配置
  • 将日志写入本地目录(上次运行、完成日期等)
  • 在本地计算机上写入一些环境变量,以供后续访问远程机器时使用
  • 以您提供的 ansible 示例

值得注意的是,HashiCorp 不太喜欢 local-remote- exec。如果您与他们的开发人员交流,他们会告诉您这是一种必要的权宜之计。除了可能的延时操作或写入某些信息之外,尽量避免在其中存储有状态的数据。

英文:

The short answer is local-exec is for anything you want to do on your local machine instead of the remote machine.

You can do a bunch of different things:

  • write an ssh key into your ~/.ssh to access the server
  • run a sleep 30 or something to make sure the next commands wait a bit for your machine to provision
  • write logs to your local directory (last run, date completed, etc.)
  • write some env_vars to your local machine you can use to access the machine
  • the ansible example you provided

FYI, hashicorp hates local- and remote- exec. If you talk to one of their devs, they will tell you that it is a necessary evil. Other than maybe a sleep or write this or that, avoid it for any stateful data.

答案2

得分: 1

我会将以下内容翻译为中文:

"我会将那解释为Terraform应在控制节点上执行本地命令。

阅读有关local-exec Provisioner的文档后,发现

local-exec provisioner在(注:远程)资源创建后调用本地可执行文件。这在运行Terraform的机器上启动一个进程...

而不是在远程资源上。

因此,在Terraform创建虚拟机之后,它会调用Ansible播放书以进一步进行操作。"

英文:

I would interpret that as Terraform should execute a local command on the Control Node.

Reading the documentation about local-exec Provisioner it turns out that

> The local-exec provisioner invokes a local executable after a (annot.: remote) resource is created. This invokes a process on the machine running Terraform ...

and not on the Remote Resource.

So after Terraform has in example created a Virtual Machine, it calls an Ansible playbook to proceed further on it.

huangapple
  • 本文由 发表于 2023年2月14日 02:15:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定