202 lines
6.3 KiB
YAML
202 lines
6.3 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: Template out scripts
|
|
template:
|
|
src: "{{ item.src }}"
|
|
dest: "/var/minecraft/{{ mcname }}/{{ item.dest }}"
|
|
mode: "{{ item.mode }}"
|
|
loop:
|
|
- { src: "install.sh", dest: "install.sh", mode: "0755" }
|
|
- { src: "recover.sh", dest: "recover.sh", mode: "0755" }
|
|
- name: Install modpack
|
|
block:
|
|
- name: Disable service
|
|
systemd:
|
|
name: "minecraft@{{ mcname }}"
|
|
state: stopped
|
|
become: yes
|
|
become_user: root
|
|
- name: Remove remnants of old pack
|
|
file:
|
|
path: "{{ item }}"
|
|
state: absent
|
|
loop:
|
|
- "/var/minecraft/{{ mcname }}/config"
|
|
- "/var/minecraft/{{ mcname }}/coremods"
|
|
- "/var/minecraft/{{ mcname }}/local"
|
|
- "/var/minecraft/{{ mcname }}/mods"
|
|
- "/var/minecraft/{{ mcname }}/scripts"
|
|
- "/var/minecraft/{{ mcname }}/server.jar"
|
|
- name: Unpack zip
|
|
unarchive:
|
|
src: "/var/minecraft/{{ mcname }}/modpack.zip"
|
|
remote_src: yes
|
|
dest: "/var/minecraft/{{ mcname }}"
|
|
notify: "restart minecraft {{ mcname }}"
|
|
- name: Run install script
|
|
command:
|
|
chdir: "/var/minecraft/{{ mcname }}"
|
|
argv:
|
|
- bash
|
|
- "./install.sh"
|
|
notify: "restart minecraft {{ mcname }}"
|
|
when: get_mcpack is changed
|
|
- name: Check for Forge
|
|
stat:
|
|
path: "/var/minecraft/{{ mcname }}/server.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: Find classic jar name
|
|
stat:
|
|
path: "/var/minecraft/{{ mcname }}/forge-{{ mcforge }}-universal.jar"
|
|
register: stat_mcroot_newjar
|
|
- name: Link old-style server jar
|
|
file:
|
|
path: "/var/minecraft/{{ mcname }}/server.jar"
|
|
src: "/var/minecraft/{{ mcname }}/forge-{{ mcforge }}-universal.jar"
|
|
state: link
|
|
when: stat_mcroot_newjar.stat.exists
|
|
- name: Link new-style server jar
|
|
file:
|
|
path: "/var/minecraft/{{ mcname }}/server.jar"
|
|
src: "/var/minecraft/{{ mcname }}/forge-{{ mcforge }}.jar"
|
|
state: link
|
|
when: not stat_mcroot_newjar.stat.exists
|
|
when: not stat_mcroot_forgejar.stat.exists
|
|
- name: Check for world
|
|
stat:
|
|
path: "/var/minecraft/{{ mcname }}/world"
|
|
register: stat_mcroot_world
|
|
- name: Recover world, if available
|
|
block:
|
|
- name: Stop server
|
|
systemd:
|
|
name: "minecraft@{{ mcname }}"
|
|
state: stopped
|
|
- name: Run recover script
|
|
command:
|
|
chdir: "/var/minecraft/{{ mcname }}"
|
|
argv:
|
|
- bash
|
|
- "./recover.sh"
|
|
notify: "restart minecraft {{ mcname }}"
|
|
become: yes
|
|
become_user: root
|
|
- name: Change permissions of world
|
|
file:
|
|
path: "/var/minecraft/{{ mcname }}/world"
|
|
state: directory
|
|
owner: minecraft
|
|
group: mc-admin
|
|
when: not stat_mcroot_world.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"
|
|
- name: Assure CNAME record
|
|
route53:
|
|
state: present
|
|
overwrite: yes
|
|
zone: "{{ zone }}"
|
|
type: CNAME
|
|
record: "{{ mcname }}.mc.{{ zone }}."
|
|
ttl: 300
|
|
value:
|
|
- "{{ inventory_hostname_short }}.{{ zone }}."
|
|
- name: Assure SRV record
|
|
route53:
|
|
state: present
|
|
overwrite: yes
|
|
zone: "{{ zone }}"
|
|
type: SRV
|
|
record: "_minecraft._tcp.{{ mcname }}.mc.{{ zone }}."
|
|
ttl: 300
|
|
value:
|
|
- "1 10 {{ mcport }} {{ inventory_hostname_short }}.{{ zone }}."
|
|
become: yes
|