我想运行此剧本的 Ansible ad-hoc 命令
---
- hosts: localhost
tasks:
- name: Print message if ansible version is greater than 2.7.0
debug:
msg: "Ansible version is {{ ansible_version.full }}"
when: ansible_version.full >= "2.7.4"
我有这个命令,如何包含条件“when”?
ansible localhost -m debug -a msg="{{ ansible_version.full }}"
我有这个命令,如何包含条件“when”?
The when:
是 jinja2 的语法糖if
声明,所以你会使用:
ansible localhost -m {% if ansible_version.full >= '2.7.4' %}debug{% endif %} -a msg="{{ ansible_version.full }}"
至于如何skip整个任务,你必须找到一个可以放入其中的模块,这会导致nothing发生,例如pause
with seconds=0
或类似的,或者写你自己的noop:
module
另外,虽然我刚刚复制了你的when:
逐字陈述,我相信correct条件是使用version对比测试 https://docs.ansible.com/ansible/2.7/user_guide/playbooks_tests.html#version-comparison:
ansible localhost -m debug -a msg="{% if (ansible_version.full is version('2.7.4', '>=')) %}{{ ansible_version.full }}{% endif %}"
因为你所写的内容将按字典顺序进行比较,而不是使用版本语义,所以2.10.4
would not被考虑>=
2.7
since 2.1
在排序列表中出现早于2.7
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)