带条件的Ansible即席命令

带条件的Ansible即席命令,ansible,adhoc,Ansible,Adhoc,我想运行此剧本的Ansible临时命令 --- - 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" 我有这个命令,

我想运行此剧本的Ansible临时命令

---
- 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'

ansible localhost -m debug -a msg="{{ ansible_version.full }}"
when:
是jinja2
if
语句的合成糖,因此您可以使用:

ansible localhost -m {% if ansible_version.full >= '2.7.4' %}debug{% endif %} -a msg="{{ ansible_version.full }}"
至于如何完全跳过任务,你必须找到一个模块,你可以把它放在那里,而不会导致任何事情发生,比如
暂停
,时间
秒=0
或诸如此类,或者编写你自己的
noop:
模块


另外,虽然我刚刚逐字复制了您的
when:
语句,但我认为正确的条件是使用:


因为您所写的内容将在词汇上进行比较,而不使用版本语义,因此
2.10.4
被视为
=
2.7
,因为
2.1
在排序列表中出现得比
2.7

更早,看起来如果版本为<2.7.4],我将无法使用失败模块失败。当我使用上面的adhoc命令(将“debug”替换为“fail”)时,该命令将不受影响地失败。该命令将不受影响地失败,这意味着什么
ansible-v-i localhost,-c local-m'{%if 1==1%}debug{%else%}fail{%endif%}'*'
完全按照我说的那样执行,并且更改为
1==0
调用
fail
模块