diff --git a/defaults/main.yml b/defaults/main.yml index 0062287..25ed85c 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,2 +1,26 @@ --- # defaults file for ansible-consul +consul_server: True +consul_ui: True +consul_iface: "{{ ansible_default_ipv4.interface }}" +consul_bind_address: "{{ hostvars[inventory_hostname]['ansible_'+ consul_iface | replace('-', '_')]['ipv4']['address']}}" +consul_client_addr: "" +consul_bootstrap: False + +consul_data_dir: "/opt/consul" +consul_datacenter: "dc1" +consul_gossip_encryption_key: + +consul_ansible_group: "consul" +consul_servers_list: "\ + {% set _consul_servers_list = [] %}\ + {% for host in groups[consul_ansible_group] %}\ + {% set _consul_server = hostvars[host]['consul_server']|default(true,true) %}\ + {% if ( _consul_server == True) %}\ + {% if _consul_servers_list.append(host) %}{% endif %}\ + {% endif %}\ + {% endfor %}\ + {{ _consul_servers_list }}" + +consul_join: [] +consul_join_wan: [] diff --git a/tasks/main.yml b/tasks/main.yml index 270645b..1699e4f 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,2 +1,27 @@ --- -# tasks file for ansible-consul +- name: Include OS-specific variables + include_vars: "{{ item }}" + with_first_found: + - files: + - "{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml" + - "{{ ansible_os_family }}.yml" + +- name: install package + package: + name: "{{ consul_os_package }}" + state: present + +- name: apply config template + block: + - name: server template + template: + src: config.hcl.j2 + dest: /etc/consul.d/config.hcl + owner: consul + mode: 644 + +- name: ensure service is started + systemd: + name: "{{ consul_service_name }}" + state: started + enabled: True diff --git a/templates/config.hcl.j2 b/templates/config.hcl.j2 new file mode 100644 index 0000000..f2aa47d --- /dev/null +++ b/templates/config.hcl.j2 @@ -0,0 +1,41 @@ + +bootstrap= {{consul_bootstrap|lower}} +server= {{consul_server|lower}} + +{% if consul_bind_address %} +bind_addr="{{consul_bind_address}}" +{% endif %} +{% if consul_client_addr %} +client_addr="{{consul_client_addr}}" +{% endif %} + +{%if consul_ui %} +ui_config { + enabled = true +} +{%endif%} + +{% if consul_gossip_encryption_key %} +encrypt= "{{consul_gossip_encryption_key}}" +{% endif %} + + +datacenter= "{{consul_datacenter}}" +data_dir= "{{consul_data_dir}}" +acl { + enabled = false + default_policy = "allow" + enable_token_persistence = true +} + +{% for server in _consul_lan_servers %} +{% set _ = consul_join.append(hostvars[server]['consul_bind_address'] | default(hostvars[server]['ansible_default_ipv4']['address'],true) | mandatory) %} +{% endfor %} +retry_join= {{ consul_join | map('ipwrap') | list | to_json }} + +{% if _consul_wan_servercount | int > 0 %} + {% for server in _consul_wan_servers %} + {% set _ = consul_join_wan.append(hostvars[server]['consul_bind_address']) %} + {% endfor %} +retry_join_wan= {{ consul_join_wan | map('ipwrap') | list | to_json }} +{% endif %} diff --git a/vars/Archlinux.yml b/vars/Archlinux.yml new file mode 100644 index 0000000..0b4fda5 --- /dev/null +++ b/vars/Archlinux.yml @@ -0,0 +1,6 @@ +--- +consul_os_package: + - consul + - consul-template + +consul_service_name: consul diff --git a/vars/main.yml b/vars/main.yml index 95535da..b531a62 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,2 +1,23 @@ --- # vars file for ansible-consul +_consul_lan_servers: "\ + {% set __consul_lan_servers = [] %}\ + {% for server in consul_servers_list %}\ + {% set _consul_datacenter = hostvars[server]['consul_datacenter'] | default('dc1', true) %}\ + {% if _consul_datacenter == consul_datacenter %}\ + {% if __consul_lan_servers.append(server) %}{% endif %}\ + {% endif %}\ + {% endfor %}\ + {{ __consul_lan_servers }}" +_consul_lan_servercount: "{{ (_consul_lan_servers | length) }}" + +_consul_wan_servers: "\ + {% set __consul_wan_servers = [] %}\ + {% for server in consul_servers_list %}\ + {% set _consul_datacenter = hostvars[server]['consul_datacenter'] | default('dc1', true) %}\ + {% if _consul_datacenter != consul_datacenter %}\ + {% if __consul_wan_servers.append(server) %}{% endif %}\ + {% endif %}\ + {% endfor %}\ + {{ __consul_wan_servers }}" +_consul_wan_servercount: "{{ (_consul_wan_servers | length) }}"