首页 > 解决方案 > ansible:引用同一列表中的变量

问题描述

我有一个这样的列表(示例的缩写)

vars:  
 cli:  
   terraform:  
     bin_name: "terraform"
     source_url: "https://releases.hashicorp.com.."  
     bin_to_copy: "/tmp/{{ bin_name }}"  

为什么以后无法访问 {{ bin_name }} 名称?我尝试使用 cli.bin_name;item.bin_name; item.key.b.... 错误总是:失败!=> {"msg": "'bin_name' 未定义"}

标签: listvariablesansible

解决方案


这是不可能的。请参阅Can't reference a dict key inside the same dict #50280

FWIW。从字典中取出常用值。例如

vars:  
  my_bin_name: "terraform"
  cli:  
    terraform:  
      bin_name: "{{ my_bin_name }}"
      source_url: "https://releases.hashicorp.com.."  
      bin_to_copy: "/tmp/{{ my_bin_name }}"

推荐阅读