带条件的Ansible即席命令
我想运行此剧本的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" 我有这个命令,
---
- 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:
是jinja2if
语句的合成糖,因此您可以使用:
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
模块