2022-03-07 20:22:15 +00:00
|
|
|
---
|
2022-06-06 14:04:47 +00:00
|
|
|
- name: Read bootstrapped state
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.stat:
|
2022-06-06 14:04:47 +00:00
|
|
|
path: "{{ consul_bootstrap_state }}"
|
|
|
|
register: bootstrap_state
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
|
2022-03-08 20:07:38 +00:00
|
|
|
- name: Include OS-specific variables
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.include_vars: "{{ item }}"
|
2022-03-08 20:07:38 +00:00
|
|
|
with_first_found:
|
|
|
|
- files:
|
|
|
|
- "{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml"
|
|
|
|
- "{{ ansible_os_family }}.yml"
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Add hashicorp repo
|
|
|
|
ansible.builtin.get_url:
|
2022-06-06 16:19:36 +00:00
|
|
|
url: "https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo"
|
|
|
|
dest: '/etc/yum.repos.d/hashicorp.repo'
|
|
|
|
owner: root
|
|
|
|
group: root
|
2023-10-31 19:08:20 +00:00
|
|
|
mode: "0644"
|
2022-06-06 16:19:36 +00:00
|
|
|
when: ansible_os_family == "RedHat"
|
2022-11-19 19:57:40 +00:00
|
|
|
- name: Add an Apt signing key, uses whichever key is at the URL
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.apt_key:
|
2022-11-19 19:57:40 +00:00
|
|
|
url: "{{ consul_repo_url }}/gpg"
|
|
|
|
state: present
|
|
|
|
when: "ansible_os_family|lower == 'debian'"
|
|
|
|
|
|
|
|
- name: Add Debian/Ubuntu Linux repository
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.apt_repository:
|
2022-11-19 19:57:40 +00:00
|
|
|
repo: "deb {{ consul_repo_url }} {{ ansible_distribution_release }} main"
|
|
|
|
state: present
|
|
|
|
update_cache: true
|
|
|
|
when: "ansible_os_family|lower == 'debian'"
|
|
|
|
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Install package
|
|
|
|
ansible.builtin.package:
|
2022-03-08 20:07:38 +00:00
|
|
|
name: "{{ consul_os_package }}"
|
|
|
|
state: present
|
2023-10-18 17:15:43 +00:00
|
|
|
when: ansible_architecture is not search('aarch*')
|
2022-03-08 20:07:38 +00:00
|
|
|
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Encure data dir exist
|
|
|
|
ansible.builtin.file:
|
2022-03-10 10:33:53 +00:00
|
|
|
state: directory
|
|
|
|
owner: consul
|
|
|
|
path: "{{ consul_data_dir }}"
|
2023-10-31 19:08:20 +00:00
|
|
|
mode: "0755"
|
2022-03-10 10:33:53 +00:00
|
|
|
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Manage encrypt key
|
|
|
|
when:
|
|
|
|
- consul_encrypt_enable | bool
|
|
|
|
block:
|
|
|
|
- name: Check encrypt key in config file
|
|
|
|
when:
|
|
|
|
- consul_raw_key is not defined
|
|
|
|
- bootstrap_state.stat.exists | bool
|
|
|
|
- inventory_hostname in consul_servers_list
|
|
|
|
block:
|
2022-06-06 14:04:47 +00:00
|
|
|
- name: Check for gossip encryption key on previously boostrapped server
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.slurp:
|
2022-06-06 14:04:47 +00:00
|
|
|
src: "{{ consul_config_path }}/config.json"
|
|
|
|
register: consul_config_b64
|
|
|
|
ignore_errors: true
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Print config
|
|
|
|
ansible.builtin.debug:
|
|
|
|
msg: "{{ consul_config_b64 }}"
|
2022-06-06 14:04:47 +00:00
|
|
|
- name: Deserialize existing configuration
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.set_fact:
|
2022-06-06 14:04:47 +00:00
|
|
|
consul_config: "{{ consul_config_b64.content | b64decode | from_json }}"
|
|
|
|
when: consul_config_b64.content is defined and consul_config_b64.content != ""
|
|
|
|
|
|
|
|
- name: Save gossip encryption key from existing configuration
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.set_fact:
|
2022-06-06 14:04:47 +00:00
|
|
|
consul_raw_key: "{{ consul_config.encrypt }}"
|
|
|
|
when: consul_config is defined
|
|
|
|
|
|
|
|
|
|
|
|
# Key provided by extra vars or the above block
|
|
|
|
- name: Write gossip encryption key locally for use with new servers
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.copy:
|
2022-06-06 14:04:47 +00:00
|
|
|
content: "{{ consul_raw_key }}"
|
|
|
|
dest: '/tmp/consul_raw.key'
|
2023-10-31 19:08:20 +00:00
|
|
|
mode: "0600"
|
2022-06-06 14:04:47 +00:00
|
|
|
become: false
|
|
|
|
vars:
|
|
|
|
ansible_become: false
|
|
|
|
no_log: true
|
|
|
|
delegate_to: localhost
|
|
|
|
changed_when: false
|
|
|
|
when: consul_raw_key is defined
|
|
|
|
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Generate new key if none was found
|
|
|
|
no_log: true
|
|
|
|
run_once: true
|
|
|
|
when:
|
|
|
|
# if files '/tmp/consul_raw.key' exist
|
|
|
|
- lookup('first_found', dict(files=['/tmp/consul_raw.key'], skip=true)) | ternary(false, true)
|
|
|
|
- not bootstrap_state.stat.exists | bool
|
|
|
|
block:
|
2022-06-06 14:04:47 +00:00
|
|
|
- name: Generate gossip encryption key
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.shell: "PATH={{ consul_bin_path }}:$PATH consul keygen"
|
2022-06-06 14:04:47 +00:00
|
|
|
register: consul_keygen
|
2023-10-31 19:08:20 +00:00
|
|
|
changed_when: false
|
2022-06-06 14:04:47 +00:00
|
|
|
|
|
|
|
- name: Write key locally to share with other nodes
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.copy:
|
2022-06-06 14:04:47 +00:00
|
|
|
content: "{{ consul_keygen.stdout }}"
|
|
|
|
dest: '/tmp/consul_raw.key'
|
2023-10-31 19:08:20 +00:00
|
|
|
mode: "0600"
|
2022-06-06 14:04:47 +00:00
|
|
|
become: false
|
|
|
|
vars:
|
|
|
|
ansible_become: false
|
|
|
|
delegate_to: localhost
|
|
|
|
- name: Read gossip encryption key for servers that require it
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.set_fact:
|
2022-06-06 14:04:47 +00:00
|
|
|
consul_raw_key: "{{ lookup('file', '/tmp/consul_raw.key') }}"
|
|
|
|
no_log: true
|
|
|
|
when:
|
|
|
|
- consul_raw_key is not defined
|
|
|
|
|
|
|
|
- name: Delete gossip encryption key file
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.file:
|
2022-06-06 14:04:47 +00:00
|
|
|
path: '/tmp/consul_raw.key'
|
|
|
|
state: absent
|
|
|
|
become: false
|
|
|
|
vars:
|
|
|
|
ansible_become: false
|
|
|
|
run_once: true
|
|
|
|
delegate_to: localhost
|
|
|
|
changed_when: false
|
|
|
|
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Delete hcl config file
|
|
|
|
ansible.builtin.template:
|
|
|
|
src: consul.hcl.j2
|
|
|
|
dest: "{{ consul_config_path }}/consul.hcl"
|
|
|
|
owner: consul
|
|
|
|
mode: "0644"
|
|
|
|
become: true
|
|
|
|
- name: Apply config template
|
|
|
|
notify: reload consul configuration
|
2022-03-08 20:07:38 +00:00
|
|
|
block:
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Server template
|
|
|
|
ansible.builtin.template:
|
2022-06-06 14:04:47 +00:00
|
|
|
src: config.json.j2
|
2023-10-31 19:08:20 +00:00
|
|
|
dest: "{{ consul_config_path }}/config.json"
|
2022-03-08 20:07:38 +00:00
|
|
|
owner: consul
|
2023-10-31 19:08:20 +00:00
|
|
|
mode: "0644"
|
2022-03-12 14:44:06 +00:00
|
|
|
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Configure backup
|
|
|
|
when: consul_snapshot
|
2022-03-12 14:44:06 +00:00
|
|
|
block:
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Copy backup script
|
|
|
|
ansible.builtin.copy:
|
2022-03-12 14:44:06 +00:00
|
|
|
dest: "{{ consul_data_dir }}/cs-backup.sh"
|
2023-10-31 19:08:20 +00:00
|
|
|
mode: "0744"
|
2022-03-12 14:44:06 +00:00
|
|
|
owner: consul
|
|
|
|
src: cs-backup.sh
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Consul snaphot cron.d
|
|
|
|
ansible.builtin.cron:
|
2022-03-12 14:44:06 +00:00
|
|
|
name: consul backup
|
|
|
|
user: consul
|
|
|
|
state: present
|
|
|
|
job: "{{ consul_data_dir }}/cs-backup.sh {{ consul_backup_location }}"
|
2023-10-31 19:08:20 +00:00
|
|
|
hour: "{{ consul_cron_hour }}"
|
2022-06-06 14:04:47 +00:00
|
|
|
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Ensure service is started
|
|
|
|
ansible.builtin.systemd:
|
2022-03-08 20:07:38 +00:00
|
|
|
name: "{{ consul_service_name }}"
|
|
|
|
state: started
|
2023-10-31 19:08:20 +00:00
|
|
|
enabled: true
|
2022-04-03 18:39:52 +00:00
|
|
|
|
2022-06-06 14:04:47 +00:00
|
|
|
- name: Create bootstrapped state file
|
2023-10-31 19:08:20 +00:00
|
|
|
ansible.builtin.file:
|
2022-06-06 14:04:47 +00:00
|
|
|
dest: "{{ consul_bootstrap_state }}"
|
|
|
|
state: touch
|
2023-10-31 19:08:20 +00:00
|
|
|
mode: "0600"
|
2022-06-06 14:04:47 +00:00
|
|
|
when: not bootstrap_state.stat.exists
|
|
|
|
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Include dnsmasq
|
|
|
|
ansible.builtin.include_tasks: dnsmasq.yml
|
2022-04-03 18:39:52 +00:00
|
|
|
when: consul_dnsmasq_enable | bool
|
2023-10-21 13:17:06 +00:00
|
|
|
|
2023-10-31 19:08:20 +00:00
|
|
|
- name: Include systemd-resolved
|
2023-10-21 13:17:06 +00:00
|
|
|
ansible.builtin.include_tasks: systemd-resolved.yml
|
|
|
|
when: consul_systemd_resolved_enable
|