在Ansible中,如何将活动角色中的变量组合到一个数组中? [英] In Ansible, how to combine variables from active roles into one array?

查看:93
本文介绍了在Ansible中,如何将活动角色中的变量组合到一个数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看"在Ansible中,如何将变量从单独的文件合并到一个数组中?",答案之一是建议使用

Looking at "In Ansible, how to combine variables from separate files into one array?" one of the answers suggests using include_vars to get variables from several sources into one array, this is almost what I need but not quite.

我正在设置cloudfront_logging,它需要 awslogs_logs:数组中的项.我希望能够为我所拥有的角色添加到此数组中,所以Syslog担任我的常见角色,但是如果我有php角色,我希望它包含php日志.

I'm setting up cloudfront_logging which requires items in a awslogs_logs: array. I'd like to be able to add to this array for the roles that I have active, so Syslog in my common role but if I have a php role, I'd like it to include the php logs.

我想我可以让 include_vars 适用于所有角色,但是我看不到如何使它仅适用于构建中包含的角色.因此,如果我包括php角色,请包括php日志,但如果不包括php日志,则不包括.

I think I could get include_vars to work for all roles, but I can't see how to get this to work for only the roles included in a build. So if I include the php role, include the php logs but not if it is not included.

我当然可以将数组静态地包含在顶层,但是在架构上似乎有些偏离,因为您希望角色能够处理自己的日志记录.

I could, of course, include the array at the top level statically but that seems like it is architecturally a bit off as you'd expect a role to be able to deal with its' own logging.

推荐答案

您的角色可以使用 set_fact 任务将信息附加到变量.例如,假设您希望角色能够在 logfiles 事实中注册用于记录日志文件的路径.您可以在每个角色中执行以下操作:

Your roles can use a set_fact task to append information to a variable. For example, let's say you want roles to be able to register paths to log files in the logfiles fact; you could do something like this in each role:

- set_fact:
    logfiles: "{{ logfiles|default([]) + ['/var/log/something.log', '/var/log/anotherthing.log'] }}"

换句话说,如果 roles/role1/tasks 看起来像这样:

In other words, if roles/role1/tasks looks like this:

---
- set_fact:
    logfiles: "{{ logfiles|default([]) + ['/var/log/role1.log'] }}"

角色/角色2/任务看起来像这样:

---
- set_fact:
    logfiles: "{{ logfiles|default([]) + ['/var/log/role2.log'] }}"

然后是一个看起来像这样的剧本:

Then a playbook that looks like this:

---
- hosts: localhost
  gather_facts: false
  roles:
    - role1
    - role2

  tasks:
    - debug:
        var: logfiles

将产生以下输出:


PLAY [localhost] ******************************************************************************

TASK [role1 : set_fact] ***********************************************************************
ok: [localhost]

TASK [role2 : set_fact] ***********************************************************************
ok: [localhost]

TASK [debug] **********************************************************************************
ok: [localhost] => {
    "logfiles": [
        "/var/log/role1.log", 
        "/var/log/role2.log"
    ]
}

PLAY RECAP ************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

这篇关于在Ansible中,如何将活动角色中的变量组合到一个数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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