简体   繁体   English

ansible playbook 多个当条件包含选项时

[英]ansible playbook multiple when condition with include option

I am creating playbook where i am taking input from user of which software version need to download as input and passing into tasks/main.yml我正在创建剧本,我从用户那里获取需要下载哪个软件版本作为输入并传递到 tasks/main.yml 的输入

ansible-playbook -i inventory.txt --extra-vars 'git_version=2.7.1'

tasks/main.yml :任务/main.yml :

---
  - name: git
    include: ../../deploy/templates/git/git.yml
    when: git_version == "1.7.4" or
          git_version == "2.7.1" 

  - name: ant
    include: ../../deploy/templates/ant/ant.yml
    when: ant_version == "1.10.3" or
          ant_version == "1.10.4"

git.yml : git.yml :


- name: Download Software
  get_url:
     url: "/Linux/git/{{ git_version }}/git-{{ git_version }}.tar.gz"
     dest: "{{ Tools }}"
     mode: 0755

So when i run playbook i am getting below error as syntax problem my requirement is take user input of which software he need to download and using multiple when condition with different include /import_task option to download software因此,当我运行 playbook 时,由于语法问题而出现以下错误,我的要求是让用户输入他需要下载的软件,并在条件不同的情况下使用多个包含 /import_task 选项来下载软件

TASK [Gathering Facts] ***************************************************************************************************
    ok: []

    TASK [rtt : Download Software] ********************************************************************************************
    skipping: []

    TASK [rtt : Unarchive software] *******************************************************************************************
    skipping: []

    TASK [rtt : Delete the tar file] ******************************************************************************************
    skipping: []

    TASK [rtt : Download Software] ********************************************************************************************

To the best of my knowledge, Ansible doesn't allow you to have when: conditions for undefined variables, with one exception:据我所知,Ansible 不允许你有when:未定义变量的条件,只有一个例外:

when: variable_name is defined

For your purposes, it may be simpler to just set empty defaults, eg create defaults/main.yml with the following content:出于您的目的,设置空默认值可能更简单,例如使用以下内容创建defaults/main.yml

---
ant_version: ''
git_version: ''

This allows the role to execute and tasks are only run when user's override the default variables (eg when user adds --extra-vars 'git_version=2.7.1' to playbook command).这允许角色执行并且任务仅在用户覆盖默认变量时运行(例如,当用户将--extra-vars 'git_version=2.7.1'到 playbook 命令时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM