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

查看:141
本文介绍了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中,我已定义/可用于该角色的所有必需变量,即 perf_tests 角色可以看到jmeterplugins_extras_artifactory_url和其他(标准/webdriver).

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个不同的-name任务,以分别下载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:

var:giga

推荐答案

不是.但这并不意味着您必须将其扩展为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 }} and {{ 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天全站免登陆