From 8dd7cba5e6e863a16f67976c4679ce3bf2935eab Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Mon, 16 Dec 2019 14:10:56 +0100 Subject: [PATCH] springboot: install systemd units on systemd based OSs. --- .../roles/springboot-config/handlers/main.yml | 4 +++ .../roles/springboot-config/tasks/main.yml | 29 ++++++++++++------ .../templates/springboot.service.systemd.j2 | 30 +++++++++++++++++++ 3 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 library/roles/springboot-config/handlers/main.yml create mode 100644 library/roles/springboot-config/templates/springboot.service.systemd.j2 diff --git a/library/roles/springboot-config/handlers/main.yml b/library/roles/springboot-config/handlers/main.yml new file mode 100644 index 00000000..0b643045 --- /dev/null +++ b/library/roles/springboot-config/handlers/main.yml @@ -0,0 +1,4 @@ +--- +- name: systemd reload + command: systemctl daemon-reload + diff --git a/library/roles/springboot-config/tasks/main.yml b/library/roles/springboot-config/tasks/main.yml index d5194b5c..a99715ef 100644 --- a/library/roles/springboot-config/tasks/main.yml +++ b/library/roles/springboot-config/tasks/main.yml @@ -1,13 +1,24 @@ --- -- block: - - name: Install the springboot app upstart init file - template: src=springboot-upstart.conf.j2 dest=/etc/init/{{ item.name }}.conf owner=root group=root mode=0644 - with_items: '{{ springboot_apps }}' - when: is_trusty +- name: Springboot startup files management + block: + - name: Install the springboot app upstart init file + template: src=springboot-upstart.conf.j2 dest=/etc/init/{{ item.name }}.conf owner=root group=root mode=0644 + with_items: '{{ springboot_apps }}' + when: ansible_service_mgr != 'systemd' - - name: Install the springboot logrotate configuration - template: src=springboot-logrotate.j2 dest=/etc/logrotate.d/{{ item.name }} owner=root group=root mode=0444 - with_items: '{{ springboot_apps }}' + - name: Install the springboot systemd service unit + template: src=springboot.service.systemd.j2 dest=/etc/systemd/system/{{ item.name }}.service mode=0644 owner=root group=root + with_items: '{{ springboot_apps }}' + when: ansible_service_mgr == 'systemd' + notify: systemd reload - when: springboot_config + - name: Install the springboot logrotate configuration + template: src=springboot-logrotate.j2 dest=/etc/logrotate.d/{{ item.name }} owner=root group=root mode=0444 + with_items: '{{ springboot_apps }}' + + - name: Ensure that the springboot service is running and enabled + service: name={{ item.name }} state=started enabled=yes + with_items: '{{ springboot_apps }}' + + when: springboot_config | bool tags: [ 'springboot', 'springboot_config' ] diff --git a/library/roles/springboot-config/templates/springboot.service.systemd.j2 b/library/roles/springboot-config/templates/springboot.service.systemd.j2 new file mode 100644 index 00000000..9c7d8efc --- /dev/null +++ b/library/roles/springboot-config/templates/springboot.service.systemd.j2 @@ -0,0 +1,30 @@ +[Unit] +Description={{ item.name }} +After=network.target + +[Service] +{% if item.envfile is defined %} +EnvironmentFile=/etc/myapp/environmentfile +{% endif %} +Type=simple +User={{ item.user }} +Group={{ item.user }} + +WorkingDirectory={{ item.install_dir }} +{% if item.envfile is defined %} +ExecStart=/usr/bin/java $JAVA_OPTS -jar $EXEC_JAR +{% else %} +ExecStart=/usr/bin/java {{ item.java_opts }} -jar {{ item.install_dir }}/{{ item.name }}.jar +{% endif %} + +StandardOutput=journal +StandardError=journal +SyslogIdentifier={{ item.name }} + +SuccessExitStatus=143 +TimeoutStopSec=10 +Restart=on-failure +RestartSec=60 + +[Install] +WantedBy=multi-user.target