如何在 terraform 代码中调用 ansible playbook? [英] How to call ansible playbook in terraform code?

查看:54
本文介绍了如何在 terraform 代码中调用 ansible playbook?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行良好的 ansible playbook,现在我必须使用 terrform 脚本调用该 playbook.目前我正在使用如下所示的代码,但它在 terraform init 期间导致错误:错误:未知的根级密钥:provisioner

我使用的是 Terraform v0.11.7,只有在我运行此特定代码时才会出现错误.另外我的 main.tf 仅包含此代码.我使用的目录结构是这样的:

<预><代码>.├── create-user.yml├── 图书馆│ └── mkpassword.py├── main.tf├── 输出.tf├── 角色│ └── linux_user_creation│ └── 任务│ └── main.yml└── 变量.tf

main.tf 看起来像:

 供应商远程执行"{内联 = ["sudo dnf -y 安装 python"]联系 {类型 = "ssh"用户 = "ubuntu"private_key = "${file(var.ssh_keyname)}"}}供应商远程执行"{命令 = ["ansible-playbook -u root --private-key ${var.ssh_keyname} -i ${self.ipv4_address} create-user.yml -e 'email_id=${var.email_id}'"]}

我希望应该从 terraform 脚本中调用剧本并显示结果.

解决方案

代码截取表单 main.tf 不完整.您能否发布运行 remote-exec 配置程序的完整资源定义?

Ansible playbook 应该做什么?在远程主机上创建用户?或者它只是一个存储 Ansible 脚本的主机,而用户实际上是在另一个远程主机上从那里创建的?

正如@ydaetskcoR 提到的,您需要在 null_resource 中运行此代码:

null_resource "provisioner" {联系 {... # 这里设置连接参数}供应商远程执行"{命令 = ["ansible-playbook -u root --private-key ${var.ssh_keyname} -i ${self.ipv4_address} create-user.yml -e 'email_id=${var.email_id}'"]}}

不过,我建议安装 Ansible 配置器,如上面的评论中所述.这样,您就可以将 Ansible playbook 直接与 Terraform 代码捆绑在一起,而无需连接到其他实例.

I have an ansible playbook which works well , now I have to call that playbook using terrform scripts. Currently I m using code which is shown below but it results in error during terraform init as: Error: Unknown root level key: provisioner

I am using Terraform v0.11.7 and the error occurs only when I run this specific code. Also my main.tf consists of only this code. The directory structure I have used is such as :

.
├── create-user.yml
├── library
│   └── mkpassword.py
├── main.tf
├── outputs.tf
├── roles
│   └── linux_user_creation
│       └── tasks
│           └── main.yml
└── variables.tf

main.tf looks like :

  provisioner "remote-exec" {
  inline = ["sudo dnf -y install python"]

  connection {
    type        = "ssh"
    user        = "ubuntu"
    private_key = "${file(var.ssh_keyname)}"
  }
}

provisioner "remote-exec" {
  command = ["ansible-playbook -u root --private-key ${var.ssh_keyname} -i ${self.ipv4_address} create-user.yml -e 'email_id=${var.email_id}'"]
}

I expect the playbook should be called from the terraform scripts and should display the results.

解决方案

The code snipped form main.tf is not complete. Could you post the full resource definitions in which you run the remote-exec provisioners please?

An what should the Ansible playbook do? Create a user on the remote host itself? Or is it just a host where your Ansible scripts are stored, and the user is actually created on another remote host from there?

As @ydaetskcoR mentioned, you need to run this code within a null_resource:

null_resource "provisioner" {
  connection {
    ... # set the connection parameters here
  }

  provisioner "remote-exec" {
    command = ["ansible-playbook -u root --private-key ${var.ssh_keyname} -i ${self.ipv4_address} create-user.yml -e 'email_id=${var.email_id}'"]
  }
}

I'd however suggest to install the Ansible provisioner, as already mentioned in the comment above. This way you'd bundle the Ansible playbook directly with your Terraform code and won't not need to connect to some other instance.

这篇关于如何在 terraform 代码中调用 ansible playbook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆