From 7e2cc273a066c6cbd490b4d721bf71632e5f8c9e Mon Sep 17 00:00:00 2001 From: lhahn Date: Thu, 15 Oct 2026 17:57:12 +0200 Subject: [PATCH] Initial minecraft role Paper/Java via itzg/minecraft-server in docker compose; whitelist and ops synchronized from Ansible, RCON on localhost only, explicit EULA flag. Co-Authored-By: Claude Opus 5.5 --- LICENSE | 9 ++++ README.md | 11 +++++ defaults/main.yml | 33 +++++++++++++ handlers/main.yml | 8 ++++ meta/main.yml | 17 +++++++ tasks/main.yml | 48 +++++++++++++++++++ templates/opt/minecraft/.env.j2 | 27 +++++++++++ templates/opt/minecraft/docker-compose.yml.j2 | 16 +++++++ vars/main.yml | 3 ++ 9 files changed, 172 insertions(+) create mode 100755 LICENSE create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/main.yml create mode 100644 templates/opt/minecraft/.env.j2 create mode 100644 templates/opt/minecraft/docker-compose.yml.j2 create mode 100644 vars/main.yml diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..2071b23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ff6b92b --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# cloud-minecraft + +Ansible role to setup a Minecraft Java Edition server (Paper by default) via docker compose, +based on the `itzg/minecraft-server` image. + +- the game port is public, RCON listens on `127.0.0.1` only +- whitelist and operators are managed from Ansible (`minecraft_whitelist`, `minecraft_ops`); + the whitelist is enforced and synchronized on every start +- `minecraft_eula` must be set to `true` explicitly (Minecraft EULA: https://aka.ms/MinecraftEULA) +- world data lives in `minecraft_data_location`; for a consistent backup run + `rcon-cli save-off` and `rcon-cli save-all flush` before copying, `rcon-cli save-on` afterwards diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..0ef1099 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,33 @@ +--- +cloud_apps: /opt +cloud_storage: /opt/storage +cloud_stage: prod +cloud_update: false +cloud_tzdata: Europe/Berlin + + +#Minecraft EULA has to be accepted explicitly: https://aka.ms/MinecraftEULA +minecraft_eula: false + +minecraft_image: itzg/minecraft-server +minecraft_image_tag: java25 +minecraft_type: PAPER +minecraft_version: "26.3" +minecraft_memory: 4G + +minecraft_port: 25565 +minecraft_rcon_port: 25575 +minecraft_rcon_password: MyRconPassword! + +minecraft_data_location: "{{ cloud_storage }}/minecraft-data" + +minecraft_motd: "A Minecraft Server" +minecraft_difficulty: normal +minecraft_mode: survival +minecraft_max_players: 10 +minecraft_view_distance: 10 +minecraft_pvp: true + +#player names (Java Edition) +minecraft_whitelist: [] +minecraft_ops: [] diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..aa86cde --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,8 @@ +--- +- name: restart minecraft + community.docker.docker_compose_v2: + project_src: "{{ minecraft_inst }}" + state: present + pull: missing + recreate: always + remove_orphans: yes diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..fc64cce --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,17 @@ +--- +galaxy_info: + role_name: minecraft + namespace: hahn-cloud + author: Lars Hahn + company: OpenDevChain + license: MIT + description: Role to setup a Minecraft Java Edition server + min_ansible_version: 2.7 + platforms: + - name: Debian + versions: + - 13 + galaxy_tags: + - minecraft +dependencies: + - docker diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..950a00b --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,48 @@ +--- +- name: check minecraft eula acceptance + assert: + that: minecraft_eula | bool + fail_msg: "Set minecraft_eula: true to accept the Minecraft EULA (https://aka.ms/MinecraftEULA)." + +- name: setup minecraft directories + file: + state: directory + path: "{{ item }}" + owner: root + group: root + mode: 0750 + loop: + - "{{ minecraft_dir }}" + - "{{ minecraft_inst }}" + +# the container runs as uid/gid 1000 +- name: setup minecraft data directory + file: + state: directory + path: "{{ minecraft_data_location }}" + owner: "1000" + group: "1000" + mode: 0750 + +- name: template minecraft compose file + template: + src: opt/minecraft/docker-compose.yml.j2 + dest: "{{ minecraft_inst }}/docker-compose.yml" + mode: 0644 + owner: root + group: root + notify: restart minecraft + +- name: template minecraft environment + template: + src: opt/minecraft/.env.j2 + dest: "{{ minecraft_inst }}/.env" + mode: 0640 + owner: root + group: root + notify: restart minecraft + +- name: start minecraft + community.docker.docker_compose_v2: + project_src: "{{ minecraft_inst }}" + state: present diff --git a/templates/opt/minecraft/.env.j2 b/templates/opt/minecraft/.env.j2 new file mode 100644 index 0000000..a8f0351 --- /dev/null +++ b/templates/opt/minecraft/.env.j2 @@ -0,0 +1,27 @@ +### BASIC ### +EULA={{ minecraft_eula | bool | upper }} +TYPE={{ minecraft_type }} +VERSION={{ minecraft_version }} +MEMORY={{ minecraft_memory }} +TZ={{ cloud_tzdata }} +ONLINE_MODE=TRUE + +### GAME ### +MOTD={{ minecraft_motd }} +DIFFICULTY={{ minecraft_difficulty }} +MODE={{ minecraft_mode }} +MAX_PLAYERS={{ minecraft_max_players }} +VIEW_DISTANCE={{ minecraft_view_distance }} +PVP={{ minecraft_pvp | bool | lower }} + +### ACCESS ### +ENABLE_WHITELIST=TRUE +ENFORCE_WHITELIST=TRUE +EXISTING_WHITELIST_FILE=SYNCHRONIZE +WHITELIST={{ minecraft_whitelist | join(',') }} +EXISTING_OPS_FILE=SYNCHRONIZE +OPS={{ minecraft_ops | join(',') }} + +### RCON ### +ENABLE_RCON=TRUE +RCON_PASSWORD='{{ minecraft_rcon_password }}' diff --git a/templates/opt/minecraft/docker-compose.yml.j2 b/templates/opt/minecraft/docker-compose.yml.j2 new file mode 100644 index 0000000..70dbdf2 --- /dev/null +++ b/templates/opt/minecraft/docker-compose.yml.j2 @@ -0,0 +1,16 @@ +name: minecraft +services: + server: + image: {{ minecraft_image }}:{{ minecraft_image_tag }} + restart: unless-stopped + env_file: + - .env + ports: + - "{{ minecraft_port }}:25565" + - "127.0.0.1:{{ minecraft_rcon_port }}:25575" + volumes: + - {{ minecraft_data_location }}:/data + # give the server time to save the world on stop + stop_grace_period: 60s + tty: true + stdin_open: true diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..594cc79 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,3 @@ +--- +minecraft_dir: "{{ cloud_apps }}/minecraft" +minecraft_inst: "{{ minecraft_dir }}/inst"