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

问题描述

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

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

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


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

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

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

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

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.

推荐答案

此方案适用于Ansible 2.2.
在主要角色的vars文件中指定了从属角色的变量:

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

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

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

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

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

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

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

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

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

- debug: msg=role2

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

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

param1: hello1
param2: hello2

结果:

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

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

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

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

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

10-24 12:22