gpt4 book ai didi

variables - 通过 Ansible "terraform"模块执行 Terraform。需要一组字符串

转载 作者:行者123 更新时间:2023-12-04 17:36:45 27 4
gpt4 key购买 nike

所以我有以下 Ansible 剧本来执行 Terraform 脚本:

    - name: run Terraform
terraform:
project_path: "{{ terraform_base_dir }}"
force_init: yes
variables:
cp_key: "{{ cp_key }}"
cp_secret_key: "{{ cp_secret_key }}"
cluster_name: "{{ cluster_name }}"
template: "{{ template }}"
zone: "{{ zone }}"
size: "{{ size }}"
disk_size: "{{ disk_size }}"
key_pair: "{{ key_pair }}"
security_groups: "{{ security_groups }}"

和以下 Ansible 变量:

terraform_base_dir: "/root/deploy_k8s/terraform"
cp_key: "xxxxxxxxxxxxx"
cp_secret_key: "xxxxxxxxxxxxx"
cluster_name: "test123"
template: "Linux Ubuntu 18.04 LTS 64-bit"
zone: "at-vie-1"
size: "Medium"
disk_size: "100"
key_pair: "kp_xyz"
user: "ubuntu"
security_groups:
- "k8s-{{ cluster_name }}"
- "Jumphosts"
- "mobile_network"
- "home"

执行剧本,我得到以下错误:

fatal: [localhost]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"backend_config": null,
"binary_path": null,
"force_init": true,
"lock": true,
"lock_timeout": null,
"plan_file": null,
"project_path": "/root/deploy_k8s/terraform",
"purge_workspace": false,
"state": "present",
"state_file": null,
"targets": [],
"variables": {
"cluster_name": "test123",
"disk_size": "100",
"cp_key": "xxxxxxxxxxxxx",
"cp_secret_key": "xxxxxxxxxxxxx",
"key_pair": "kp_xyz",
"security_groups": [
"k8s-test123",
"Jumphosts",
"mobile_network",
"home"
],
"size": "Medium",
"template": "Linux Ubuntu 18.04 LTS 64-bit",
"zone": "at-vie-1"
},
"variables_file": null,
"workspace": "default"
}
},
"msg": "Terraform plan could not be created\r\nSTDOUT: Refreshing Terraform state in-memory prior to plan...\nThe refreshed state will be used to calculate this plan, but will not be\npersisted to local or remote state storage.\n\n\n------------------------------------------------------------------------\n\r\n\r\nSTDERR: \nError: Incorrect attribute value type\n\n on build.tf line 28, in resource \"cp_compute\" \"master\":\n 28: security_groups = \"${var.security_groups}\"\n\nInappropriate value for attribute \"security_groups\": set of string required.\n\n\nError: Incorrect attribute value type\n\n on build.tf line 38, in resource \"cp_compute\" \"node01\":\n 38: security_groups = \"${var.security_groups}\"\n\nInappropriate value for attribute \"security_groups\": set of string required.\n\n\nError: Incorrect attribute value type\n\n on build.tf line 48, in resource \"cp_compute\" \"node02\":\n 48: security_groups = \"${var.security_groups}\"\n\nInappropriate value for attribute \"security_groups\": set of string required.\n\n"
}

属性“security_groups”的值不合适:需要一组字符串。

Terraform“build.tf”文件是:

...
variable "security_groups" {}
...
resource "cp_compute" "master" {
display_name = "k8s-${var.cluster_name}-master"
template = "${var.template}"
zone = "${var.zone}"
size = "${var.size}"
disk_size = "${var.disk_size}"
key_pair = "${var.key_pair}"
security_groups = "${var.security_groups}"
}
...

在 .tf 文件中提供硬编码的安全组非常有效。

security_groups = ["k8s-test123","Jumphosts","mobile_network","home"]

通过 Ansible 变量提供完全相同的内容,这是行不通的。

有人有任何提示变量的语法有什么问题吗?

非常感谢!

最佳答案

很可能您需要将变量 "security_groups"{type="list"} 声明为一个列表,以便它获取多个值

定义这个应该可以理想地解决您的问题。

关于variables - 通过 Ansible "terraform"模块执行 Terraform。需要一组字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56430885/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com