我试图避免在包含的文件中重复一个值。我尝试在包含的文件中添加顶级name
,vars
和tasks
:
---
- name: Install Terraform
vars:
terraform_base_url: "https://releases.hashicorp.com/terraform/{{ terraform_version }}"
terraform_package_file: "terraform_{{ terraform_version }}_linux_amd64.zip"
terraform_package_url: "{{ terraform_base_url }}/{{ terraform_package_file }}"
terraform_checksum_url: "{{ terraform_base_url }}/terraform_{{ terraform_version }}_SHA256SUMS"
terraform_package_path: "/tmp/{{ terraform_package_file }}"
tasks:
- name: Install unzip package
…
这导致“任务中未检测到任何操作”错误。我还尝试删除了tasks
标题并将所有任务放在顶层,但是得到了相同的结果。因此,我设置了这样的变量(terraform_version
在剧本中定义):
- name: Set Terraform variables
set_fact:
terraform_base_url: "https://releases.hashicorp.com/terraform/{{ terraform_version }}"
terraform_package_file: "terraform_{{ terraform_version }}_linux_amd64.zip"
terraform_package_url: "{{ terraform_base_url }}/{{ terraform_package_file }}"
terraform_checksum_url: "{{ terraform_base_url }}/terraform_{{ terraform_version }}_SHA256SUMS"
terraform_package_path: "/tmp/{{ terraform_package_file }}"
ansible-lint
对此没有抱怨,但是由于某种原因,第二个变量中的terraform_base_url
引用中断了:
The task includes an option with an undefined variable. The error was: 'terraform_base_url' is undefined
The error appears to have been in '/vagrant/provisioning/ansible/install-terraform.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- name: Set Terraform variables
^ here
如果可以为包含的文件设置vars
(即,文件中所有任务可以用于 all 而不复制它们的变量),那就更好了。
答案 0 :(得分:0)
看来必须将其拆分为两个才能工作:
- name: Set Terraform variables 1
set_fact:
terraform_base_url: "https://releases.hashicorp.com/terraform/{{ terraform_version }}"
terraform_package_file: "terraform_{{ terraform_version }}_linux_amd64.zip"
- name: Set Terraform variables 2
set_fact:
terraform_package_url: "{{ terraform_base_url }}/{{ terraform_package_file }}"
terraform_checksum_url: "{{ terraform_base_url }}/terraform_{{ terraform_version }}_SHA256SUMS"
terraform_package_path: "/tmp/{{ terraform_package_file }}"
答案 1 :(得分:0)
如果稍后不需要中间变量,则可以使用var
语句:
- name: Set Terraform variables
set_fact:
terraform_package_url: "{{ terraform_base_url }}/{{ terraform_package_file }}"
terraform_checksum_url: "{{ terraform_base_url }}/terraform_{{ terraform_version }}_SHA256SUMS"
terraform_package_path: "/tmp/{{ terraform_package_file }}"
vars:
terraform_base_url: "https://releases.hashicorp.com/terraform/{{ terraform_version }}"
terraform_package_file: "terraform_{{ terraform_version }}_linux_amd64.zip"
此外,这里很有可能不需要set_fact
。在任何级别(游戏,任务)使用vars
部分,或将它们放置在role\vars\main.yaml
中作为角色。