ansible - Ansible: when variable/fact is greater than or equal to AND less than or equal to?
问题描述
As the question implies, I'm trying to evaluate a fact in an Ansible role if the value is greater than or equal to a number AND less than or equal to another number; basically a range. I can't seem to find how to do this.
Here's part of my playbook snippet:
- name: DEBUG Snapshots to be deleted
debug:
msg: The snapshot for {{ inventory_hostname.split("_")[0] }} is {{ snap_age }} day(s) old and would have been deleted.
when: (old_snap is defined) and (old_snap == true) and (snap_age >= "1")
This code above actually works, and it returns two items, one that is 80 days old and one that is 102 days old.
Now I want to get any snapshots that are between 1 and 100 in age. I tried doing this:
- name: DEBUG Snapshots to be deleted
debug:
msg: The snapshot for {{ inventory_hostname.split("_")[0] }} is {{ snap_age }} day(s) old and would have been deleted.
when: (old_snap is defined) and (old_snap == true) and (snap_age >= "1" and snap_age <= "100")
But that didn't work. I then tried:
- name: DEBUG Snapshots to be deleted
debug:
msg: The snapshot for {{ inventory_hostname.split("_")[0] }} is {{ snap_age }} day(s) old and would have been deleted.
when: (old_snap is defined) and (old_snap == true) and ((snap_age >= "1" and snap_age <= "100"))
That didn't work either, so I'm wondering what I'm doing wrong here. It must be something I'm overlooking.
解决方案
This is because you're not using integers but strings.
This playbook not work :
- hosts: localhost
connection: local
gather_facts: no
vars:
old_snap: true
tasks:
- name: DEBUG Snapshots to be deleted
debug:
msg: The snapshot for {{ inventory_hostname.split("_")[0] }} is {{ item }} day(s) old and would have been deleted.
when: (old_snap is defined) and (old_snap == true) and (item >= "1" and item <= "100")
with_items:
- "80"
- "102"
But this one works :
- hosts: localhost
connection: local
gather_facts: no
vars:
old_snap: true
tasks:
- name: DEBUG Snapshots to be deleted
debug:
msg: The snapshot for {{ inventory_hostname.split("_")[0] }} is {{ item }} day(s) old and would have been deleted.
when: (old_snap is defined) and (old_snap == true) and (item >= 1 and item <= 100)
with_items:
- 80
- 102
If you can't use integers, you can pipe them with int
filter like :
- hosts: localhost
connection: local
gather_facts: no
vars:
old_snap: true
tasks:
- name: DEBUG Snapshots to be deleted
debug:
msg: The snapshot for {{ inventory_hostname.split("_")[0] }} is {{ item }} day(s) old and would have been deleted.
when: (old_snap is defined) and (old_snap == true) and (item | int >= 1 and item | int <= 100)
with_items:
- "80"
- "102"
I'm using ansible 2.8.7, command is
ansible-playbook <your file>
推荐阅读
- spring-boot-admin - Spring Boot Admin 客户端在初始化期间出现异常
- maven - Maven 依赖树(按包)
- java - Java中的“===”等价物
- java - 使用 firebase-mlkit 访问云视觉 api 时套接字已关闭
- templates - Kendo UI - Datepicker 未在 Kendo-Template 中呈现
- php - PHP 的位置或路径问题
- java - 如何在 java 的流 API 中应用 if/else 逻辑。?
- excel - 我可以在 Excel 中使用 &-Operator 设置颜色/格式吗?
- javascript - 使用连字符吸引 ID 时 GetElementById 不起作用
- c# - 如何使用 RWLock 实现多读一写的场景?