Ansible将变量传递给角色 [英] Ansible passing variables to roles
问题描述
我想根据环境和主机名创建文件夹,用户和组.
I would like to create folders, users and groups depending on the environment and hostname.
我的剧本将环境和主机名传递给该角色,该角色应该从defaults/main.yml文件中选择正确的数据.
My playbook passes the environment and hostname to the role, which is supposed to pick the correct data from the defaults/main.yml file.
剧本:
roles:
- { role: myrole, environment: 'xxx', hostname: 'yyy' }
myrole/tasks/main.yml
myrole/tasks/main.yml
- name: add the group
group: name={{ item }} state=present
with_items: "{{ environment }}_{{ hostname }}_groups"
myrole/defaults/main.yml
myrole/defaults/main.yml
xxx_yyy_groups: [ '1', '2', '3', ]
但是,当我将变量传递给角色时,它无法使用defaults/main.yml文件,并且出现以下错误:
However when I pass variables to my role it fails to use the defaults/main.yml file and I get the following error:
任务[internal/groups_users_folders:添加组] **********************************
TASK [internal/groups_users_folders : add the group] ******************************
失败! => {失败":true,"msg":环境必须是字典, 收到xxx()}
FAILED! => {"failed": true, "msg": "environment must be a dictionary, received xxx ()"}
为什么Ansible无法再访问defaults文件夹中的变量? 还是有一种更优雅的方式?
Why can't Ansible access the variables in the defaults folder anymore? Or is there a more elegant way?
显然Ansible不喜欢将该变量称为环境",而且我无法像这样将with_items变量粘合在一起.
Apparently Ansible doesn't like the variable to be called "environment" and I can't glue together the with_items variable like this.
- name: set groups variable
set_fact:
groups: "{{ env }}_{{ hostname }}_groups"
- name: add groups
group: name={{ item }} state=present
with_items: "{{ groups }}"
似乎可以使用数组,如果我使用哈希并想使用item.xxx访问变量,则此错误返回:
Seems to work with arrays, if I use a hash and want to access the variables with item.xxx this error returns:
失败! => {"failed":true,"msg":字段'args'无效 值,其中似乎包含未定义的变量.这 错误为:"unicode对象"没有属性名称" \ n \ n 似乎已经在 '.../任务/main.yml': 第25行第3列,但可能\ n不在文件的其他位置,具体取决于 确切的语法问题.\ n \ n出现问题的行似乎是:\ n \ n \ n- 名称:添加用户\ n ^这里\ n}
FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'unicode object' has no attribute 'name'\n\nThe error appears to have been in '.../tasks/main.yml': line 25, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: add users\n ^ here\n"}
推荐答案
与with_items: "{{ env }}_{{ hostname }}_groups"
一起使用with_items的方式有点错误.
实际上,您传递了字符串"xxx_yyy_groups",但您需要将其作为变量名传递.
You use with_items in a bit wrong way with with_items: "{{ env }}_{{ hostname }}_groups"
.
In fact you pass string "xxx_yyy_groups", but you need to pass it as a variable name.
我做了一些研究,看来不可能以这种方式进行.您要么需要使用事实,要么使用变量加载文件(例如
{{ env }}_{{ hostname }}.yml
).
实际上,这是人们通常如何处理环境和默认OS系列的不同任务和变量.
I did some research and it looks like it isn't possible to do it this way. You either need to use fact as you do or load file with variables(e.g.
{{ env }}_{{ hostname }}.yml
).
Actually it how normally people handle different tasks and variables for environments and for default OS families.
我希望这会有所帮助!
UPD you can also take a look on this answer:Ansible: how to construct a variable from another variable and then fetch it's value to get some details/thoughts
这篇关于Ansible将变量传递给角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!