From d2b2492c16711dbf1971ef513bcd43d33339cf38 Mon Sep 17 00:00:00 2001 From: Andy McCrae Date: Thu, 14 Dec 2017 13:49:38 +1000 Subject: [PATCH] Add ability to manage non-inventory hosts We may want to load balance to existing services that we aren't managing via ansible. Currently the hosts have to exist in the inventory in order to add a VIP for these hosts, this patch adds the ability to set the hostnames and addresses of the hosts manually when they aren't in the ansible inventory. Additionally, this patch adds a test for both the group method and the host_lists method. Change-Id: Ida66f401d8320d9bf14eac9b8014124631978808 --- doc/source/configure-haproxy.rst | 19 ++++++++++++++++ .../non_inventory_hosts-c0fa4c185a01e78b.yaml | 6 +++++ templates/service.j2 | 14 ++++++++---- tests/host_vars/localhost.yml | 2 ++ tests/test-vars.yml | 22 +++++++++++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/non_inventory_hosts-c0fa4c185a01e78b.yaml create mode 100644 tests/host_vars/localhost.yml diff --git a/doc/source/configure-haproxy.rst b/doc/source/configure-haproxy.rst index 8c1511e..fbc84f9 100644 --- a/doc/source/configure-haproxy.rst +++ b/doc/source/configure-haproxy.rst @@ -167,6 +167,25 @@ An example HTTP service could look like: haproxy_port: 10000 haproxy_balance_type: http +Additionally, you can specify haproxy services that are not managed +in the Ansible inventory by manually specifying their hostnames/IP Addresses: + +.. code-block:: yaml + + haproxy_extra_services: + - service: + haproxy_service_name: extra-non-inventory-service + haproxy_backend_nodes: + - name: nonInvHost01 + ip_addr: 172.0.1.1 + - name: nonInvHost02 + ip_addr: 172.0.1.2 + - name: nonInvHost03 + ip_addr: 172.0.1.3 + haproxy_ssl: "{{ haproxy_ssl }}" + haproxy_port: 10001 + haproxy_balance_type: http + Adding additional global VIP addresses ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/releasenotes/notes/non_inventory_hosts-c0fa4c185a01e78b.yaml b/releasenotes/notes/non_inventory_hosts-c0fa4c185a01e78b.yaml new file mode 100644 index 0000000..43a6c48 --- /dev/null +++ b/releasenotes/notes/non_inventory_hosts-c0fa4c185a01e78b.yaml @@ -0,0 +1,6 @@ +--- +features: + - HAProxy services that use backend nodes that are not + in the Ansible inventory can now be specified manually + by setting ``haproxy_backend_nodes`` to a list of + ``name`` and ``ip_addr`` settings. diff --git a/templates/service.j2 b/templates/service.j2 index 773f356..243a635 100644 --- a/templates/service.j2 +++ b/templates/service.j2 @@ -99,10 +99,13 @@ backend {{ item.service.haproxy_service_name }}-back {% for host_name in item.service.haproxy_backend_nodes %} +{% if hostvars[host_name] is defined %} +{% set ip_addr = hostvars[host_name]['ansible_host'] %} +{% endif %} {% set entry = [] %} {% set _ = entry.append("server") %} -{% set _ = entry.append(host_name | string) %} -{% set _ = entry.append(hostvars[host_name]['ansible_host'] + ":" + haproxy_backend_port | string) %} +{% set _ = entry.append((host_name.name | default(host_name)) | string) %} +{% set _ = entry.append((host_name.ip_addr | default(ip_addr)) + ":" + haproxy_backend_port | string) %} {% set _ = entry.append("check") %} {% set _ = entry.append("port") %} {% set _ = entry.append(haproxy_check_port | string) %} @@ -116,10 +119,13 @@ backend {{ item.service.haproxy_service_name }}-back {% endfor %} {% for host_name in item.service.haproxy_backup_nodes|default([]) %} +{% if hostvars[host_name] is defined %} +{% set ip_addr = hostvars[host_name]['ansible_host'] %} +{% endif %} {% set entry = [] %} {% set _ = entry.append("server") %} -{% set _ = entry.append(host_name | string) %} -{% set _ = entry.append(hostvars[host_name]['ansible_host'] + ":" + haproxy_backend_port | string) %} +{% set _ = entry.append((host_name.name | default(host_name)) | string) %} +{% set _ = entry.append((host_name.ip_addr | default(ip_addr)) + ":" + haproxy_backend_port | string) %} {% set _ = entry.append("check") %} {% set _ = entry.append("port") %} {% set _ = entry.append(haproxy_check_port | string) %} diff --git a/tests/host_vars/localhost.yml b/tests/host_vars/localhost.yml new file mode 100644 index 0000000..39e5c64 --- /dev/null +++ b/tests/host_vars/localhost.yml @@ -0,0 +1,2 @@ +--- +ansible_host: 127.0.0.1 diff --git a/tests/test-vars.yml b/tests/test-vars.yml index 0274176..e22fb46 100644 --- a/tests/test-vars.yml +++ b/tests/test-vars.yml @@ -1,2 +1,24 @@ --- external_lb_vip_address: 127.0.0.1 +internal_lb_vip_address: 127.0.0.1 +haproxy_service_configs: + - service: + haproxy_service_name: test_group + haproxy_backend_nodes: "{{ groups['haproxy_all'] | default([]) }}" + haproxy_port: 8180 + haproxy_backend_port: 22 + haproxy_ssl: False + haproxy_balance_type: tcp + haproxy_backend_options: + - tcp-check + - service: + haproxy_service_name: test_list + haproxy_backend_nodes: + - name: "localhost" + ip_addr: "127.0.0.1" + haproxy_port: 8181 + haproxy_backend_port: 22 + haproxy_ssl: False + haproxy_balance_type: tcp + haproxy_backend_options: + - tcp-check