Ansible 是否可以在运行时定义一个var并使用它访问另一个var?
我不确定这是否可行 我想在运行时定义一个变量,并使用它访问文件playbook中定义的另一个变量 在运行时定义:Ansible 是否可以在运行时定义一个var并使用它访问另一个var?,ansible,jinja2,ansible-template,Ansible,Jinja2,Ansible Template,我不确定这是否可行 我想在运行时定义一个变量,并使用它访问文件playbook中定义的另一个变量 在运行时定义: typeConfig (possible values: "in_config" or "out_config") 在剧本中定义: in_config: url_config: http://localhost/configuration out_config: url_config: http://config.pi.dyn-dns.org/configuration
typeConfig (possible values: "in_config" or "out_config")
在剧本中定义:
in_config:
url_config: http://localhost/configuration
out_config:
url_config: http://config.pi.dyn-dns.org/configuration
我需要解决类似的问题:
{{ {{ typeConfig }}.url_config }}
我尝试:
- name: Mytest
hosts: all
gather_facts: false
sudo: yes
vars:
- in_config:
url_config: http://localhost/configuration
- out_config:
url_config: http://config.pi.dyn-dns.org/configuration
tasks:
- set_fact:
typeConfig: in_config
- name: Value in_config.url_config
debug: msg=" {{in_config.url_config}}"
- name: Value out_config.url_config
debug: msg=" {{out_config.url_config}}"
- name: Value typeConfig
debug: var=typeConfig
- debug: msg="{{ {{ typeConfig }}.url_config }} "
实际结果
任务路径:/home/nor/gitrepos/iiot-iac/ansible/myUnitTest.yml:19
致命:[node1]:失败!=>{
失败:对,
msg:template字符串时出错:应为标记“:”,获得“}”。字符串:{{{{typeConfig}}.url_config}}
您可以使用以下方式访问该值:
- debug:
msg: "{{ vars[typeConfig].url_config }}"
请记住,{…}}不是编写变量名的方法,而是启动Jinja2表达式的方法。当查询值时,变量在Ansible中使用Jinja2表达式引用,因此使用{{{{…}}}}}没有任何意义