Deploying Plex with docker and ansible

After deploying watchtower to one of my docker instances, I noticed that it was having issues with restarting the container. The actual fault turned out to be the networking with portainer, but in the meantime, I wrote an ansible playbook to redeploy / restart the container.

The first thing I needed to do was make sure the configuration volume was created. This is one of the 2 volumes that the container needs access to. The other being the media directory. The initial part of my playbook set up the variables I was going to use and the plex-config volume. I like to give descriptive names to things, as if it all goes to pot, I have a fighting chance of sorting this in portainer or by hand.

---
  - name: "Plex on Docker Playbook"
    hosts: dockerhost
    become: yes
    become_method: sudo

    vars:
    - container: plex
    - domain: 'docker.host'

    tasks:

    - name: Create the homepage configuration volume
      docker_volume:
        name: "{{ container }}-config"
      tags: volumecreate

Once completed, I set up my labels for traefik. I don't actually use the TLS redirect for plex, but I do like the fact that I can. This is also part of the basics of my docker template.

    - name: Create Traefik labels's dictionary
      set_fact:
        my_labels: "{{ my_labels | default({}) | combine ({ item.key : item.value }) }}"
      with_items:
      - { 'key': 'traefik.enable' , 'value': 'true'}
      - { 'key': 'traefik.docker.network', 'value': "traefik-public"}
      - { 'key': "traefik.http.middlewares.{{ container }}-https-redirect.redirectscheme.scheme", 'value': "https"}
      - { 'key': "traefik.http.routers.{{ container }}-secure.entrypoints",'value': "https"}
      - { 'key': "traefik.http.routers.{{ container }}-secure.rule",'value': "Host(`{{ container }}.{{ domain }}`)"}
      - { 'key': "traefik.http.routers.{{ container }}-secure.service",'value': "{{ container }}"}
      - { 'key': "traefik.http.routers.{{ container }}-secure.tls",'value': "true"}
      - { 'key': "traefik.http.routers.{{ container }}.entrypoints",'value': "http"}
      - { 'key': "traefik.http.routers.{{ container }}.middlewares",'value': "{{ container }}-https-redirect"}
      - { 'key': "traefik.http.routers.{{ container }}.rule",'value': "Host(`{{ container }}.{{ domain }}`)"}
      - { 'key': "traefik.http.services.{{ container }}.loadbalancer.server.port", 'value': "32400"}

The last stage is the details of the actual container. For this, using the bridge networking actually was causing me the issues, hence it is commented out.

    - name: Start Plex and apply labels
      docker_container:
        name: "{{ container }}"
        state: started
#        networks:
#        - name: bridge
        image: lscr.io/linuxserver/plex:latest
        env:
          PUID: "1000"
          PGID: "1000"
          TZ: "Etc/UTC"
          VERSION: "docker"
        ports:
        - "32400:32400"
        - "1900:1900/udp"
        - "5353:5353/udp"
        - "8324:8324"
        - "32410:32410/udp"
        - "32412:32412/udp"
        - "32413:32413/udp"
        - "32414:32414/udp"
        - "32469:32469"
        volumes:
        - "{{ container }}-config:/config"
        - /mnt/data/media:/media
        devices:
        - /dev/dri:/dev/dri
        labels: "{{ my_labels }}"
      tags: deploycontainer

Finally, bring it all together gives us the following playbook.

---
  - name: "Plex on Docker Playbook"
    hosts: dockerhost
    become: yes
    become_method: sudo

    vars:
    - container: plex
    - domain: 'docker.host'

    tasks:

    - name: Create the homepage configuration volume
      docker_volume:
        name: "{{ container }}-config"
      tags: volumecreate

    - name: Create Traefik labels's dictionary
      set_fact:
        my_labels: "{{ my_labels | default({}) | combine ({ item.key : item.value }) }}"
      with_items:
      - { 'key': 'traefik.enable' , 'value': 'true'}
      - { 'key': 'traefik.docker.network', 'value': "traefik-public"}
      - { 'key': "traefik.http.middlewares.{{ container }}-https-redirect.redirectscheme.scheme", 'value': "https"}
      - { 'key': "traefik.http.routers.{{ container }}-secure.entrypoints",'value': "https"}
      - { 'key': "traefik.http.routers.{{ container }}-secure.rule",'value': "Host(`{{ container }}.{{ domain }}`)"}
      - { 'key': "traefik.http.routers.{{ container }}-secure.service",'value': "{{ container }}"}
      - { 'key': "traefik.http.routers.{{ container }}-secure.tls",'value': "true"}
      - { 'key': "traefik.http.routers.{{ container }}.entrypoints",'value': "http"}
      - { 'key': "traefik.http.routers.{{ container }}.middlewares",'value': "{{ container }}-https-redirect"}
      - { 'key': "traefik.http.routers.{{ container }}.rule",'value': "Host(`{{ container }}.{{ domain }}`)"}
      - { 'key': "traefik.http.services.{{ container }}.loadbalancer.server.port", 'value': "32400"}

    - name: Start Plex and apply labels
      docker_container:
        name: "{{ container }}"
        state: started
#        networks:
#        - name: bridge
        image: lscr.io/linuxserver/plex:latest
        env:
          PUID: "1000"
          PGID: "1000"
          TZ: "Etc/UTC"
          VERSION: "docker"
        ports:
        - "32400:32400"
        - "1900:1900/udp"
        - "5353:5353/udp"
        - "8324:8324"
        - "32410:32410/udp"
        - "32412:32412/udp"
        - "32413:32413/udp"
        - "32414:32414/udp"
        - "32469:32469"
        volumes:
        - "{{ container }}-config:/config"
        - /mnt/data/media:/media
        devices:
        - /dev/dri:/dev/dri
        labels: "{{ my_labels }}"
      tags: deploycontainer
As an Amazon Associate I earn from qualifying purchases.

If you have found this post useful, please consider donating.