通过Ansible的“ terraform”模块执行Terraform。所需的字符串集

时间:2019-06-03 16:02:04

标签: variables ansible terraform

所以我有以下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变量提供完全相同的内容,这是行不通的。

有人暗示该变量的语法有什么问题吗?

非常感谢!

1 个答案:

答案 0 :(得分:1)

很可能您需要将变量"security_groups" {type="list"}声明为列表以使其采用多个值

定义此选项可以理想地解决您的问题。