在检查模式下运行时跳过Ansible任务?
我正在写一本Ansible剧本,有一项任务在检查模式下总是会失败:在检查模式下运行时跳过Ansible任务?,ansible,ansible-playbook,Ansible,Ansible Playbook,我正在写一本Ansible剧本,有一项任务在检查模式下总是会失败: hosts: ... tasks: - set_fact: filename="{{ansible_date_time.iso8601}}" - file: state=touch name={{filename}} - file: state=link src={{filename}} dest=latest --- - name: create artifact directory file:
hosts: ...
tasks:
- set_fact: filename="{{ansible_date_time.iso8601}}"
- file: state=touch name={{filename}}
- file: state=link src={{filename}} dest=latest
---
- name: create artifact directory
file: {{ artifact_dest_dir }} state=directory
always_run: true
- name: download artifact on the remote host
get_url:
url={{ artifact_url }}
dest={{ artifact_dest_dir }}/{{ artifact_filename }}
force=yes
always_run: true
- name: unpack build artifact
unarchive: src={{ artifact_dest_dir }}/{{ artifact_filename }}
dest={{ artifact_dest_dir }}
copy=no
在检查模式下,不会创建文件,因此链接
任务将始终失败。在检查模式下运行时,是否有方法标记要跳过的任务?比如:
- file: state=link src={{filename}} dest=latest
when: not check_mode
当:filename被定义为所有任务时,您可能只需设置一个
。缺点是,如果未定义filename
,则无法在正常模式下失败
hosts: ...
tasks:
- set_fact: filename="{{ansible_date_time.iso_8601}}"
- file: state=present name={{filename}}
when: filename is defined
- file: state=link src={{filename}} dest=latest
when: filename is defined
我在unarchive
中也遇到过同样的情况:
如果存档不存在,并且目标目录不存在,则取消存档
将在检查模式下失败(这两个步骤都是在取消存档
之前的步骤中完成的)。
我通过将准备步骤设置为始终\u run:true
来解决此问题,以便它们也在检查模式下执行:
hosts: ...
tasks:
- set_fact: filename="{{ansible_date_time.iso8601}}"
- file: state=touch name={{filename}}
- file: state=link src={{filename}} dest=latest
---
- name: create artifact directory
file: {{ artifact_dest_dir }} state=directory
always_run: true
- name: download artifact on the remote host
get_url:
url={{ artifact_url }}
dest={{ artifact_dest_dir }}/{{ artifact_filename }}
force=yes
always_run: true
- name: unpack build artifact
unarchive: src={{ artifact_dest_dir }}/{{ artifact_filename }}
dest={{ artifact_dest_dir }}
copy=no
在我的情况下,它是有效的,但对于依赖时间的目录,这可能不是一个好的解决方案。这里有一种黑客解决方案:
hosts: ...
tasks:
- command: /bin/true
register: noop_result
- set_fact: check_mode={{ noop_result|skipped }}
- set_fact: filename="{{ansible_date_time.iso_8601}}"
- file: state=touch name={{filename}}
- file: state=link src={{filename}} dest=latest
when: not check_mode
在检查模式下,将跳过命令
任务,因此检查模式
将设置为真
。当不在检查模式下时,任务应该总是成功的,check\u mode
将设置为false
虽然已经有了一个问题,但我想指出的是,所提到的解决方案对我不起作用,因为我不断遇到以下错误:跳过需要一个字典
对于我来说,最终的结果是通过传递带有-e
标志的额外变量,实现了一个稍微不那么粗糙的解决方案,如下所示:
# On the terminal
ansible-playbook [...] --check -e '{"check_mode":true}'
# In the playbook or role
when: [...] and not check_mode
# In the proper `group_vars/` file
check_mode: false
让我知道你们的想法 需要注意的其他选项是标签或--step
选项
标签
tasks:
- set_fact: filename="{{ansible_date_time.iso8601}}"
- file: state=touch name={{filename}}
- file: state=link src={{filename}} dest=latest
tags:
- test
那么要使用的Ansible命令是:
ansible playbook example.yml--跳过标记“test”--检查
还有其他一些示例可以跳过/指定要使用中的标记运行的任务
开始和步骤
Ansible还通过--step
选项提供了一种很好的分步调试模式
运行ansible playbook example.yml--step--check
将以交互方式引导您完成playbook中的每个任务
从中的“开始和步骤”部分:
这将导致ansible在每个任务上停止,并询问是否应该执行该任务。假设您有一个名为“配置ssh”的任务,playbook运行将停止并询问:
执行任务:配置ssh(y/n/c):
回答“y”将执行该任务,回答“n”将跳过该任务,回答“c”将继续执行所有剩余任务而不询问
Ansible 2.1支持Ansible\u check\u模式
magic变量,该变量在check模式下设置为True
)。这意味着您将能够执行以下操作:
- file:
state: link
src: '{{ filename }}'
dest: latest
when: not ansible_check_mode
或
无论你喜欢哪个。我更关心的是一般性问题,而不是这个具体案例。无论如何,这甚至不适用于具体案例。问题是,当第一个文件任务未执行时,第二个文件任务失败,因为链接目标不存在。在我的实际用例中,创建文件的步骤是我在检查模式下绝对不想运行的操作。我很好奇为什么我的解决方案不适用于您。您使用的是什么版本的Ansible?如果在设置事实之前添加debug:var=noop\u result
任务,会发生什么?他们最终完成了:可能会在下一个ansible版本中。@SelivanovPavel-Woo!我想指出,了解正在使用的模块如何处理检查模式很重要,请确保您了解在测试剧本时忽略了什么。此外,当使用ignore\u errors
时,如果任务中未定义变量,则playbook运行不会失败;当:not ansible\u check\u mode
跳过任务时,playbook运行将失败。因此,再次强调,在检查模式下修改任务行为时,理解您试图测试的内容非常重要。