Ansible Learnings – Week 6 – Automating Docker Deployments – Minecraft Servers!

So I played with docker on the weekend to make my son a MineCraft server because he wanted one and I was thinking – “Well I have been how great docker (and Kubernetes is), why not see if there is a docker container for MineCraft (MC), and sure enough there is so lets give this a go…”

Well after reading this brilliant quick guide (https://www.freecodecamp.org/news/a-beginners-guide-to-docker-how-to-create-your-first-docker-application-cc03de9b639f/?source=rss——programming-5) , installing a Ubuntu server, installing docker and finding the MC docker container on Docker Hub – I had deployed my first container! It was easy… A little too easy… Most probably by the exceptional work by the developer who made the docker container – Itzg, link below to his docker container.

https://hub.docker.com/r/itzg/minecraft-server/

This is a super vanilla Minecraft server, though I hardly know or care enough at this point to invest more time finding out what other kind of servers you can have and the differences. Itzg, does however provide a few variants within his container, so there is an option there for anyone that is interested.

This was super easy for me to setup for my son and his friend, I thought surely Ansible can execute a docker run command through a module, sure enough there is a good collection of docker modules. Ansible has even released recently a more agnostic container management system, which is the future and I will get up to speed with it over the next few weeks (see: https://docs.ansible.com/ansible-container/), but I am sticking with the more traditional one for this example.

Deploy_mc_docker-servers.yml

---

- name: Docker Deploy Minecraft Servers
  become: yes
  gather_facts: no
  hosts: NVDKR3
  vars:
    default_container_image: itzg/minecraft-server

  tasks:
    - name: Deploy Docker MC Servers
      docker_container:
          name: "{{ item.name }}"
          image: "{{ default_container_image }}"
          published_ports: "{{ item.pub_port }}"
          env:
            EULA: "TRUE"
            
# List to loop over to create the MC docker containers with their unique name and ports
      loop:
        - { name: 'NV-MC1', pub_port: '25565:25565' }
        - { name: 'NV-MC2', pub_port: '25566:25565' }
        - { name: 'NV-MC3', pub_port: '25567:25565' }
        - { name: 'NV-MC4', pub_port: '25568:25565' }

Results

(venv) Daniels-MacBook-Pro:Lab03-Servers daniel$ ansible-playbook deploy_mc_docker-servers.yml -i hosts

PLAY [Docker Deploy Minecraft Servers] **********************************************************************************************************************************************************************************************************************

TASK [Deploy Docker MC Servers] *****************************************************************************************************************************************************************************************************************************
changed: [NVDKR3] => (item={'name': 'NV-MC1', 'pub_port': '25565:25565'})
changed: [NVDKR3] => (item={'name': 'NV-MC2', 'pub_port': '25566:25565'})
changed: [NVDKR3] => (item={'name': 'NV-MC3', 'pub_port': '25567:25565'})
changed: [NVDKR3] => (item={'name': 'NV-MC4', 'pub_port': '25568:25565'})

PLAY RECAP **************************************************************************************************************************************************************************************************************************************************
NVDKR3                     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Review of the servers running on Docker

Summary

As you can see, this is a very simple and flat playbook. It took me about 2 hours of reading, and playing around with it. From here I am going to take it further and I am going to use Jinja2 templating. Also I will be building into the playbook the tasks to incorporate Palo Alto service object creation and security policy creation to allow communication across vlan’s and firewall zones within the one playbook. If I do really well with it, I am going to try deploying it across multiple docker nodes as well and share how that goes.

I suspect this next action is going to be very rewarding, as I am still very fresh with Jinja2 and this playbook makes a good case to use it and shouldn’t be too complicated to implement it and thus make the playbook more portable.

As always let me know what you guys think especially if you already have ideas on how I could do this playbook better!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.