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

查看:25
本文介绍了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天全站免登陆