ansible if else 语句到 运行 只需要任务

ansible if else statement to run only required task

我正在编写应 运行 条件

的剧本

例如,当 choice = repos 我想 运行 第一个任务,如果 choice = projects 我想 运行 第二个任务

tasks:
  - name: Run python script for generating Empty Repos Report
    command: python GetRepos.py -o {{ org }} -p {{ pat }}
    register: result
  - debug: msg="{{result.stdout}}"
    when:
       - "{{ choice }}" == "Repos"

  - name: Run python script for generating Empty Repos Report
    command: python GetRepos.py -o {{ org }} -p {{ pat }}
    register: result
  - debug: msg="{{result.stdout}}"
    when:
      - "{{ choice }}" == "Projects"

由于语法错误,这给了我错误,实现此目的的正确语法是什么?

这是正确的语法

tasks:

  - debug:
      msg: Run first task
    when: choice == "Repos"

  - debug:
      msg: Run second task
    when: choice == "Projects"

可选地,使用默认值以避免由于未定义的变量选择而导致崩溃。例如

    when: choice|default("Undefined") == "Projects"