From 78f4ce9ee53e940803f2fb657cf1eb0a9bb2a239 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sat, 5 Oct 2019 16:41:47 -0500 Subject: [PATCH] Add Minecraft server role --- roles/minecraft/defaults/main.yaml | 15 +++ roles/minecraft/handlers/main.yaml | 11 ++ roles/minecraft/tasks/main.yaml | 104 ++++++++++++++++++ .../minecraft/templates/minecraft.service.j2 | 17 +++ .../minecraft/templates/server.properties.j2 | 2 + 5 files changed, 149 insertions(+) create mode 100644 roles/minecraft/defaults/main.yaml create mode 100644 roles/minecraft/handlers/main.yaml create mode 100644 roles/minecraft/tasks/main.yaml create mode 100644 roles/minecraft/templates/minecraft.service.j2 create mode 100644 roles/minecraft/templates/server.properties.j2 diff --git a/roles/minecraft/defaults/main.yaml b/roles/minecraft/defaults/main.yaml new file mode 100644 index 0000000..55b72f6 --- /dev/null +++ b/roles/minecraft/defaults/main.yaml @@ -0,0 +1,15 @@ +--- +minecraft_java_package_name: openjdk-8-jre-headless +minecraft_java_package_state: present + +minecraft_service_name: minecraft.service +minecraft_service_state: started +minecraft_service_enabled: yes + +minecraft_user: minecraft +minecraft_group: minecraft + +minecraft_jar_url: https://launcher.mojang.com/v1/objects/3dc3d84a581f14691199cf6831b71ed1296a9fdf/server.jar + +minecraft_opt_path: /opt/minecraft +minecraft_var_path: "{{ minecraft_opt_path }}/var" diff --git a/roles/minecraft/handlers/main.yaml b/roles/minecraft/handlers/main.yaml new file mode 100644 index 0000000..38c2de9 --- /dev/null +++ b/roles/minecraft/handlers/main.yaml @@ -0,0 +1,11 @@ +--- +- name: minecraft daemon-reload + systemd: + name: "{{ minecraft_service_name }}" + daemon_reload: yes + state: restarted + +- name: restart minecraft + service: + name: "{{ minecraft_service_name }}" + state: restarted diff --git a/roles/minecraft/tasks/main.yaml b/roles/minecraft/tasks/main.yaml new file mode 100644 index 0000000..5d8a22a --- /dev/null +++ b/roles/minecraft/tasks/main.yaml @@ -0,0 +1,104 @@ +--- +- name: create minecraft group + group: + name: "{{ minecraft_group }}" + gid: "{{ minecraft_group_gid | default(omit) }}" + state: "{{ minecraft_group_state | default('present') }}" + system: yes + +- name: create minecraft user + user: + name: "{{ minecraft_user }}" + uid: "{{ minecraft_user_uid | default(omit) }}" + group: "{{ minecraft_group }}" + home: "{{ minecraft_var_path }}" + create_home: no + shell: "{{ minecraft_shell | default('/usr/sbin/nologin') }}" + state: "{{ minecraft_user_state | default('present') }}" + system: yes + +- name: install java + package: + name: "{{ minecraft_java_package_name }}" + state: "{{ minecraft_java_package_state }}" + +- name: create minecraft installation directory + file: + path: "{{ item }}" + state: directory + owner: root + group: root + mode: "0755" + with_items: + - "{{ minecraft_opt_path }}" + - "{{ minecraft_opt_path }}/bin" + - "{{ minecraft_opt_path }}/etc" + +- name: create minecraft var directory + file: + path: "{{ item }}" + state: directory + owner: "{{ minecraft_user }}" + group: "{{ minecraft_group }}" + mode: "0755" + with_items: + - "{{ minecraft_var_path }}" + +- name: download minecraft server + get_url: + url: "{{ minecraft_jar_url }}" + dest: "{{ minecraft_opt_path }}/bin/server.jar" + owner: "{{ minecraft_user }}" + group: "{{ minecraft_group }}" + mode: "0644" + +- name: agree to the eula + copy: + content: "eula=true" + dest: "{{ minecraft_var_path }}/eula.txt" + owner: "{{ minecraft_user }}" + group: "{{ minecraft_group }}" + mode: "0644" + +- name: configure minecraft + template: + src: server.properties.j2 + dest: "{{ minecraft_var_path }}/server.properties" + owner: root + group: root + mode: 0644 + notify: restart minecraft + +- name: configure systemd unit + template: + src: minecraft.service.j2 + dest: /etc/systemd/system/minecraft.service + owner: root + group: root + mode: 0644 + #validate: systemd-analyze verify %s + notify: minecraft daemon-reload + +- name: manage minecraft service + service: + name: "{{ minecraft_service_name }}" + state: "{{ minecraft_service_state }}" + enabled: "{{ minecraft_service_enabled }}" + +- name: configure ops + copy: + content: "{{ (minecraft_ops | default([])) | to_nice_json }}" + dest: "{{ minecraft_var_path }}/ops.json" + owner: "{{ minecraft_user }}" + group: "{{ minecraft_group }}" + mode: "0644" + notify: restart minecraft + +- name: configure whitelist + copy: + content: "{{ (minecraft_whitelist | default([])) | to_nice_json }}" + dest: "{{ minecraft_var_path }}/whitelist.json" + owner: "{{ minecraft_user }}" + group: "{{ minecraft_group }}" + mode: "0644" + notify: restart minecraft diff --git a/roles/minecraft/templates/minecraft.service.j2 b/roles/minecraft/templates/minecraft.service.j2 new file mode 100644 index 0000000..94b959e --- /dev/null +++ b/roles/minecraft/templates/minecraft.service.j2 @@ -0,0 +1,17 @@ +# {{ ansible_managed }} + +[Unit] +Description=Minecraft server +After=network.target + +[Service] +ExecStart=/usr/bin/java -Xmx{{ minecraft_java_xmx | default('1024M') }} -Xms{{ minecraft_java_xms | default('1024M') }} -jar {{ minecraft_opt_path }}/bin/server.jar --nogui +Type=simple +User={{ minecraft_user }} +Group={{ minecraft_group }} +WorkingDirectory={{ minecraft_var_path }} +Restart=on-failure +SyslogIdentifier=minecraft + +[Install] +WantedBy=multi-user.target diff --git a/roles/minecraft/templates/server.properties.j2 b/roles/minecraft/templates/server.properties.j2 new file mode 100644 index 0000000..c8b9f4f --- /dev/null +++ b/roles/minecraft/templates/server.properties.j2 @@ -0,0 +1,2 @@ +white-list: {{ (minecraft_config.whitelist | default(true)) | ternary('true', 'false') }} +enforce-whitelist: {{ (minecaft_config.enfoce_whitelist | default(true)) | ternary('true', 'false') }}