Ansible 将变量传递给角色 [英] Ansible passing variables to roles

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

问题描述

我想根据环境和主机名创建文件夹、用户和组.

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:

TASK [internal/groups_users_folders : 添加组] ********************************

TASK [internal/groups_users_folders : add the group] ******************************

失败!=> {"failed": 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 object' 没有属性 'name'\n\n错误似乎已经在'.../tasks/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 }}_{{ 主机名 }}.yml).实际上,人们通常如何处理环境和默认操作系统系列的不同任务和变量.

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 你也可以看看这个答案:Ansible:如何从另一个变量构造一个变量,然后获取它的值获取一些细节/想法

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屋!

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