ansible - 变量中的变量 [英] ansible - variable within variable

查看:20
本文介绍了ansible - 变量中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ansible 1.9.2 版本.

Ansible 1.9.2 version.

Ansible 是否支持在评估变量时在变量内进行变量扩展.

Does Ansible supports variable expansion within a variable while evaluating it.

我的任务是从 Artifactory 下载 3 个 zip 文件.

I have a task to download 3 zip files from Artifactory.

我没有在角色中编写 3 个单独的任务,而是在剧本中使用了 ansible 循环.在 Ansible 角色的 default/main.yml 中,我为角色定义了/可用的所有必需变量,即 jmeterplugins_extras_artifactory_url 和其他(标准/网络驱动程序)对 perf_tests 角色可见.

Instead of writing 3 separate tasks within the role, I used ansible's loop in the playbook. In Ansible role's default/main.yml, I have all the required variables defined/available to the role i.e. jmeterplugins_extras_artifactory_url and other (standard / webdriver) are visible to perf_tests role.

---
#- 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' }   

但是使用上面的代码,我收到一个错误(如下所示):

But with the above code, I'm getting an error (as shown below):

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 不支持变量扩展/评估.

Doesn't ansible supports variable expansion/evaluation if a variable contains another variable (especially when I'm using a loop).

我只是不想将我的简单循环任务扩展为 3 个不同的名称任务,用于分别下载 jmeterplugins_extras、jmeterplugins_standard 和 jmeterplugins_webdriver 的 zip 文件.错误似乎与 Jinja 有关.

I just dont want to expand my simple loop task into 3 different -name tasks for downloading zip files for jmeterplugins_extras, jmeterplugins_standard and jmeterplugins_webdriver separately. It seems like the error is related due to Jinja.

如何在另一个变量中使用 var 的值 giga,即如果 var 包含 giga,那么我应该获取变量special_giga_variable"的值" ({{special_{{ var }}_variable}})?其中 var 在 defaults/main.yml 中定义为:

How can I use var's value giga in another variable i.e. if var contains giga, then I should get the value of variable "special_giga_variable" ({{special_{{ var }}_variable}})? where var was defined in defaults/main.yml as:

变量:千兆

推荐答案

不,它没有.但这并不意味着您必须将其扩展为 3 个不同的任务.您实际上可以做的是将您的字典"扩展为类似于以下内容:

No it doesn't. But it doesn't mean that you have to expand it into 3 different tasks. What you can do is actually expand you "dictionary" to look similar to this:

with_items:
 - {"url": "https://xxxxx", "file": "/tmp/xxxxx" }
 - {"url": "https://yyyyy", "file": "/tmp/yyyyy" }
 - {"url": "https://zzzzz", "file": "/tmp/zzzzz" }

然后在你的任务中调用不同的参数:{{ item.url }} 和 {{ item.file }}

Then in your task just call different parameters: {{ item.url }} and {{ item.file }}

替代方案:

  1. 编写您自己的过滤器,根据值 {{ jmeterplugins_url | 扩展您的变量my_custom_filter(item.plugin) }}

编写一个自定义模块,将根据您的输入将获取 url 的所有功能封装到文件中

Write a custom module, that will encapsulate all of the functionality of fetching url into the file based on your inputs

编写自定义lookup_plugin,它将遍历您的变量列表并产生正确的结果.

Write custom lookup_plugin that will iterate through your list of variables and produce correct result.

由于您正在使用 command 模块,因此您可以利用 bash 将您的 url、文件连接到同一命令中(这可能是最混乱的解决方案)

Since you are using command module you can leverage bash to concatenate your url, file in the same command ( this would probably be the messiest solution )

这篇关于ansible - 变量中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆