【问题标题】:Pass integer variable to task without losing the integer type将整数变量传递给任务而不丢失整数类型
【发布时间】:2018-09-24 21:08:16
【问题描述】:

我有一个我不拥有的任务(实际上是一个角色,但在这里使用一个任务来简化示例),它对变量执行一些操作。它假设变量是一个整数。我需要以某种方式将它传递给一个变量并让它作为一个 int 传递,而我没有任何运气。

这是我不拥有的任务的超级简化版本:

frob.yml

- name: Validate that frob_count is <= 100
  fail: msg="{{frob_count}} is greater than 100"
  when: frob_count > 100

- name: Do real work
  debug: msg="We frobbed {{frob_count}} times!"

我的剧本是:

- name: Frob some things
  hosts: localhost
  vars:
    things:
      - parameter: 1
      - parameter: 2
      - parameter: 45
  tasks:
    - with_items: "{{things}}"
      include: frob.yml
      vars:
        frob_count: "{{item.parameter}}"

无论如何,我从frob.yml 收到诸如“1 大于 100”之类的错误。看起来它将 var 作为字符串而不是整数获取。

我尝试过frob_count: "{{item.parameter | int}}" 之类的东西,但没有成功。如果我可以更改frob.yml,那会很容易,但就像我说的那样,那是我无法控制的。有什么想法吗?

这是在 Ansible 2.6.4 上

【问题讨论】:

    标签: ansible


    【解决方案1】:

    解决方案

    1. 升级到 Ansible 2.7(目前作为 stable-2.7 分支提供,scheduled for GA on Oct. 4th, 2018)。

    2. 将jinja2_native=True 添加到ansible.cfg 的[defaults] 部分(或设置环境变量ANSIBLE_JINJA2_NATIVE=True。

    3. 保留问题中的代码(即frob_count: "{{item.parameter}}")。

    结果:

    TASK [Do real work] **********************************************************************************************************************
    ok: [localhost] => {
        "msg": "We frobbed 1 times!"
    }
    
    TASK [Validate that frob_count is <= 100] ************************************************************************************************
    skipping: [localhost]
    
    TASK [Do real work] **********************************************************************************************************************
    ok: [localhost] => {
        "msg": "We frobbed 2 times!"
    }
    
    TASK [Validate that frob_count is <= 100] ************************************************************************************************
    skipping: [localhost]
    
    TASK [Do real work] **********************************************************************************************************************
    ok: [localhost] => {
        "msg": "We frobbed 45 times!"
    }
    

    说明

    目前 Jinja2 模板返回的任何值都是字符串,因此即使您在内部使用了 int 过滤器(如在 {{item.parameter | int}} 中),输出总是由 Ansible 呈现为字符串。

    Ansible 2.7 will use(带有上述参数)Jinja 2.10 的一个功能称为Native Python Types 并保留数据类型。

    【讨论】:

    • 听起来我必须希望我的组织尽快采用 2.7,或者对麻烦的脚本进行 PR 并祈祷。呃,好吧。谢谢!
    • 完美,这是解决 Ansible 问题 github.com/ansible/ansible/issues/17992 的正确方法
    【解决方案2】:

    当您使用 include 任务发送变量时,它会变成 unicode 甚至强制 int 在您的剧本上。所以你需要像这样在 frob.yml 上使用 int 过滤器:

    frob.yml

    - name: Validate that frob_count is <= 100
      fail: msg="{{frob_count}} is greater than 100"
      when: frob_count|int > 100
    

    【讨论】:

    • 是的,但在我的场景中,我无法编辑任务。此外,传递一个 int 字面量(如 frob_count: 5)也可以正常工作
    • 嗯,基于 github.com/ansible/ansible/issues/9362 和 github.com/pallets/jinja/pull/708 听起来这是一个 Jinja 问题...
    • @KelsonSilva "当您使用 include 任务发送变量时,它会变成 unicode" -- 错误。您可以将include 替换为debug: msg={{ frob_count | type_debug }},您会看到类型是Unicode。 include/"sending" (不管它是什么意思)不是原因。并且 OP 明确表示他们无法控制frob.yml。
    • @techraf 我明白了......谢谢你的解释,我的理解是错误的......我很感激!
    【解决方案3】:

    我找到了一个似乎可行的解决方案,它不需要 ANSIBLE_JINJA2_NATIVE=True env 变量

    如果不是

    initialDelaySeconds: "{{ _initialDelaySeconds }}"
    

    你会的

    initialDelaySeconds: |
        {{ _initialDelaySeconds }}
    

    jinja模板中的"可以省略,值不会转成字符串!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      相关资源
      最近更新 更多