Ansible:当 variable/fact 大于或等于 AND 小于或等于?
Ansible: when variable/fact is greater than or equal to AND less than or equal to?
正如问题所暗示的,如果值大于或等于一个数字且小于或等于另一个数字,我正在尝试评估 Ansible 角色中的事实;基本上是一个范围。我似乎找不到如何执行此操作。
这是我的剧本片段的一部分:
- 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")
上面的代码确实有效,它 returns 两个项目,一个是 80 天,一个是 102 天。
现在我想获取年龄在 1 到 100 之间的所有快照。我试过这样做:
- 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")
但这没有用。然后我尝试了:
- 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"))
这也不起作用,所以我想知道我在这里做错了什么。这一定是我忽略的东西。
这是因为您使用的不是整数而是字符串。
此剧本无效:
- 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"
但是这个有效:
- 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
如果你不能使用整数,你可以用 int
filter 像 :
- 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>
正如问题所暗示的,如果值大于或等于一个数字且小于或等于另一个数字,我正在尝试评估 Ansible 角色中的事实;基本上是一个范围。我似乎找不到如何执行此操作。
这是我的剧本片段的一部分:
- 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")
上面的代码确实有效,它 returns 两个项目,一个是 80 天,一个是 102 天。
现在我想获取年龄在 1 到 100 之间的所有快照。我试过这样做:
- 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")
但这没有用。然后我尝试了:
- 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"))
这也不起作用,所以我想知道我在这里做错了什么。这一定是我忽略的东西。
这是因为您使用的不是整数而是字符串。
此剧本无效:
- 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"
但是这个有效:
- 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
如果你不能使用整数,你可以用 int
filter 像 :
- 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>