Ephes Blog

Miscellaneous things. Mostly Weeknotes and links I stumbled upon.

Date -

Deploying a Minecraft Server

, Jochen

Let's talk about one of life's little annoyances: I'm part of that "lucky" generation stuck between helping our parents with printers and setting up our kids' tech. And now, on top of printer duty, I've somehow inherited the job of deploying Minecraft servers. At least it's slightly less painful than dealing with printers.

I found these articles while researching this task:

Since we're an Apple household with iOS devices everywhere, Bedrock edition was really our only option. I decided to keep things simple and install it directly in a dedicated user account without messing with Docker. Here's the Ansible playbook I use to deploy the Bedrock server:

- name: Install Minecraft Bedrock Server
  hosts: all
  become: true
  vars:
    minecraft_user: "bedrock"
    minecraft_home: "/home/bedrock"
    minecraft_download_url: "https://www.minecraft.net/bedrockdedicatedserver/bin-linux/bedrock-server-1.21.60.10.zip"  # Update to latest version

  tasks:
    - name: Create Minecraft user
      user:
        name: "{{ minecraft_user }}"
        home: "{{ minecraft_home }}"
        create_home: true
        shell: /usr/bin/fish

    - name: Create Minecraft directory
      file:
        path: "{{ minecraft_home }}"
        state: directory
        owner: "{{ minecraft_user }}"
        group: "{{ minecraft_user }}"
        mode: "0755"

    - name: Download Minecraft Bedrock Server
      get_url:
        url: "{{ minecraft_download_url }}"
        dest: "/tmp/bedrock-server.zip"
        mode: "0644"

    - name: Extract Minecraft Bedrock Server
      unarchive:
        src: "/tmp/bedrock-server.zip"
        dest: "{{ minecraft_home }}"
        remote_src: true
        owner: "{{ minecraft_user }}"
        group: "{{ minecraft_user }}"

    - name: Ensure Minecraft server is executable
      file:
        path: "{{ minecraft_home }}/bedrock_server"
        mode: "0755"

    - name: Create a systemd service file for Minecraft Bedrock
      copy:
        dest: "/etc/systemd/system/minecraft_bedrock.service"
        content: |
          [Unit]
          Description=Minecraft Bedrock Server
          After=network.target

          [Service]
          User={{ minecraft_user }}
          WorkingDirectory={{ minecraft_home }}
          ExecStart={{ minecraft_home }}/bedrock_server
          Restart=always
          RestartSec=5
          StandardOutput=journal
          StandardError=journal
          LimitNOFILE=4096

          [Install]
          WantedBy=multi-user.target
      notify: Restart Minecraft Server

    - name: Enable and start Minecraft Bedrock service
      systemd:
        name: minecraft_bedrock
        enabled: true
        state: started

  handlers:
    - name: Restart Minecraft Server
      systemd:
        name: minecraft_bedrock
        state: restarted