Skip to content

Commit

Permalink
Consul installation from repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Zinchuk authored and bbaassssiiee committed Jun 26, 2021
1 parent 4f73cb8 commit e8ad89b
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 4 deletions.
7 changes: 4 additions & 3 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ consul_checksum_file_url: "https://releases.hashicorp.com/consul/{{ consul_versi
### Install Method
consul_install_remotely: false
consul_install_upgrade: false
consul_install_from_repo: false

### Paths
consul_bin_path: "/usr/local/bin"
consul_config_path: "/etc/consul"
consul_configd_path: "{{ consul_config_path }}/consul.d"
consul_configd_path: "/etc/consul.d"
consul_bootstrap_state: "{{ consul_config_path }}/.consul_bootstrapped"
consul_data_path: "/var/consul"
consul_data_path: "/opt/consul"
consul_log_path: "{{ lookup('env','CONSUL_LOG_PATH') | default('/var/log/consul', true) }}"
consul_log_file: "{{ lookup('env','CONSUL_LOG_FILE') | default('consul.log', true) }}"
consul_run_path: "/run/consul"
Expand All @@ -49,7 +50,7 @@ consul_binary: "{{ consul_bin_path }}/consul"
consul_manage_user: true
consul_user: "consul"
consul_manage_group: true
consul_group: "bin"
consul_group: "consul"
consul_systemd_restart_sec: 42
consul_systemd_limit_nofile: 65536
consul_systemd_unit_path: "/lib/systemd/system"
Expand Down
4 changes: 4 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@

- name: start snapshot
import_tasks: start_snapshot.yml

- name: systemctl daemon-reload
ansible.builtin.systemd:
daemon_reload: yes
5 changes: 4 additions & 1 deletion tasks/dirs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
owner: "{{ consul_user }}"
group: "{{ consul_group }}"
mode: 0750
when: not consul_install_from_repo | bool

when: ansible_os_family != 'Windows'

Expand Down Expand Up @@ -56,7 +57,9 @@
state: directory
owner: root
mode: 0755
when: ansible_os_family != 'Windows'
when:
- ansible_os_family != 'Windows'
- not consul_install_from_repo | bool

- name: Create directories on Windows
win_file:
Expand Down
105 changes: 105 additions & 0 deletions tasks/install_linux_repo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
# File: install_linux_repo.yml - package installation tasks for Consul

- name: Install OS packages
package:
name: "{{ item }}"
state: present
with_items: "{{ consul_os_packages }}"
tags: installation

- name: Populate service facts
service_facts:

- name: Gather the package facts
package_facts:
manager: auto

- name: Clean up previous consul data
block:
- name: Stop service consul, if running
systemd:
name: consul
state: stopped
when: ansible_facts.services['consul.service'] is defined

- name: Remove consul systemd unit file from previous installation
file:
path: /usr/lib/systemd/system/consul.service
state: absent
notify: systemctl daemon-reload

- name: Remove the user 'consul'
user:
name: consul
state: absent
remove: yes

when:
- "ansible_distribution|lower == 'redhat' or ansible_distribution|lower == 'centos' or \
ansible_distribution|lower == 'fedora' or ansible_distribution|lower == 'amazon' or \
ansible_distribution|lower == 'debian' or ansible_distribution|lower == 'ubuntu'"
- "'consul' not in ansible_facts.packages"

- name: Install repository
block:
- name: Add Redhat/CentOS/Fedora/Amazon Linux repository
command: "yum-config-manager --add-repo {{ consul_repo_url }}"
args:
creates: /etc/yum.repos.d/hashicorp.repo
when: "ansible_distribution|lower == 'redhat' or ansible_distribution|lower == 'centos' or \
ansible_distribution|lower == 'fedora' or ansible_distribution|lower == 'amazon'"


- name: Add an Apt signing key, uses whichever key is at the URL
apt_key:
url: https://apt.releases.hashicorp.com/gpg
state: present
when: ansible_distribution|lower == 'debian' or ansible_distribution|lower == 'ubuntu'

- name: Add Debian/Ubuntu Linux repository
apt_repository:
repo: "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
state: present
update_cache: true
when: ansible_distribution|lower == 'debian' or ansible_distribution|lower == 'ubuntu'

- name: Install consul package
package:
name: "consul-{{ consul_version }}"
state: present

- name: Create a directory /etc/systemd/system/consul.service.d
file:
path: /etc/systemd/system/consul.service.d
state: directory
mode: '0755'
owner: root
group: root
register: systemd_override


- name: Override systemd service params
template:
src: consul_systemd_service.override.j2
dest: /etc/systemd/system/consul.service.d/override.conf
owner: root
group: root
mode: 0644
register: systemd_override
notify:
- systemctl daemon-reload
- restart consul
when:
- ansible_service_mgr == "systemd"
- not ansible_os_family == "FreeBSD"
- not ansible_os_family == "Solaris"
- consul_install_from_repo | bool

- name: Flush handlers
meta: flush_handlers

- name: As, this role work with json conf file only - delete file /etc/consul.d/consul.hcl
file:
path: /etc/consul.d/consul.hcl
state: absent
8 changes: 8 additions & 0 deletions tasks/nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
- name: Include user and group settings
import_tasks: user_group.yml

- name: Install OS packages and consul - from the repository
include_tasks: install_linux_repo.yml
when:
- consul_install_from_repo | bool

- name: Include directory settings
import_tasks: dirs.yml

Expand All @@ -46,12 +51,14 @@
when:
- consul_install_binary | bool
- not consul_install_remotely | bool
- not consul_install_from_repo | bool

- name: Install OS packages and consul - remotely
include_tasks: install_remote.yml
when:
- consul_install_binary | bool
- consul_install_remotely | bool
- not consul_install_from_repo | bool

# XXX: Individual gossip tasks are deprecated and need to be removed
# - include_tasks: ../tasks/encrypt_gossip.yml
Expand Down Expand Up @@ -198,6 +205,7 @@
- ansible_service_mgr == "systemd"
- not ansible_os_family == "FreeBSD"
- not ansible_os_family == "Solaris"
- not consul_install_from_repo | bool

- name: Reload systemd
systemd:
Expand Down
2 changes: 2 additions & 0 deletions tasks/user_group.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
state: present
when:
- consul_manage_group | bool
- not consul_install_from_repo | bool

# Add user
- name: Add Consul user
Expand All @@ -18,3 +19,4 @@
system: true
when:
- consul_manage_user | bool
- not consul_install_from_repo | bool
10 changes: 10 additions & 0 deletions templates/consul_systemd_service.override.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# WARNING!!! Ansible managed.

[Unit]
ConditionFileNotEmpty=
ConditionFileNotEmpty={{ consul_config_path }}/config.json

[Service]
ExecStart=
ExecStart=/usr/bin/consul agent -config-file={{ consul_config_path }}/config.json -config-dir={{ consul_configd_path }}

5 changes: 5 additions & 0 deletions vars/Amazon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ consul_os_packages:
- git
- unzip
consul_syslog_enable: false

consul_os_prepare_packages:
- yum-utils

consul_repo_url: https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
2 changes: 2 additions & 0 deletions vars/Debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

consul_os_packages:
- unzip

consul_os_prepare_packages: []
9 changes: 9 additions & 0 deletions vars/RedHat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ consul_os_packages:
python3-libselinux\
{% endif %}"
- unzip

consul_os_prepare_packages:
- yum-utils

consul_repo_url: "{% if ( ansible_distribution == 'Fedora') %}\
https://rpm.releases.hashicorp.com/fedora/hashicorp.repo\
{% else %}\
https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo\
{% endif %}"

0 comments on commit e8ad89b

Please sign in to comment.