Skip to content

Instantly share code, notes, and snippets.

@rmullinnix
Last active January 22, 2021 22:56
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rmullinnix/ebb5ef2bb877309aebcd to your computer and use it in GitHub Desktop.
Save rmullinnix/ebb5ef2bb877309aebcd to your computer and use it in GitHub Desktop.
Ansible playbook for consul cluster bounce - ansible-playbook -i hosts.dev cluster_bounce.yml --ask-become-pass (peers.tmpl is in roles/consul/templates; sedfile is in roles/consul/files; main.yml is in roles/consul/vars)
---
# This Playbook bounces a consul cluster that is unable to elect a leader
# push peers.json to the consul/raft data directory, change single quotes to doubles, restart consul servers
- hosts: consulservers
become: yes
# update the peers.json file with the correct consul server ip addresses and port
tasks:
- include_vars: roles/consul/vars/main.yml
- name: Create peers.json file
template: src=roles/consul/templates/peers.tmpl dest={{ consul_data_path }}/raft/peers.json owner={{ consul_user }}
- name: Copy sedfile
copy: src=roles/consul/files/sedfile dest=/tmp/sedfile
- name: Correct quotes
shell: sed -i -f /tmp/sedfile {{ consul_data_path }}/raft/peers.json
- name: restart consul server
service: name=consul state=restarted
[consulservers]
host1.domain.com
host2.domain.com
host3.domain.com
[consulagents]
host4.domain.com
host5.domain.com
---
# roles/consul/vars/main.yml
consul_user: consul
consul_server_port: 8300
consul_data_path: /usr/local/data/consul
[{% for host in groups['consulservers'] %}
"{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}:{{ consul_server_port}}"
{% if not loop.last %},{% endif %}
{% endfor %}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment