我有nginx负载均衡器角色,其上游变量如下所示:
nginx_lb_upstreams:
- name: backend
balancing_method: least_conn
servers:
- address: 192.168.10.10
weight: 3
- address: 192.168.10.11
weight: 2
在库存中存在具有负载平衡相关权重的“AppServers”组:
[AppServers]
192.168.77.10 weight=3
192.168.77.11 weight=2
192.168.77.12 weight=1
如何正确地将AppServers
的相关权重地址传递给nginx_lb_upstreams
?类似于下面的内容,但使用循环或with_items
语句:
nginx_lb_upstreams:
- name: backend
balancing_method: least_conn
servers:
- address: groups['AppServers']
weight: groups['AppServers']['weight']
UPD :作为解决方法,我正在使用初始化servers
,如下所示:
nginx_lb_upstreams:
- name: backend
balancing_method: least_conn
servers: "{{ groups['AppServers'] }}"
在模板中使用hostvars
:
{% for server in upstream.servers %}
server {{ server }} {%- if hostvars[server].weight is defined %} weight={{ hostvars[server].weight }} {%- endif %};
{% endfor %}
答案 0 :(得分:0)
您无法按照自己的方式生成变量。那么你可能能够使用set_fact但这将是丑陋的。在您的角色中,您可以执行以下操作:
- name: show configured lbs
debug: msg="host {{item}} weight hostvars[item].weight"
with_items: groups['AppServers']