loop over a group of hosts when setting up upstream servers
replicajune opened this issue · comments
Hi,
I would like to have an abstraction when configuring servers in nginx_upstreams
. to keep enough flexibility to deploy a playbook without the need to reconfigure my variables. The idea would be to had an host in the inventory, run the playbook and have something working 🙂
I found a way to do that in the template, this would look something like this :
{% for host in groups['myphpgroup'] %}
server {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}:9000
{% endfor %}
This obviously isn't perfect because it cause the role to force anyone to setup a specific inventory with myphpgroup
group somewhere including every host serving php (in my case).
if I set a variable (once and for all) like nginx_upstreams_group_php-fpm
referencing the group of machines I want to setup as upstreams, this would resolve this issue.
the constraint here is to set up a 'meta' variable like : nginx_upstreams_group_{{ nginx_upstreams.name }} and seems to be good enough for what I would like to implement
I'm guessing that, in the template, this could be implemented by something like :
{% for host in groups['nginx_upstreams_group_{{ nginx_upstreams.name }}'] %}
server {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}:9000
{% endfor %}
after a quick test, groups['nginx_upstreams_group_{{ nginx_upstreams.name }}'] is actually resolving to something 😁 so it look like it's faisable
what do you guys think ?
I noticed that if I want to test before looping over nginx_upstreams_group_{{ nginx_upstreams.name }}, i'll need to check the existence of something.
Therefore the implementation i'll do should probably more be something like nginx_upstreams_group.{{ nginx_upstreams.name }} with nginx_upstreams_group being a list and lopped over afterwards. That way, I can test the existance of nginx_upstreams_group and loop on it if it exist
I also fix a weird behaviour with the trailing semi coma at :
log_format main {{ nginx_log_format|indent(23) }}
I was having an error with nginx :
nginx: [emerg] unexpected ";" in /etc/nginx/nginx.conf:27
An exemple of this change when setting variables :
nginx_upstreams:
- name: php-fpm
strategy: "least_conn"
group: groupofmachines
port: 9000
Where groupofmachines
is a list of machines that will be set as upstream servers
Groupofmachines and name can be different.
hope it'll be useful for someone else !
UP
Thanks @replicajune for the proposal and for the PR.
I think this feature needs to be added to the role.