dre*_*ard 0 ansible ansible-facts
正如问题所暗示的,我试图评估 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")
Run Code Online (Sandbox Code Playgroud)
上面的代码实际上有效,它返回两项,一项是 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")
Run Code Online (Sandbox Code Playgroud)
但这没有用。然后我尝试:
- 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"))
Run Code Online (Sandbox Code Playgroud)
这也不起作用,所以我想知道我在这里做错了什么。这一定是我忽略的事情。
这是因为您使用的不是整数而是字符串。
这个剧本不起作用:
- 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"
Run Code Online (Sandbox Code Playgroud)
但这有效:
- 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
Run Code Online (Sandbox Code Playgroud)
如果您不能使用整数,您可以使用int过滤器对它们进行管道传输,例如:
- 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"
Run Code Online (Sandbox Code Playgroud)
我使用的是ansible 2.8.7,命令是
ansible-playbook <your file>
| 归档时间: |
|
| 查看次数: |
32262 次 |
| 最近记录: |