From 1fdff1c48be5547af436117bfde516fe7f0a0a88 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 31 Oct 2023 20:11:28 +0100 Subject: [PATCH] feat: add consul service --- handlers/main.yml | 8 ++++++-- tasks/consul.yml | 7 +++++++ tasks/main.yml | 4 ++++ templates/consul.d/postgres.hcl.j2 | 16 ++++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tasks/consul.yml create mode 100644 templates/consul.d/postgres.hcl.j2 diff --git a/handlers/main.yml b/handlers/main.yml index cce42b7..271f022 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,6 +1,10 @@ --- -- name: restart postgresql - service: +- name: Restart postgresql + ansible.builtin.service: name: "{{ postgresql_daemon }}" state: "{{ postgresql_restarted_state }}" sleep: 5 + +- name: Reload consul configuration on Linux + ansible.builtin.command: "consul reload" + listen: "reload consul configuration" diff --git a/tasks/consul.yml b/tasks/consul.yml new file mode 100644 index 0000000..e205cad --- /dev/null +++ b/tasks/consul.yml @@ -0,0 +1,7 @@ +- name: Template consul service + ansible.builtin.template: + src: consul.d/postgres.hcl.j2 + dest: /etc/consul.d/postgres.hcl + owner: consul + mode: "0644" + notify: Reload consul configuration on Linux diff --git a/tasks/main.yml b/tasks/main.yml index 922ab92..3c5fa3e 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -24,3 +24,7 @@ # Configure PostgreSQL. - import_tasks: users.yml - import_tasks: databases.yml + +- name: Import Consul service task + ansible.builtin.import_tasks: consul.yml + when: postgres_consul_service diff --git a/templates/consul.d/postgres.hcl.j2 b/templates/consul.d/postgres.hcl.j2 new file mode 100644 index 0000000..a415d94 --- /dev/null +++ b/templates/consul.d/postgres.hcl.j2 @@ -0,0 +1,16 @@ +service { + name = "{{ postgres_consul_service_name }}" + id = "{{ postgres_consul_service_name }}" + port = 5432 + tags = ["{{postgres_consul_tag}}"] + check + { + id = "check-{{ postgres_consul_service_name }}", + name = "{{ postgres_consul_service_name }} status check", + service_id = "{{ postgres_consul_service_name }}", + tcp = "localhost:5432", + interval = "5s", + timeout = "5s" + } +} +