首页 > 解决方案 > 如何在 Ansible 中跳过剩余任务并创建某些条件

问题描述

我想在下面创建以下条件。任何想法如何进行?我试过When Condition,set_facts,looping [],但我没有成功。任何帮助将不胜感激。

已生成的变量示例:

version_detected: "9.0.0.7"

version_available:   "9.1.0.3"
                     "9.0.0.7",
                     "8.0.0.13"

创造条件

If "version_detected" is inside "version_available", then skip all remaining tasks and end playbook with a debug message.

If "version_detected" is not inside "version_available", then continue playbook for the next task.

下一个任务:

If "version_detected" contains "8.0", then download file www.site.com/fixpack80.gz to path

If "version_detected" contains "9.0", then download file www.site.com/fixpack90.gz to path

If "version_detected" contains "9.1", then download file www.site.com/fixpack91.gz to path

标签: ansible

解决方案


when假设这version_available是一个列表,这仅在条件下是可以实现的。

例如:

  vars:
    version_detected: "9.0.0.7"
    version_available: ["9.1.0.3", "9.0.0.7", "8.0.0.13"]

  tasks:
    - fail:
        msg: Unsupported version
      when: version_detected not in version_available

    - debug:
        msg: Do some task for version 8.0.x.x
      when: '"8.0" in version_detected'

    - debug:
        msg: Do some task for version 9.0.x.x
      when: '"9.0" in version_detected'

推荐阅读