【发布时间】:2015-12-18 16:27:48
【问题描述】:
Ansible 1.9.2 版本。
Ansible 在评估变量时是否支持变量内的变量扩展。
我的任务是从 Artifactory 下载 3 个 zip 文件。
我没有在角色中编写 3 个单独的任务,而是在剧本中使用了 ansible 的循环。在 Ansible 角色的 default/main.yml 中,我已经为角色定义/可用的所有必需变量,即 jmeterplugins_extras_artifactory_url 和其他(标准/webdriver)对 perf_tests 角色可见。
---
#- Download and install JMeterPlugins
# Use get_url when Ansible is 2.0+ is available on the machine (otherwise, we can't use get_url) thus, using wget.
- name: Download JMeterPlugins-*
command: wget {{ jmeterplugins_{{ item.plugin }}_artifactory_url }}
chdir="{{ common_download_dir }}"
creates="{{ common_download_dir }}/{{ jmeterplugins_{{ item.plugin }}_file }}"
with_items:
- { plugin: 'extras' }
- { plugin: 'standard' }
- { plugin: 'webdriver' }
但是使用上面的代码,我得到一个错误(如下所示):
15:58:57 TASK: [perf_tests | Download JMeterPlugins-*] *********************************
15:58:57 <jmeter01.super.fast.jenkins> ESTABLISH CONNECTION FOR USER: cmuser on PORT 22 TO jmeter01.super.fast.jenkins
15:58:57 fatal: [jmeter01.super.fast.jenkins] => Failed to template wget {{ jmeterplugins_{{ item.plugin }}_artifactory_url }} chdir="{{ common_download_dir }}" creates="{{ common_download_dir }}/{{ jmeterplugins_{{ item.plugin }}_file }}": template error while templating string: expected token 'variable_end', got '{'
15:58:57
15:58:57 FATAL: all hosts have already failed -- aborting
15:58:57
15:58:57 PLAY RECAP ********************************************************************
15:58:57 to retry, use: --limit @/home/cmuser/perf_tests.retry
15:58:57
15:58:57 jmeter01.super.fast.jenkins : ok=23 changed=6 unreachable=1 failed=0
如果一个变量包含另一个变量(尤其是当我使用循环时),ansible 不支持变量扩展/评估。
我只是不想将我的简单循环任务扩展为 3 个不同的名称任务,以分别下载 jmeterplugins_extras、jmeterplugins_standard 和 jmeterplugins_webdriver 的 zip 文件。该错误似乎与 Jinja 有关。
如何在另一个变量中使用var的值giga,即如果var包含giga,那么我应该得到变量“special_giga_variable”的值" ({{special_{{ var }}_variable}})?其中 var 在 defaults/main.yml 中定义为:
变量:千兆
【问题讨论】:
标签: variables ansible ansible-playbook jinja2