ansible - 如何使用 ansible 角色生成 terraform 执行计划?
问题描述
ansible 提供的 Terraform 模块非常适合使用 S3 后端配置为 statefile 创建 aws 资源。但无法terraform plan
使用此模块获得输出。我们希望输出应该列出如下内容:
Plan: 1 to add, 0 to change, 0 to destroy.
并提供要创建/销毁/更改的资源的详细信息
已在 ansible 中尝试过以下任务,但无法按预期生成输出。
以下是创建计划的 ansible 任务:
- 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 }}"
上述ansible任务的输出:
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 apply
) ,但希望它适用于state:planned
( terraform plan
)
解决方案
在当前的 ansible 文档中:To just run a terraform plan, use check mode.
此外,您应该添加到terraform
模块参数:
- name: "Run terraform project with plan file"
terraform:
state: planned
check_mode: true
推荐阅读
- django - Django 动态模型.FileField 存储
- .htaccess - RewriteEngine - 如何忽略对已经存在的文件夹的请求
- php - Laravel - 在多个单独的代码库之间共享功能
- angular - CdkDragDrop 和 ngTemplateOutlet
- javascript - 有什么方法可以将 URL 中的复选框状态保存为站点的最终用户?
- ruby-on-rails - 如何比较上一个条目
- php - 如何在一段时间内禁用 drupal 8 中的缓存?
- python - Python线程在while循环中
- c# - Xamarin Forms Picker 控件在 Entry 控件中输入时打开
- vim - 在 vim 命令中插入当前文件名不起作用