条件角色包含在Ansible中失败 [英] Conditional role inclusion fails in Ansible
问题描述
我想有条件地运行Ansible角色,即仅当不存在某些二进制文件时(对我而言,这意味着缺少某些特定的应用程序安装).
I want to run an Ansible role conditionally, i.e. only when some binary does NOT exist (which for me implies absence of some particular app installation).
类似于此处使用的模式.
在我的剧本中使用以下代码:
Using the following code in my playbook:
- hosts: my_host
tasks:
- name: check app existence
command: /opt/my_app/somebinary
register: myapp_exists
ignore_errors: yes
roles:
- { role: myconditional_role, when: myapp_exists|failed }
- another_role_to_be_included_either_way
以下是输出:
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [my_host ]
TASK [ myconditional_role : create temporary installation directory] ***********************
fatal: [my_host]: FAILED! => {"failed": true, "msg": "ERROR! The conditional check 'myapp_exists|failed' failed. The error was: ERROR! |failed expects a dictionary"}
为什么条件检查失败?
在Ubuntu 16.04.01
上使用ansible 2.0.0.2
btw:创建临时安装目录"是有条件包含角色的第一个主要任务的名称.
btw: "create temporary installation directory" is the name of the first main task of the conditionally included role.
推荐答案
任务是在角色之后执行的,因此myapp_exists
是不确定的.
改为使用pre_tasks
.
Tasks are executed after roles, so myapp_exists
is undefined.
Use pre_tasks
instead.
还请记住,when
实际上不是有条件的角色,它只是将此when
语句附加到您角色中的每个任务上.
Also keep in mind that when
is not actually a conditional role, it just attaches this when
statement to every task in your role.
这篇关于条件角色包含在Ansible中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!