Ansible会自动翻转变量。

huangapple go评论101阅读模式
英文:

Ansible automatically flips variable

问题

我最近遇到了Ansible的一个非常奇怪的行为,想知道为什么会发生这种情况。

我使用的是Ansible 2.7.7Python 3.5.3

具体来说,我有两个变量:

  1. GLOBAL_HIGHEST_PARAM: "{5,0}"
  2. LOCAL_HIGHEST_PARAM: "{5,0}"

并且在使用LOCAL_HIGHEST_PARAM时,例如像这样:

  1. - name: 设置最高分参数 {{ LOCAL_HIGHEST_PARAM }}
  2. # ...

在运行时给我正确的输出:设置最高分参数 {5,0}

但是当我定义变量如下:

  1. GLOBAL_HIGHEST_PARAM: "{5,0}"
  2. LOCAL_HIGHEST_PARAM: "{{ GLOBAL_HIGHEST_PARAM }}"

我得到了不正确的输出:设置最高分参数 {0,5}

为什么会发生这种情况?

英文:

I recently came across a very strange behavior of Ansible and would like to know why this is happening.

I'm using Ansible 2.7.7 with python 3.5.3.

So to the point.
I have two variables:

  1. GLOBAL_HIGHEST_PARAM: "{5,0}"
  2. LOCAL_HIGHEST_PARAM: "{5,0}"

And using LOCAL_HIGHEST_PARAM for example like this:

  1. - name: Setting up highest score param {{ LOCAL_HIGHEST_PARAM }}
  2. # ...

Which gives me the correct output during run: Setting up highest score param {5,0}

But when I define variables such as:

  1. GLOBAL_HIGHEST_PARAM: "{5,0}"
  2. LOCAL_HIGHEST_PARAM: "{{ GLOBAL_HIGHEST_PARAM }}"

I get the incorrect output: Setting up highest score param {0,5}

Why is this happening?

答案1

得分: 2

  1. 这是因为`{5,0}`实际上是一个[Python集合](https://docs.python.org/3.11/library/stdtypes.html#set-types-set-frozenset),你可以在调试变量时看到:
  2. ```yaml
  3. - debug:
  4. msg: "{{ GLOBAL_HIGHEST_PARAM }}"
  5. vars:
  6. GLOBAL_HIGHEST_PARAM: "{5,0}"

会产生:

  1. ok: [localhost] =>
  2. msg: !!set
  3. 0: null
  4. 5: null

由于50是集合的键,所以集合会按键排序。

你可以使用Jinja的string过滤器强制将其表示为字符串。

任务:

  1. - debug:
  2. msg: "{{ LOCAL_HIGHEST_PARAM }}"
  3. vars:
  4. GLOBAL_HIGHEST_PARAM: "{5,0}"
  5. LOCAL_HIGHEST_PARAM: "{{ GLOBAL_HIGHEST_PARAM | string }}"

会得到你期望的结果:

  1. ok: [localhost] =>
  2. msg: '{5,0}'

请注意,在任务名称中可以正常工作,因为任务名称只能是字符串,所以它会隐式转换为字符串,在其他情况下可能不会,除非使用显式的string过滤器。

  1. <details>
  2. <summary>英文:</summary>
  3. This happens because `{5,0}` is actually a [Python set](https://docs.python.org/3.11/library/stdtypes.html#set-types-set-frozenset), which you can actually see debugging the variable:
  4. ```yaml
  5. - debug:
  6. msg: &quot;{{ GLOBAL_HIGHEST_PARAM }}&quot;
  7. vars:
  8. GLOBAL_HIGHEST_PARAM: &quot;{5,0}&quot;

Would yield:

  1. ok: [localhost] =&gt;
  2. msg: !!set
  3. 0: null
  4. 5: null

And since 5 and 0 are the keys of the set, the set get sorted by keys.

You can force it to be represented as a string, though, with the string filter of Jinja.

The task:

  1. - debug:
  2. msg: &quot;{{ LOCAL_HIGHEST_PARAM }}&quot;
  3. vars:
  4. GLOBAL_HIGHEST_PARAM: &quot;{5,0}&quot;
  5. LOCAL_HIGHEST_PARAM: &quot;{{ GLOBAL_HIGHEST_PARAM | string }}&quot;

Will give you the expected

  1. ok: [localhost] =&gt;
  2. msg: &#39;{5,0}&#39;

Note that it does work in a task name because tasks names can only be string, so it is implicitly casted as a string, while it might not be, in other cases, without an explicit string filter.

huangapple
  • 本文由 发表于 2023年7月17日 18:40:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定