Skip to content

Instantly share code, notes, and snippets.

@JonTheNiceGuy
Created December 15, 2022 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonTheNiceGuy/96521dece55dd1dbe8e3ea83f9b71e03 to your computer and use it in GitHub Desktop.
Save JonTheNiceGuy/96521dece55dd1dbe8e3ea83f9b71e03 to your computer and use it in GitHub Desktop.
This is an Ansible Playbook to monitor a git repo for players to add (or remove) and then restart the relevant docker containers using `docker-compose`. I use this to manage the players on a minecraft server I run.

Docker Minecraft Server

This is an Ansible Playbook to monitor a git repo for players to add (or remove) and then restart the relevant docker containers using docker-compose. I use this to manage the players on a minecraft server I run.

In this repo, you'll find the actual ansible playbook (playbook.yml), the crontab-able run_playbook.sh (which silences unchanged plays) and the template I'm using (templates=docker-compose.yml). There is also an example vars file (host_vars=localhost=vars.yml) to show the basics of what this file would look like. Note that directory structures in gists are not permitted, so I've replaced / with = in each filename.

default_players:
- notch
git_repos:
players: https://example.com/user/players
servers:
- players: "{{ default_players + (example_players | default([])) }}"
gamemode: creative
difficulty: peaceful
cheats: "true"
name: Example
---
- name: Run Playbook
hosts: localhost
tasks:
- name: Run git pull
register: git_pull
ansible.builtin.git:
repo: "{{ item.value }}"
dest: "host_vars/localhost/{{ item.key }}/"
loop: "{{ git_repos | dict2items }}"
- name: Reload Vars
when: git_pull.changed
ansible.builtin.include_vars:
dir: "host_vars/{{ inventory_hostname }}"
ignore_unknown_extensions: yes
- name: Template docker
when: git_pull.changed or force | default(false)
register: template
template:
src: templates/docker-compose.yml
dest: /opt/docker/minecraft/docker-compose.yml
- name: Restart docker containers
when: template.changed or force | default(false)
register: docker
community.docker.docker_compose:
project_src: /opt/docker/minecraft
build: false
- name: Document running containers
debug:
msg: |
{
{% for item in servers %}
"{{ item.port|default(19132) }} - {{ item.name | default('default') }}": {
"mode": "{{ item.gamemode | default('survival') }}",
"difficulty": "{{ item.difficulty | default('easy') }}"{% if item.cheats | default('false') != 'false' %},
"cheats": "allowed"{% endif %}
},
{% endfor %}
}
#!/bin/bash
cd "$(dirname "$0")"
outfile="/tmp/$(date +%Y%m%d)-ansible-minecraft.out"
ansible-playbook playbook.yml 2>&1 > "$outfile"
if grep -qE "(changed|failed|fail|fatal):" "$outfile"; then
cat "$outfile"
fi
version: '3.4'
services:
{% for item in servers %}
minecraft_{{ item.gamemode | default('survival') }}_{{ item.difficulty | default('easy') }}_{{ item.name | default('default') }}:
image: itzg/minecraft-bedrock-server
restart: always
environment:
EULA: "TRUE"
GAMEMODE: {{ item.gamemode | default('survival') }}
DIFFICULTY: {{ item.difficulty | default('easy') }}
ALLOW_CHEATS: "{{ item.cheats | default('false') }}"
{% if item.seed | default('') | string | length > 0 %} LEVEL_SEED: '{{ item.seed | default('') }}'
{% endif %}
SERVER_NAME: "{{ item.server_name | default(ansible_fqdn) }}{% if item.name | default('') | length > 0 %} - {{ item.name }}{% endif %}"
WHITE_LIST: "true"
WHITE_LIST_USERS: {{ item.players | join(",") }}
DEFAULT_PLAYER_PERMISSION_LEVEL: operator
ports:
- {{ item.port|default(19132) }}:19132/udp
volumes:
- ./data_{{ item.gamemode | default('survival') }}_{{ item.difficulty | default('easy') }}_{{ item.name | default('default') }}:/data
stdin_open: true
tty: true
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment