postgresql
postgresql copied to clipboard
[feature] use customized systemd-file (RedHat)
Hi sometimes I use a postgresdatabase and want to give it a easy name to control it, e.g. "holiday-db". Then I can "systemctl start|status|stop holiday-db". I dont care about the version of postgresql in the servicename.
To achive this I modified:
vars/RedHat.yml:
postgresql_default_service_name: "postgresql-{{ postgresql_version }}"
and than in
tasks/configure.yml:
# - make the var overrideable from playbook
- name: Define postgresql service name
set_fact:
postgresql_service_name: "{{ postgresql_default_service_name }}"
when: postgresql_service_name is not defined
# - no custom serviceunit, use conf directory
- name: PostgreSQL | Ensure the systemd directory for PostgreSQL exists | RedHat
file:
name: "/etc/systemd/system/postgresql-{{ postgresql_version }}.service.d"
state: directory
mode: 0755
when: ansible_os_family == "RedHat" and (postgresql_default_service_name == postgresql_service_name)
notify: restart postgresql
# - no custom serviceunit, use conf directory and default config as serviceunit
- name: PostgreSQL | Use the conf directory when starting the Postgres service | RedHat
template:
src: etc_systemd_system_postgresql.service.d_custom.conf.j2
dest: "/etc/systemd/system/postgresql-{{ postgresql_version }}.service.d/custom.conf"
when: ansible_os_family == "RedHat" and (postgresql_default_service_name == postgresql_service_name)
register: postgresql_systemd_custom_conf
# - custom serviceunit name
- name: "Systemd unit file for {{ postgresql_service_name }}"
copy:
dest: /etc/systemd/system/{{ postgresql_service_name }}.service
mode: 0644
content: |
# by ansible
# create different PGDATA with a different service:
.include /lib/systemd/system/postgresql-{{ postgresql_version }}.service
[Service]
User={{ postgresql_service_user }}
Group={{ postgresql_service_group }}
Environment=PGDATA={{ postgresql_conf_directory }}
ExecStartPre=
{% if postgresql_version is version_compare('10', '>=') %}
ExecStartPre={{ postgresql_bin_directory }}/postgresql-{{ postgresql_version_terse }}-check-db-dir {{ postgresql_data_directory }}
{% else %}
ExecStartPre={{ postgresql_bin_directory }}/postgresql{{ postgresql_version_terse }}-check-db-dir {{ postgresql_data_directory }}
{% endif %}
when: ansible_os_family == "RedHat" and (postgresql_default_service_name != postgresql_service_name)
register: postgresql_systemd_custom_conf
Than I can override the var postgresql_service_name and create my custom serviceunit for this.
In the end I have either two tasks creating custom_conf directory and file OR my own serviceunit. I could also use a template for my own serviceunit. cu Harvey
also please fix the daemon-reload and the enabling of the service:
- name: restart postgresql
service:
name: "{{ postgresql_service_name }}"
state: restarted
daemon_reload: yes
enabled: "{{ postgresql_service_enabled }}"
@harvey637 , is this similar to #289 ? That gives service names such as:
postgresql-11-clustername
This allows you name your cluster, and have unique services just in case you want multiple clusters on the one server. I did this without having to mess with the main service file, by including it:
# {{ ansible_managed }}
# Systemd unit file override to specify user/group as well as separate config
# and data directories.
.include /lib/systemd/system/postgresql-{{ postgresql_version }}.service
[Unit]
Description=PostgreSQL {{ postgresql_version }} database server - cluster_name:{{ postgresql_cluster_name }}
[Service]
User={{ postgresql_service_user }}
Group={{ postgresql_service_group }}
Environment=PGDATA={{ postgresql_conf_directory }}
{% if postgresql_version is version_compare('10', '>=') %}
ExecStartPre={{ postgresql_bin_directory }}/postgresql-{{ postgresql_version_terse }}-check-db-dir {{ postgresql_data_directory }}
{% else %}
ExecStartPre={{ postgresql_bin_directory }}/postgresql{{ postgresql_version_terse }}-check-db-dir {{ postgresql_data_directory }}
{% endif %}
Hi, similar, but not fully customizable. I dont want the part "postgresql-11". I use something like "sonar-db" with the "sonar-" part as indication of the application this database is for. So from a higher level I start/restart/status "sonar-db" for the sonar-database and "sonar" for the applikation. On the same system I might have a "bacula-db" and "bacula"-application. As you can see my systemd-unit very similar to yours as I also use the ".include" of the original postgresql-database systemd-unit, but with a "free variable" as service name.
So my proposal looks universal to me, as the servicename is any variable (might default to "postgresql-{{ pgversion }}" ) cu Harvey
also please fix the daemon-reload and the enabling of the service:
- name: restart postgresql service: name: "{{ postgresql_service_name }}" state: restarted daemon_reload: yes enabled: "{{ postgresql_service_enabled }}"
I think this possible only witth systemd module. I propose another solution: #443. Service restarts should be done with service module so we have only one restart task for all distros. And distros with systemd should have systemd reload before restart.
This issue has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution!
This issue has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details.