Ansible:任务名称中的变量插值 [英] Ansible: variable interpolation in task name

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

问题描述

我无法让这个看似简单的示例在Ansible 1.8.3中工作.变量插值不会加入任务名称.我看到的所有示例似乎都建议这样做.鉴于变量是在vars部分中定义的,因此我希望任务名称能够显示变量的值.为什么这行不通?

I cannot get this seemingly simple example to work in Ansible 1.8.3. The variable interpolation does not kick in the task name. All examples I have seen seem to suggest this should work. Given that the variable is defined in the vars section I expected the task name to print the value of the variable. Why doesn't this work?

即使Ansible 文档中的示例似乎也不会打印变量值.

Even the example from the Ansible documentation seems to not print the variable value.

---
- hosts: 127.0.0.1
  gather_facts: no
  vars:
    vhost: "foo"
  tasks:
    - name: create a virtual host file for {{ vhost }}
      debug: msg="{{ vhost }}"

这将产生以下输出:

PLAY [127.0.0.1]     
************************************************************** 

TASK: [create a virtual host file for {{ vhost }}] 
**************************** 
ok: [127.0.0.1] => {
   "msg": "foo"
}

PLAY RECAP 
******************************************************************** 
127.0.0.1                  : ok=1    changed=0    unreachable=0    failed=0   

更新 这适用于1.7.2,但不适用于1.8.3.因此,这是错误还是功能.

Update This works with 1.7.2 but does not work with 1.8.3. So either this is a bug or a feature.

推荐答案

name内部未解析变量.仅在实际任务/条件等内部,将解析占位符.我想这是设计使然.假设您有一个with_items循环,并在name中使用了{{ item }}.任务name仅打印一次,但{{ item }}在每次迭代中都会更改.

Variables are not resolved inside the name. Only inside the actual tasks/conditions etc. the placeholders will be resolved. I guess this is by design. Imagine you have a with_items loop and use the {{ item }}in the name. The tasks name will only be printed once, but the {{ item }} would change in every iteration.

我看到了示例,即使您链接到的文档中的示例,也使用name中的变量.但这并不意味着结果会像您预期的那样.该文档是由社区管理的.可能是有人将那条线放在那儿而没有对其进行测试-或它曾经像以前的Ansible版本中那样工作,并且当时该文档还没有更新. (大约一年后我才使用Ansible).但是,即使它不能按我们希望的那样工作,但我仍在name中使用变量,只是表明该任务是基于动态参数的.可能是出于同样的意图编写了示例.

I see the examples, even the one in the doc you linked to, use variables in the name. But that doesn't mean the result would be like you expected it. The docs are community managed. It might be someone just put that line there w/o testing it - or maybe it used to work like that in a previous version of Ansible and the docs have not been updated then. (I'm only using Ansible since about one year). But even though it doesn't work like we wish it would, I'm still using variables in my name's, just to indicate that the task is based on dynamic parameters. Might be the examples have been written with the same intention.

我最近做的一个有趣的观察(Ansible 1.9.4)是,默认值写在任务名称中.

An interesting observation I recently made (Ansible 1.9.4) is, default values are written out in the task name.

- name: create a virtual host file for {{ vhost | default("foo") }}

执行时,Ansible将任务标题显示为:

When executed, Ansible would show the task title as:

任务:[为foo创建虚拟主机文件]

TASK: [create a virtual host file for foo]

这样,您可以避免输出中出现难看的任务名称.

This way you can avoid ugly task names in the output.

这篇关于Ansible:任务名称中的变量插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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