Manage Minecraft servers with systemd
This commit is contained in:
parent
32455e8016
commit
3dc0e04ab4
@ -3,4 +3,7 @@ allow_reboot: false
|
||||
manage_network: false
|
||||
|
||||
# minecraft
|
||||
minecraft_eula: true
|
||||
# Agree to EULA to start service in Vagrant
|
||||
# i.e. sed -i 's/^#minecraft_eula/minecraft_eula/' ./dev/host_vars/minecraft.yml
|
||||
#minecraft_eula: true
|
||||
minecraft_sha1: 1b557e7b033b583cd9f66746b7a9ab1ec1673ced # 1.16.5
|
||||
|
1
roles/.gitignore
vendored
1
roles/.gitignore
vendored
@ -7,6 +7,7 @@
|
||||
!gitea*/
|
||||
!jenkins*/
|
||||
!libvirt*/
|
||||
!minecraft*/
|
||||
!nextcloud*/
|
||||
!nginx*/
|
||||
!prometheus*/
|
||||
|
6
roles/minecraft/defaults/main.yml
Normal file
6
roles/minecraft/defaults/main.yml
Normal file
@ -0,0 +1,6 @@
|
||||
minecraft_eula: false
|
||||
minecraft_home: "/opt/minecraft"
|
||||
minecraft_url: "https://launcher.mojang.com/v1/objects/{{ minecraft_sha1 }}/server.jar"
|
||||
minecraft_user: minecraft
|
||||
minecraft_xms: 1G
|
||||
minecraft_xmx: "{{ minecraft_xms }}"
|
55
roles/minecraft/tasks/main.yml
Normal file
55
roles/minecraft/tasks/main.yml
Normal file
@ -0,0 +1,55 @@
|
||||
- name: Install Screen
|
||||
apt:
|
||||
name: screen
|
||||
state: present
|
||||
|
||||
- name: Install Java
|
||||
apt:
|
||||
name: default-jre
|
||||
state: present
|
||||
|
||||
- name: Create Minecraft user
|
||||
user:
|
||||
name: "{{ minecraft_user }}"
|
||||
state: present
|
||||
shell: /bin/bash
|
||||
|
||||
- name: Create Minecraft directory
|
||||
file:
|
||||
path: "{{ minecraft_home }}"
|
||||
state: directory
|
||||
owner: "{{ minecraft_user }}"
|
||||
group: "{{ minecraft_user }}"
|
||||
|
||||
- name: Download Minecraft
|
||||
get_url:
|
||||
url: "{{ minecraft_url }}"
|
||||
dest: "{{ minecraft_home }}/minecraft_server.jar"
|
||||
checksum: "sha1:{{ minecraft_sha1 }}"
|
||||
owner: "{{ minecraft_user }}"
|
||||
group: "{{ minecraft_user }}"
|
||||
mode: 0700
|
||||
|
||||
- name: Deploy Minecraft systemd service
|
||||
template:
|
||||
src: minecraft.service.j2
|
||||
dest: "/etc/systemd/system/minecraft.service"
|
||||
register: minecraft_systemd
|
||||
|
||||
- name: Answer to Mojang's EULA
|
||||
template:
|
||||
src: eula.txt.j2
|
||||
dest: "{{ minecraft_home }}/eula.txt"
|
||||
owner: "{{ minecraft_user }}"
|
||||
group: "{{ minecraft_user }}"
|
||||
|
||||
- name: Reload systemd manager configuration
|
||||
systemd:
|
||||
daemon_reload: true
|
||||
when: minecraft_systemd.changed
|
||||
|
||||
- name: Start Service
|
||||
service:
|
||||
name: minecraft
|
||||
state: started
|
||||
when: minecraft_eula
|
1
roles/minecraft/templates/eula.txt.j2
Normal file
1
roles/minecraft/templates/eula.txt.j2
Normal file
@ -0,0 +1 @@
|
||||
eula={{ minecraft_eula | lower }}
|
27
roles/minecraft/templates/minecraft.service.j2
Normal file
27
roles/minecraft/templates/minecraft.service.j2
Normal file
@ -0,0 +1,27 @@
|
||||
[Unit]
|
||||
Description=Minecraft Server
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
WorkingDirectory={{ minecraft_home }}
|
||||
User={{ minecraft_user }}
|
||||
Group={{ minecraft_user }}
|
||||
Restart=always
|
||||
|
||||
ExecStart=/usr/bin/screen -DmS minecraft \
|
||||
/usr/bin/java -Xms{{ minecraft_xms }} -Xmx{{ minecraft_xmx }} -jar minecraft_server.jar nogui
|
||||
ExecReload=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "reload"\\015'
|
||||
ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "say SERVER SHUTDOWN IN 30 SECONDS"\015'
|
||||
ExecStop=/bin/sleep 15
|
||||
ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "say SERVER SHUTDOWN IN 15 SECONDS"\015'
|
||||
ExecStop=/bin/sleep 10
|
||||
ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "say SERVER SHUTDOWN IN 5 SECONDS"\015'
|
||||
ExecStop=/bin/sleep 5
|
||||
ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "save-all"\015'
|
||||
ExecStop=/bin/sleep 1
|
||||
ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "stop"\015'
|
||||
ExecStop=/bin/sleep 3
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
Loading…
Reference in New Issue
Block a user