如何使用ansible将变量从下游一个角色传递到其他依赖角色? [英] How to pass variables from one role downstream to other dependency roles with ansible?

查看:22
本文介绍了如何使用ansible将变量从下游一个角色传递到其他依赖角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用的 webserver 角色,它使用另一个 nginx 角色来生成新的虚拟服务器.

webserver/meta/main.yml 看起来像:

<前>allow_duplicates: 是依赖:- 角色:nginx名称:api虚拟服务器前端端口:{{前端端口}}"域:{{ api_domain }}"后端主机:127.0.0.1- 角色:nginx名称:门户虚拟服务器域:{{ portal_domain }}"后端主机:127.0.0.1

问题是这些变量应该被定义在 webserver-role/vars/(test|staging).yml

似乎 Ansible 会在加载变量之前尝试加载依赖项.

我该如何解决这个问题?我不想在低级角色中放入任何配置细节.

此外,我不想将配置放在剧本本身中,因为这些配置在多个剧本之间共享.

解决方案

此方案适用于 Ansible 2.2.
依赖角色的变量在主要角色的变量文件中指定:

./roles/role1/tasks/main.yml:

- 调试:msg="{{ role_param }}"

./roles/role2/meta/main.yml:

allow_duplicates: 是依赖:- 角色:角色 1role_param: "{{ param1 }}"- 角色:角色 1role_param: "{{ param2 }}"

./roles/role2/tasks/main.yml:

- 调试:msg=role2

./roles/role2/vars/main.yml:

param1: hello1参数2:你好2

结果:

PLAY [localhost] ****************************************************************任务 [role1 : debug] ***************************************************************好的:[本地主机] =>{"msg": "你好1"}任务 [role1 : debug] ***************************************************************好的:[本地主机] =>{"msg": "hello2"}任务 [角色 2 : 调试] ***************************************************************好的:[本地主机] =>{"msg": "角色 2"}

I have a generic webserver role that is using another nginx role to spawn new vservers.

webserver/meta/main.yml looks like:

allow_duplicates: yes
dependencies:
  - role: nginx
    name: api vserver
    frontend_port: "{{ frontend_port }}"
    domain: "{{ api_domain }}"
    backend_host: 127.0.0.1
  - role: nginx
    name: portal vserver
    domain: "{{ portal_domain }}"
    backend_host: 127.0.0.1

The problem is that these variables are supposed to be defined inside the webserver-role/vars/(test|staging).yml

Is seems that Ansible will try to load the dependencies before loading the variables.

How can I solve this problem? I don't want to put any configuration specifics inside the low level roles.

Also, I do not want to put configurations inside the playbook itself because these configurations are shared across multiple playbooks.

解决方案

This scenario works with Ansible 2.2.
Vars for dependent roles are specified in main role's vars files:

./roles/role1/tasks/main.yml:

- debug: msg="{{ role_param }}"

./roles/role2/meta/main.yml:

allow_duplicates: yes
dependencies:
  - role: role1
    role_param: "{{ param1 }}"
  - role: role1
    role_param: "{{ param2 }}"

./roles/role2/tasks/main.yml:

- debug: msg=role2

./roles/role2/vars/main.yml:

param1: hello1
param2: hello2

Result:

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

TASK [role1 : debug] ***********************************************************
ok: [localhost] => {
    "msg": "hello1"
}

TASK [role1 : debug] ***********************************************************
ok: [localhost] => {
    "msg": "hello2"
}

TASK [role2 : debug] ***********************************************************
ok: [localhost] => {
    "msg": "role2"
}

这篇关于如何使用ansible将变量从下游一个角色传递到其他依赖角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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