ansible/roles/minecraft/tasks/main.yml

144 lines
4.5 KiB
YAML

#!/usr/bin/ansible-playbook
# vim:ft=ansible:
---
- name: Set up Minecraft user
block:
- name: Install required packages
apt:
name: "{{ packages }}"
vars:
packages:
- openjdk-8-jre
- screen
- unzip
- name: Assure minecraft admin group
group:
name: mc-admin
- name: Assure server user
user:
name: minecraft
group: mc-admin
system: yes
home: /var/minecraft
- name: Assure var directory
file:
path: /var/minecraft
state: directory
owner: root
group: mc-admin
# Sticky, SetGID
mode: "3770"
- name: Assure systemd unit
template:
src: "minecraft@.service"
dest: "/etc/systemd/system/minecraft@.service"
- name: Reload available daemons
systemd:
daemon_reload: yes
become: yes
- name: Set up Minecraft server
block:
- name: Create server directory
file:
path: "/var/minecraft/{{ mcname }}"
state: directory
- name: Download zip
get_url:
dest: "/var/minecraft/{{ mcname }}/modpack.zip"
url: "{{ mczip }}"
register: get_mcpack
- name: Install modpack
block:
- name: Remove remnants of old pack
file:
path: "{{ item }}"
state: absent
loop:
- "/var/minecraft/{{ mcname }}/config"
- "/var/minecraft/{{ mcname }}/coremods"
- "/var/minecraft/{{ mcname }}/mods"
- "/var/minecraft/{{ mcname }}/scripts"
- name: Unpack zip
unarchive:
src: "/var/minecraft/{{ mcname }}/modpack.zip"
remote_src: yes
dest: "/var/minecraft/{{ mcname }}"
notify: "restart minecraft {{ mcname }}"
- name: Check for MultiMC pack
stat:
path: "/var/minecraft/{{ mcname }}/.minecraft"
register: stat_mcroot_multimc
- name: Move .minecraft contents to root
command:
chdir: "/var/minecraft/{{ mcname }}"
argv:
- mv
- "/var/minecraft/{{ mcname }}/.minecraft/*"
- "."
notify: "restart minecraft {{ mcname }}"
when: stat_mcroot_multimc.stat.exists
- name: Remove MultiMC artifacts
file:
path: "{{ item }}"
state: absent
loop:
- "/var/minecraft/{{ mcname }}/.minecraft"
- "/var/minecraft/{{ mcname }}/instance.cfg"
- "/var/minecraft/{{ mcname }}/mmc-pack.json"
when: stat_mcroot_multimc.stat.exists
when: get_mcpack is changed
- name: Check for Forge
stat:
path: "/var/minecraft/{{ mcname }}/forge-{{ mcforge }}-universal.jar"
register: stat_mcroot_forgejar
- name: Install Forge
block:
- name: Download Forge
get_url:
dest: "/var/minecraft/{{ mcname }}/installer.jar"
# Accurate as of 2020-02-23
url: "https://files.minecraftforge.net/maven/net/minecraftforge/forge/{{ mcforge }}/forge-{{ mcforge }}-installer.jar"
register: get_forge
- name: Install Forge
# Note: This also installs a Minecraft server, thankfully
command:
chdir: "/var/minecraft/{{ mcname }}"
argv:
- /usr/bin/java
- "-jar"
- "/var/minecraft/{{ mcname }}/installer.jar"
- "--installServer"
args:
creates: "/var/minecraft/{{ mcname }}/forge-{{ mcforge }}-universal.jar"
- name: Link server jar
file:
path: "/var/minecraft/{{ mcname }}/server.jar"
src: "/var/minecraft/{{ mcname }}/forge-{{ mcforge }}-universal.jar"
state: link
when: not stat_mcroot_forgejar.stat.exists
- name: Configure server
block:
- name: Template out server.properties
template:
src: server.properties
dest: "/var/minecraft/{{ mcname }}/server.properties"
- name: Accept EULA
copy:
src: "eula.txt"
dest: "/var/minecraft/{{ mcname }}/eula.txt"
become: yes
become_user: minecraft
- name: Set up system
block:
- name: Enable service
systemd:
name: "minecraft@{{ mcname }}"
enabled: yes
state: started
- name: Template out backup module
template:
src: "backup.sh"
dest: "/opt/backups/modules/minecraft-{{ mcname }}.sh"
mode: "0600"
become: yes