ansible/roles/minecraft/tasks/main.yml

202 lines
6.3 KiB
YAML
Raw Normal View History

2020-02-23 23:45:11 -06:00
#!/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:
2020-02-23 23:45:11 -06:00
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
2020-02-23 23:45:11 -06:00
block:
- name: Disable service
systemd:
name: "minecraft@{{ mcname }}"
state: stopped
2020-06-24 10:21:09 -05:00
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"
2020-02-23 23:45:11 -06:00
- name: Unpack zip
unarchive:
src: "/var/minecraft/{{ mcname }}/modpack.zip"
remote_src: yes
dest: "/var/minecraft/{{ mcname }}"
notify: "restart minecraft {{ mcname }}"
2020-06-24 10:38:24 -05:00
- name: Run install script
command:
chdir: "/var/minecraft/{{ mcname }}"
argv:
2020-06-24 10:38:24 -05:00
- 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:
2020-02-23 23:45:11 -06:00
- 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
2020-02-23 23:45:11 -06:00
- 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
2020-02-23 23:45:11 -06:00
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:
2020-06-25 08:57:58 -05:00
- 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"
2020-02-23 23:45:11 -06:00
become: yes
become_user: minecraft
2020-06-08 20:10:58 -05:00
- 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
2020-07-23 21:02:56 -05:00
overwrite: yes
zone: "{{ zone }}"
type: CNAME
record: "{{ mcname }}.mc.{{ zone }}."
ttl: 300
value:
- "{{ inventory_hostname }}.{{ zone }}."
- name: Assure SRV record
route53:
state: present
2020-07-23 21:02:56 -05:00
overwrite: yes
zone: "{{ zone }}"
type: SRV
record: "_minecraft._tcp.{{ mcname }}.mc.{{ zone }}."
ttl: 300
value:
- "1 10 {{ mcport }} {{ inventory_hostname }}.{{ zone }}."
2020-02-24 00:18:41 -06:00
become: yes