如何使用ansible角色生成terraform执行计划?

时间:2019-05-21 15:00:53

标签: ansible terraform

ansible提供的

Terraform模块非常适合通过状态文件的S3后端配置创建AWS资源。 但无法使用此模块获取COALESCE()输出。 我们希望输出应列出类似以下内容的内容: terraform plan 并提供要创建/销毁/更改的资源的详细信息

尝试了下面的任务,但无法产生预期的输出。

下面是创建计划的一项艰巨任务:

Plan: 1 to add, 0 to change, 0 to destroy.

以上任务的输出:

- name: "create file"
  shell: "touch {{playbook_dir}}/tfplan && ls -larth ../terraform/{{role_name}} "

- name: "Run terraform project with plan file"
  terraform:
    state: planned
    backend_config:
      bucket: "{{bootstrap_prefix}}-{{aws_account_type}}-{{caller_facts.account}}"
      region: "{{ bootstrap_aws_region }}"
      kms_key_id: "{{ kms_id.stdout }}"
      encrypt: true
      workspace_key_prefix: "{{ app_parent }}-{{ app_name }}"
      key: "terraform.tfstate"
    force_init: true
    project_path: "../terraform/{{role_name}}"
    plan_file: "{{playbook_dir}}/tfplan"
    variables:
      app_name: "{{ app_name }}"
    workspace: "{{ app_env }}"

它可以与ok: [localhost] => { "changed": false, "command": "/usr/local/bin/terraform -lock=true /root/project/ansible/tfplan", "invocation": { "module_args": { "backend_config": { "bucket": "XXXXXXXX2440728499", "encrypt": true, "key": "terraform.tfstate", "kms_key_id": "XXXXXXXX", "region": "XXXXXXXX", "workspace_key_prefix": "XXXXXX" }, "binary_path": null, "force_init": true, "lock": true, "lock_timeout": null, "plan_file": "/root/project/ansible/tfplan", "project_path": "../terraform/applications", "purge_workspace": false, "state": "planned", "state_file": null, "targets": [], "variables": { "app_name": "application" }, "variables_file": null, "workspace": "uat" } }, "outputs": {}, "state": "planned", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": [], "workspace": "uat" } state: present)配合使用,但希望它与terraform applystate:planned)一起使用

1 个答案:

答案 0 :(得分:0)

current ansible documentation中:To just run a terraform plan, use check mode.

此外,您应该添加到 terraform 模块参数:

- name: "Run terraform project with plan file"
  terraform:
    state: planned
    check_mode: true