将整数变量传递给任务而不会丢失整数类型 [英] Pass integer variable to task without losing the integer type

查看:25
本文介绍了将整数变量传递给任务而不会丢失整数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不属于我的任务(实际上是一个角色,但在这里使用一个任务使示例更容易),它对变量执行一些操作.它假定变量是一个整数.我需要以某种方式向它传递一个变量并将其作为 int 传递,但我没有任何运气.

I've got a task (actually a role, but using a task here to make the example easier) that I don't own which does some operations on a variable. It assumes the variable is an integer. I need to somehow pass it a variable and have it come through as an int, and I'm not having any luck.

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

Here is a super simplified version of the task that I don't own:

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 作为字符串而不是整数.

No matter what, I get errors like "1 is greater than 100" from frob.yml. Looks like it's getting the var as a string instead of an integer.

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

I've tried stuff like frob_count: "{{item.parameter | int}}" with no luck. If I could change frob.yml it'd be easy, but like I said, that's out of my control. Any thoughts?

这是在 Ansible 2.6.4 上

This is on Ansible 2.6.4

推荐答案

解决方案

  1. 升级到 Ansible 2.7(目前可用作 stable-2.7 分支,计划于 10 月 GA. 2018 年 4 月).

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

Add jinja2_native=True to the [defaults] section of the ansible.cfg (or set an environment variable ANSIBLE_JINJA2_NATIVE=True.

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

Leave your code as in the question (i.e., 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 始终将输出呈现为字符串.

Currently any value returned by Jinja2 template is a string, so even if you used the int filter inside (as in {{item.parameter | int}}) the output is always rendered to a string by Ansible.

Ansible 2.7 将使用(带有上述参数)Jinja 2.10 的一项名为Native Python Types 并保留数据类型.

Ansible 2.7 will use (with the above parameter) a feature of Jinja 2.10 called Native Python Types and retain the data type.

这篇关于将整数变量传递给任务而不会丢失整数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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