diff --git a/ansible/roles/haproxy/defaults/main.yml b/ansible/roles/haproxy/defaults/main.yml index afce84ed2d..769df40081 100644 --- a/ansible/roles/haproxy/defaults/main.yml +++ b/ansible/roles/haproxy/defaults/main.yml @@ -1,6 +1,29 @@ --- project_name: "haproxy" +haproxy_services: + haproxy: + container_name: haproxy + group: haproxy + enabled: true + image: "{{ haproxy_image_full }}" + privileged: True + volumes: + - "{{ node_config_directory }}/haproxy/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "haproxy_socket:/var/lib/kolla/haproxy/" + keepalived: + container_name: keepalived + group: haproxy + enabled: true + image: "{{ keepalived_image_full }}" + privileged: True + volumes: + - "{{ node_config_directory }}/keepalived/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "/lib/modules:/lib/modules:ro" + - "haproxy_socket:/var/lib/kolla/haproxy/" + #################### # Docker diff --git a/ansible/roles/haproxy/handlers/main.yml b/ansible/roles/haproxy/handlers/main.yml new file mode 100644 index 0000000000..5fad8e69a6 --- /dev/null +++ b/ansible/roles/haproxy/handlers/main.yml @@ -0,0 +1,61 @@ +--- +- name: Restart haproxy container + vars: + service_name: "haproxy" + service: "{{ haproxy_services[service_name] }}" + config_json: "{{ haproxy_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + haproxy_container: "{{ check_haproxy_containers.results|selectattr('item.key', 'equalto', service_name)|first }}" + kolla_docker: + action: "recreate_or_restart_container" + common_options: "{{ docker_common_options }}" + name: "{{ service.container_name }}" + image: "{{ service.image }}" + privileged: "{{ service.privileged | default(False) }}" + volumes: "{{ service.volumes }}" + when: + - action != "config" + - inventory_hostname in groups[service.group] + - service.enabled | bool + - config_json.changed | bool + or haproxy_cfg.changed | bool + or haproxy_pem.changed | bool + or haproxy_container.changed | bool + notify: + - Waiting for virtual IP to appear + +- name: Restart keepalived container + vars: + service_name: "keepalived" + service: "{{ haproxy_services[service_name] }}" + config_json: "{{ haproxy_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + keepalived_container: "{{ check_haproxy_containers.results|selectattr('item.key', 'equalto', service_name)|first }}" + kolla_docker: + action: "recreate_or_restart_container" + common_options: "{{ docker_common_options }}" + name: "{{ service.container_name }}" + image: "{{ service.image }}" + privileged: "{{ service.privileged | default(False) }}" + volumes: "{{ service.volumes }}" + when: + - action != "config" + - inventory_hostname in groups[service.group] + - service.enabled | bool + - config_json.changed | bool + or keepalived_conf.changed | bool + or keepalived_container.changed | bool + notify: + - Waiting for virtual IP to appear + +- name: Ensuring latest haproxy config is used + command: docker exec haproxy /usr/local/bin/kolla_ensure_haproxy_latest_config + register: status + changed_when: status.stdout.find('changed') != -1 + when: + - haproxy_config_jsons.changed | bool + or haproxy_cfg.changed | bool + or haproxy_pem.changed | bool + +- name: Waiting for virtual IP to appear + wait_for: + host: "{{ kolla_internal_vip_address }}" + port: "{{ database_port }}" diff --git a/ansible/roles/haproxy/tasks/config.yml b/ansible/roles/haproxy/tasks/config.yml index b23f19f3f0..37663762da 100644 --- a/ansible/roles/haproxy/tasks/config.yml +++ b/ansible/roles/haproxy/tasks/config.yml @@ -8,43 +8,91 @@ - name: Ensuring config directories exist file: - path: "{{ node_config_directory }}/{{ item }}" + path: "{{ node_config_directory }}/{{ item.key }}" state: "directory" recurse: yes - with_items: - - "keepalived" - - "haproxy" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ haproxy_services }}" - name: Copying over config.json files for services template: - src: "{{ item }}.json.j2" - dest: "{{ node_config_directory }}/{{ item }}/config.json" - with_items: - - "keepalived" - - "haproxy" + src: "{{ item.key }}.json.j2" + dest: "{{ node_config_directory }}/{{ item.key }}/config.json" + register: haproxy_config_jsons + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ haproxy_services }}" + notify: + - "Restart {{ item.key }} container" + - Ensuring latest haproxy config is used - name: Copying over haproxy.cfg + vars: + service: "{{ haproxy_services['haproxy'] }}" template: src: "{{ item }}" dest: "{{ node_config_directory }}/haproxy/haproxy.cfg" + register: haproxy_cfg + when: + - inventory_hostname in groups[service.group] + - service.enabled | bool with_first_found: - "{{ node_custom_config }}/haproxy/{{ inventory_hostname }}/haproxy.cfg" - "{{ node_custom_config }}/haproxy/haproxy.cfg" - "haproxy.cfg.j2" + notify: + - Restart haproxy container + - Ensuring latest haproxy config is used - name: Copying over keepalived.conf + vars: + service: "{{ haproxy_services['keepalived'] }}" template: src: "{{ item }}" dest: "{{ node_config_directory }}/keepalived/keepalived.conf" + register: keepalived_conf + when: + - inventory_hostname in groups[service.group] + - service.enabled | bool with_first_found: - "{{ node_custom_config }}/keepalived/{{ inventory_hostname }}/keepalived.conf" - "{{ node_custom_config }}/keepalived/keepalived.conf" - "keepalived.conf.j2" + notify: + - Restart keepalived container - name: Copying over haproxy.pem + vars: + service: "{{ haproxy_services['haproxy'] }}" copy: src: "{{ kolla_external_fqdn_cert }}" dest: "{{ node_config_directory }}/haproxy/{{ item }}" + register: haproxy_pem + when: + - kolla_enable_tls_external | bool + - inventory_hostname in groups[service.group] + - service.enabled | bool with_items: - "haproxy.pem" - when: kolla_enable_tls_external | bool + notify: + - Restart haproxy container + - Ensuring latest haproxy config is used + +- name: Check haproxy containers + kolla_docker: + action: "compare_container" + common_options: "{{ docker_common_options }}" + name: "{{ item.value.container_name }}" + image: "{{ item.value.image }}" + volumes: "{{ item.value.volumes }}" + register: check_haproxy_containers + when: + - action != "config" + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ haproxy_services }}" + notify: + - "Restart {{ item.key }} container" diff --git a/ansible/roles/haproxy/tasks/deploy.yml b/ansible/roles/haproxy/tasks/deploy.yml index ab9215b168..dd26ecc34d 100644 --- a/ansible/roles/haproxy/tasks/deploy.yml +++ b/ansible/roles/haproxy/tasks/deploy.yml @@ -1,6 +1,5 @@ --- - include: config.yml - when: inventory_hostname in groups['haproxy'] -- include: start.yml - when: inventory_hostname in groups['haproxy'] +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/haproxy/tasks/pull.yml b/ansible/roles/haproxy/tasks/pull.yml index f005168064..ca86f7328c 100644 --- a/ansible/roles/haproxy/tasks/pull.yml +++ b/ansible/roles/haproxy/tasks/pull.yml @@ -1,14 +1,10 @@ --- -- name: Pulling keepalived image +- name: Pulling haproxy images kolla_docker: action: "pull_image" common_options: "{{ docker_common_options }}" - image: "{{ keepalived_image_full }}" - when: inventory_hostname in groups['haproxy'] - -- name: Pulling haproxy image - kolla_docker: - action: "pull_image" - common_options: "{{ docker_common_options }}" - image: "{{ haproxy_image_full }}" - when: inventory_hostname in groups['haproxy'] + image: "{{ item.value.image }}" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ haproxy_services }}" diff --git a/ansible/roles/haproxy/tasks/reconfigure.yml b/ansible/roles/haproxy/tasks/reconfigure.yml index ea2b3dfc02..e078ef1318 100644 --- a/ansible/roles/haproxy/tasks/reconfigure.yml +++ b/ansible/roles/haproxy/tasks/reconfigure.yml @@ -1,74 +1,2 @@ --- -- name: Ensuring the containers up - kolla_docker: - name: "{{ item.name }}" - action: "get_container_state" - register: container_state - failed_when: container_state.Running == false - when: inventory_hostname in groups[item.group] - with_items: - - { name: haproxy, group: haproxy } - - { name: keepalived, group: haproxy } - -- include: config.yml - -- name: Check the configs - command: docker exec {{ item.name }} /usr/local/bin/kolla_set_configs --check - changed_when: false - failed_when: false - register: check_results - when: inventory_hostname in groups[item.group] - with_items: - - { name: haproxy, group: haproxy } - - { name: keepalived, group: haproxy } - -- name: Containers config strategy - kolla_docker: - name: "{{ item.name }}" - action: "get_container_env" - register: container_envs - when: inventory_hostname in groups[item.group] - with_items: - - { name: haproxy, group: haproxy } - - { name: keepalived, group: haproxy } - -- name: Remove the containers - kolla_docker: - name: "{{ item[0]['name'] }}" - action: "remove_container" - register: remove_containers - when: - - config_strategy == "COPY_ONCE" or item[1]['KOLLA_CONFIG_STRATEGY'] == 'COPY_ONCE' - - item[2]['rc'] == 1 - - inventory_hostname in groups[item[0]['group']] - with_together: - - [{ name: haproxy, group: haproxy }, - { name: keepalived, group: haproxy }] - - "{{ container_envs.results }}" - - "{{ check_results.results }}" - -- include: start.yml - when: remove_containers.changed - -# container_envs.results is a list of two elements, first corresponds to -# haproxy container result and second to keepalived container result and the -# same applicable for check_results.results -- name: Ensuring latest haproxy config is used - command: docker exec haproxy /usr/local/bin/kolla_ensure_haproxy_latest_config - register: status - changed_when: status.stdout.find('changed') != -1 - when: - - config_strategy == 'COPY_ALWAYS' - - container_envs.results[0]['KOLLA_CONFIG_STRATEGY'] != 'COPY_ONCE' - - check_results.results[0]['rc'] == 1 - - inventory_hostname in groups['haproxy'] - -- name: Restart keepalived container - kolla_docker: - name: "keepalived" - action: "restart_container" - when: - - config_strategy == 'COPY_ALWAYS' - - container_envs.results[1]['KOLLA_CONFIG_STRATEGY'] != 'COPY_ONCE' - - check_results.results[1]['rc'] == 1 - - inventory_hostname in groups['haproxy'] +- include: deploy.yml diff --git a/ansible/roles/haproxy/tasks/start.yml b/ansible/roles/haproxy/tasks/start.yml deleted file mode 100644 index aa76e2b4a2..0000000000 --- a/ansible/roles/haproxy/tasks/start.yml +++ /dev/null @@ -1,35 +0,0 @@ ---- -- name: Starting haproxy container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ haproxy_image_full }}" - name: "haproxy" - privileged: True - volumes: - - "{{ node_config_directory }}/haproxy/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "haproxy_socket:/var/lib/kolla/haproxy/" - -- name: Starting keepalived container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ keepalived_image_full }}" - name: "keepalived" - privileged: True - volumes: - - "{{ node_config_directory }}/keepalived/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "/lib/modules:/lib/modules:ro" - - "haproxy_socket:/var/lib/kolla/haproxy/" - -- name: Ensuring latest haproxy config is used - command: docker exec haproxy /usr/local/bin/kolla_ensure_haproxy_latest_config - register: status - changed_when: status.stdout.find('changed') != -1 - -- name: Waiting for virtual IP to appear - wait_for: - host: "{{ kolla_internal_vip_address }}" - port: "{{ database_port }}" diff --git a/ansible/roles/haproxy/tasks/upgrade.yml b/ansible/roles/haproxy/tasks/upgrade.yml index ce1adcfa36..7ba6034992 100644 --- a/ansible/roles/haproxy/tasks/upgrade.yml +++ b/ansible/roles/haproxy/tasks/upgrade.yml @@ -11,9 +11,11 @@ when: kolla_internal_vip_address not in secondary_addresses # Upgrading master keepalived and haproxy -- include: start.yml +- name: Flush handlers + meta: flush_handlers when: kolla_internal_vip_address in secondary_addresses # Upgrading slave keepalived and haproxy -- include: start.yml +- name: Flush handlers + meta: flush_handlers when: kolla_internal_vip_address not in secondary_addresses