Skip to content

Instantly share code, notes, and snippets.

@hyperized
Created March 3, 2016 00:06
Show Gist options
  • Save hyperized/59f2b8cbbff42e815bfc to your computer and use it in GitHub Desktop.
Save hyperized/59f2b8cbbff42e815bfc to your computer and use it in GitHub Desktop.
---
# Run like: haproxy_drain_backend.yml --extra-vars="webserver=web01"
- hosts: haproxys
serial: 1
gather_facts: true
max_fail_percentage: 0
become: true
vars:
haproxy_backend: mybackend
haproxy_ready_state: UP
haproxy_draining_state: DRAIN
haproxy_maintenance_state: MAINT
haproxy_mode_check_wait: 5 # seconds (ensure all checks are done)
haproxy_mode_change_wait: 15 # seconds
haproxy_socket_location: /var/run/haproxy.sock
tasks:
- name: sanity check
fail:
msg: "Variable {{ item }} is not defined!"
when: "{{ item }} is not defined"
with_items:
- haproxy_backend
- haproxy_mode_check_wait
- haproxy_mode_change_wait
- haproxy_ready_state
- haproxy_draining_state
- haproxy_maintenance_state
- haproxy_socket_location
- webserver
- name: doing what
debug:
msg: "Changing {{ haproxy_backend }}/{{ webserver }} to MAINT state on {{ ansible_nodename }} .."
- name: obtain current backend state (UP, DRAIN, MAINT)
shell: "echo \"show stat\" | socat {{ haproxy_socket_location }} stdio | grep {{ haproxy_backend }},{{ webserver }} | awk -F ',' '{print $18}'"
changed_when: false
register: backend_pre_state
- name: initial status
debug:
msg: "{{ backend_pre_state.stdout }}"
- name: put backend in Draining
shell: "echo \"set server {{ haproxy_backend }}/{{ webserver }} state drain\" | socat stdio {{ haproxy_socket_location }}"
register: drain_output
when: '"{{ haproxy_ready_state }}" in backend_pre_state.stdout'
# - name: output of drain action
# debug:
# msg: "{{ drain_output | to_nice_json }}"
# when: drain_output | changed
- name: wait for state change
pause:
seconds: "{{ haproxy_mode_check_wait }}"
when: drain_output | changed
- name: obtain current backend state (UP, DRAIN, MAINT)
shell: "echo \"show stat\" | socat {{ haproxy_socket_location }} stdio | grep {{ haproxy_backend }},{{ webserver }} | awk -F ',' '{print $18}'"
changed_when: false
failed_when: 'backend_post_drain_state.stdout == "{{ haproxy_ready_state }}"'
when: drain_output | changed
register: backend_post_drain_state
- name: current state
debug:
msg: "{{ backend_post_drain_state.stdout }}"
when: backend_post_drain_state.stdout is defined
- name: wait for mode change
pause:
seconds: "{{ haproxy_mode_change_wait }}"
when: backend_post_drain_state.stdout is defined
- name: put backend in Maintenance
shell: "echo \"disable server {{ haproxy_backend }}/{{ webserver }}\" | socat stdio {{ haproxy_socket_location }}"
register: maintenance_output
when: haproxy_maintenance_state != backend_pre_state.stdout
# - name: output of maintenance action
# debug:
# msg: "{{ maintenance_output | to_nice_json }}"
# when: maintenance_output | changed
- name: wait for state to change
pause:
seconds: "{{ haproxy_mode_check_wait }}"
when: maintenance_output | changed
- name: obtain current backend state (UP, DRAIN, MAINT)
shell: "echo \"show stat\" | socat {{ haproxy_socket_location }} stdio | grep {{ haproxy_backend }},{{ webserver }} | awk -F ',' '{print $18}'"
changed_when: false
failed_when: 'backend_post_maintenance_state.stdout != "{{ haproxy_maintenance_state }}"'
when: maintenance_output | changed
register: backend_post_maintenance_state
- name: current state
debug:
msg: "{{ backend_post_maintenance_state.stdout }}"
when: backend_post_maintenance_state.stdout is defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment