Conditional role inclusion fails in Ansible

一世执手 提交于 2019-12-11 08:25:25

问题


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).

Something like the pattern used here.

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

Here is the output:

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"}

Why is the conditional check failing?

Using ansible 2.0.0.2 on Ubuntu 16.04.01

btw: "create temporary installation directory" is the name of the first main task of the conditionally included role.


回答1:


Tasks are executed after roles, so myapp_exists is undefined.
Use pre_tasks instead.

Also keep in mind that when is not actually a conditional role, it just attaches this when statement to every task in your role.



来源:https://stackoverflow.com/questions/41564326/conditional-role-inclusion-fails-in-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!