Ansible-多个角色 [英] Ansible - multiple roles

查看:97
本文介绍了Ansible-多个角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 with_items 命令运行多个角色,但是出现错误:

I am trying to run multiple roles using with_items command, however I am getting error:

错误!'item'未定义"

"ERROR! 'item' is undefined"

role.yml :

---
- hosts: '{{ host }}'
  become: yes

  roles:
    - role: "{{item}}"
      with_items: "{{ roles }}"

这是我的命令:

ansible-playbook -i ./inventory/Dev ./playbooks/role.yml --extra-vars='{"host": "db", "roles": ["mysql", "apache"]}'

推荐答案

您不能以这种方式进行操作. with _ 循环对于角色无效.

You cannot do it this way. with_ loops are not valid for roles.

如果有的话,您需要为 roles:指令提供角色列表,因此语法类似于主机组 hosts:'{{host}}'.问题是:Ansible无法解析角色变量,因此 roles:'{{role}}'不起作用.

If anything, you need to provide a list of roles to the roles: directive, so the syntax would be just like for the list of host groups hosts: '{{ host }}'. The problem is: Ansible does not resolve the variable for roles, so roles: '{{ roles }}' does not work.

但是,您可以使用 include_role模块您可以在其中访问变量.

What you can do, however, is to use include_role module in which you can access the variables.

否,include_role模块也不将 with_items 中的 {{item}} 用作 name 的值.

No, include_role module doesn't take {{ item }} from the with_items as a value for name either.

因此,我能想到的唯一解决方法(假设您不想预先处理JSON)是静态包含角色:

So the only workaround I can think of (assuming you don't want to process the JSON beforehand) is to the include the roles statically:

tasks:
  - include_role:
      name: "mysql"
    when: "'mysql' in roles"
  - include_role:
      name: "apache"
    when: "'apache' in roles"

无论如何,角色都必须存在于控制机器上,因此所有角色的名称都是预定义的.

The roles need to exist on the control machine anyway, so all their names are predefined.

这篇关于Ansible-多个角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆