Ansible - 多个角色 [英] Ansible - multiple roles
问题描述
我正在尝试使用 with_items
命令运行多个角色,但是出现错误:
I am trying to run multiple roles using with_items
command, however I am getting error:
错误!‘项目’未定义"
"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: '{{ 主机 }}'
.问题是:Ansible 没有解析角色的变量,所以 roles: '{{ roles }}'
不起作用.
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屋!