diff --git a/dev/host_vars/minecraft.yml b/dev/host_vars/minecraft.yml index 1441cca..f057b68 100644 --- a/dev/host_vars/minecraft.yml +++ b/dev/host_vars/minecraft.yml @@ -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 diff --git a/roles/.gitignore b/roles/.gitignore index 4357616..9ecd153 100644 --- a/roles/.gitignore +++ b/roles/.gitignore @@ -7,6 +7,7 @@ !gitea*/ !jenkins*/ !libvirt*/ +!minecraft*/ !nextcloud*/ !nginx*/ !prometheus*/ diff --git a/roles/minecraft/defaults/main.yml b/roles/minecraft/defaults/main.yml new file mode 100644 index 0000000..2d368f1 --- /dev/null +++ b/roles/minecraft/defaults/main.yml @@ -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 }}" diff --git a/roles/minecraft/tasks/main.yml b/roles/minecraft/tasks/main.yml new file mode 100644 index 0000000..010eadd --- /dev/null +++ b/roles/minecraft/tasks/main.yml @@ -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 diff --git a/roles/minecraft/templates/eula.txt.j2 b/roles/minecraft/templates/eula.txt.j2 new file mode 100644 index 0000000..3757364 --- /dev/null +++ b/roles/minecraft/templates/eula.txt.j2 @@ -0,0 +1 @@ +eula={{ minecraft_eula | lower }} diff --git a/roles/minecraft/templates/minecraft.service.j2 b/roles/minecraft/templates/minecraft.service.j2 new file mode 100644 index 0000000..2879b9a --- /dev/null +++ b/roles/minecraft/templates/minecraft.service.j2 @@ -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