GadElKareem

Install Solr as systemd with restart on failure

Using https://github.com/geerlingguy/ansible-role-solr will install solr with a init.d file which will work well as a service but will not restart on crash. So this ansible script should install solr as a service using systemd:

tasks/service.yml

- set_facts:
    solr_install_path: /opt/solr

- name: Stop Solr
  service: name=solr state=stopped enabled=no

- name: Remove solr init.d file
  file: path=/etc/init.d/solr
        state=absent

- name: Copy solr service
  template:
    src: "service.j2"
    dest: "/lib/systemd/system/solr.service"
    owner: solr
    mode: 0755

- name: Enable solr service
  systemd:
    name: "solr"
    enabled: yes
    masked: no
    daemon_reload: yes
    state: restarted

- name: make sure daemon is reloaded (ansible bug)
  shell: systemctl daemon-reload

- name: Restart Solr
  service: name=solr state=restarted enabled=yes

templates/service.j2

[Unit]
Description=Apache SOLR
ConditionPathExists={{solr_install_path}}
After=syslog.target network.target remote-fs.target nss-lookup.target systemd-journald-dev-log.socket
Before=multi-user.target
Conflicts=shutdown.target

[Service]
User=solr
LimitNOFILE=1048576
LimitNPROC=1048576
PIDFile=/var/solr/solr-8983.pid
Environment=SOLR_INCLUDE=/etc/default/solr.in.sh
Environment=RUNAS=solr
Environment=SOLR_INSTALL_DIR={{solr_install_path}}

Restart=on-failure
RestartSec=5
startLimitIntervalSec=60

ExecStart={{solr_install_path}}/bin/solr start
ExecStop={{solr_install_path}}/bin/solr stop
Restart=on-failure

[Install]
WantedBy=multi-user.target