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" + } +} +