首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Ansible中导入具有布尔条件的任务或剧本

在Ansible中导入具有布尔条件的任务或剧本
EN

Stack Overflow用户
提问于 2020-07-09 01:47:00
回答 3查看 314关注 0票数 0

实际上,我需要这样的东西:

代码语言:javascript
复制
- hosts: localhost
    
  tasks:
    - name: Perforce template
      import_tasks: ./../vsphere-client/perforce/template_to_vm.yml
      when: new_server_type == "perforce"
    
    - name: Gitlab Pgbouncer template
      import_tasks: ./../vsphere-client/gitlab-pgbouncer/template_to_vm.yml
      when: new_server_type == "gitlab-pgbouncer"
    
    - name: Gitlab Postgres template
      import_tasks: ./../vsphere-client/gitlab-postgres/template_to_vm.yml
      when: new_server_type == "gitlab-postgres"
    
    - name: Build API template
      import_tasks: ./../vsphere-client/build-api/template_to_vm.yml
      when: new_server_type == "build-api"

但是它会给出一个错误,比如:

代码语言:javascript
复制
ERROR! unexpected parameter type in action: <type 'bool'>

我知道这是不受支持的,但有没有办法做到这一点?

EN

回答 3

Stack Overflow用户

发布于 2020-07-09 04:15:51

一定有其他东西你没有显示,因为你的代码看起来没问题,应该可以工作。我怀疑问题出在您正在导入的文件中。

同时,如果切换到include_tasks没有问题,您可以通过一项任务大大缩短上述时间:

代码语言:javascript
复制
---
- hosts: localhost

  tasks:
    - name: "{{ new_server_type }} template"
      include_tasks: "./../vsphere-client/{{ new_server_type }}/template_to_vm.yml"

如果您确实需要检查以确保new_server_type具有正确的值,仍然可以这样做。

代码语言:javascript
复制
---
- hosts: localhost

  vars:
   allowed_types:
     - perforce
     - gitlab-pgbouncer
     - gitlab-postgres
     - build-api

  tasks:
    - name: "{{ new_server_type }} template"
      include_tasks: "./../vsphere-client/{{ new_server_type }}/template_to_vm.yml"
      when: new_server_type in allowed_types
票数 2
EN

Stack Overflow用户

发布于 2020-07-09 04:16:32

你有没有试过使用include_tasks ie。类似于下面的内容

代码语言:javascript
复制
- include_tasks: setup-RedHat.yml
  when: ansible_os_family == 'RedHat'

因此,对于您来说,它将如下所示

代码语言:javascript
复制
- hosts: localhost

  tasks:
    - name: Perforce template
      include_tasks: ./../vsphere-client/perforce/template_to_vm.yml
      when: new_server_type == "perforce"
票数 1
EN

Stack Overflow用户

发布于 2021-12-14 12:49:11

正如前面两个答案都已经暗示的那样,陷阱是import_tasksinclude_tasksdocumentation for import_tasks说:

大多数关键字,包括循环和条件,只适用于导入的任务,而不适用于此语句本身。如果您需要应用其中任何一个,请改用ansible.builtin.include_tasks。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62800665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档