将整数变量传递给任务而不会丢失整数类型 [英] Pass integer variable to task without losing the integer type
问题描述
我不拥有一个任务(实际上是一个角色,但是在这里使用一个任务使示例变得容易),它对变量执行一些操作.假定变量是整数.我需要以某种方式将其传递给变量,并将其作为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
推荐答案
解决方案
-
升级到Ansible 2.7(当前可作为
stable-2.7
分支,计划于2018年10月4日在Google上发布).
将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的功能原生Python类型,并保留数据类型.
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屋!