ansible - Ansible:计算元素并用于条件检查
问题描述
我有一本剧本,用于 key: value 定义,如下所示:
x_interfaces:
- eno1
- eno2
在另一本剧本中(包括那个剧本,我想进行条件检查,如下所示:
- name: debug message
debug:
msg: There is more then 1 interface in the machine
when: x_interfaces|length > '1'
但它不起作用。我收到此错误消息:
The error was: Unexpected templating type error occurred on ({% if x_interfaces|length > '1' %} True {% else %} False {% endif %}): '>' not supported between instances of 'int' and 'str'
我究竟做错了什么?如果我使用x_interfaces|int > '1'
它也会失败。
解决方案
length
返回一个整数。'1'
是一个字符串。正如错误消息指出的那样,比较不能在字符串和整数之间执行操作。
为此,您只需更改'1'
为1
. 您现在正在将整数与整数进行比较。
- name: debug message
debug:
msg: There is more then 1 interface in the machine
when: x_interfaces|length > 1
推荐阅读
- python - 如何并排连接两个图像?
- salesforce-lightning - 如何清除 sfdx 缓存(如果存在这种情况)
- python - 如何在pytorch中初始化不同样式的nn.Sequential块的不同层的权重?
- javascript - 如何使用异步在js中动态设置参数?
- css - Bootstrap 4:使图像大于容器
- django - django在为空时过滤Q问题
- r - 在进行计算并在 Shiny 中绘制绘图之前需要按下按钮
- docker - wordpress 容器在 docker 中工作,但无法在 kubernetes 中加载
- python - 如何解决:预计最多 1 个参数,得到 3 个
- python - 使用 PonyORM 使用可选参数进行查询