Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
289 changes: 289 additions & 0 deletions plex.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
#!/bin/bash
#
# Title: PGBlitz (Reference Title File)
# Author(s): Admin9705
# URL: https://pgblitz.com - http://github.pgblitz.com
# GNU: General Public License v3.0
################################################################################
---
- hosts: localhost
gather_facts: false
tasks:
# FACTS #######################################################################
- name: 'Set Known Facts'
set_fact:
pgrole: 'plex'
intport: '32400/tcp'
extport: '32400'
intport2: '3005/tcp'
extport2: '3005'
intport3: '8324/tcp'
extport3: '8324'
intport4: '32410/udp'
extport4: '32410'
intport5: '32412/udp'
extport5: '32412'
intport6: '32413/udp'
extport6: '32413'
intport7: '32414/udp'
extport7: '32414'
intport8: '33400/tcp'
extport8: '33400'
intport9: '33443/tcp'
extport9: '33443'
image: 'plexinc/pms-docker:latest'

# CORE (MANDATORY) ############################################################
- name: 'Including cron job'
include_tasks: '/opt/coreapps/apps/_core.yml'

# FACTS #######################################################################
- name: 'Plex Server Network Type'
shell: 'cat /var/plexguide/plex.server'
register: servertype

- name: Register Claim Number
shell: 'cat /var/plexguide/plex.claim'
register: claim
when: servertype.stdout == "remote"

# CORE (MANDATORY) ############################################################
- name: 'Including cron job'
include_tasks: '/opt/coreapps/apps/_core.yml'

######################################### FOR BOTH
- name: 'Set Default Labels'
set_fact:
default_labels:
traefik.enable: 'true'
traefik.port: '{{intport}}'
traefik.frontend.rule: 'Host:{{pgrole}}.{{domain.stdout}}{{tldset}}{{cname}}'
traefik.frontend.headers.SSLHost: '{{domain.stdout}}'
traefik.frontend.headers.SSLRedirect: 'true'
traefik.frontend.headers.STSIncludeSubdomains: 'true'
traefik.frontend.headers.STSPreload: 'true'
traefik.frontend.headers.STSSeconds: '315360000'
traefik.frontend.headers.browserXSSFilter: 'true'
traefik.frontend.headers.contentTypeNosniff: 'true'
traefik.frontend.headers.customResponseHeaders: 'X-Robots-Tag:noindex,nofollow,nosnippet,noarchive,notranslate,noimageindex'
traefik.frontend.headers.forceSTSHeader: 'true'

- name: 'Set Default Volume - {{extension.stdout}}'
set_fact:
default_volumes:
- '/etc/localtime:/etc/localtime:ro'
- '{{path.stdout}}:{{path.stdout}}'
- '/mnt:/mnt'
- '/opt/appdata/{{pgrole}}/database:/config'
- '/tmp:/tmp'
- '/opt/transcodes:/transcode'
- '/dev/shm:/ram_transcode'

- name: 'Establish Key Variables'
set_fact:
default_env:
UID: '1000'
GID: '1000'
- name: Check Preferences.xml exists
stat:
path: '/opt/appdata/plex/database/Library/Application Support/Plex Media Server/Preferences.xml'
register: plex_prefs

############### This is needed because on a fresh installaions the value is missing and is defaulted to 1 Security perfered
- name: secureConnections is missing
xml:
path: '/opt/appdata/plex/database/Library/Application Support/Plex Media Server/Preferences.xml'
xpath: /Preferences/@secureConnections
count: yes
register: secure_hits
when:
- plex_prefs.stat.exists

############### This is needed because on a fresh installaions the value is missing and is defaulted to 1 Security perfered
- name: Add secureConnections if missing
xml:
path: '/opt/appdata/plex/database/Library/Application Support/Plex Media Server/Preferences.xml'
xpath: /Preferences
attribute: secureConnections
value: '1'
when:
- plex_prefs.stat.exists
- secure_hits.count == 0

- name: Read Preferences.xml
xml:
path: '/opt/appdata/plex/database/Library/Application Support/Plex Media Server/Preferences.xml'
xpath: /Preferences
content: attribute
register: plex_pref_content
when: plex_prefs.stat.exists

- name: Add https to default_label if secureConnection != 2
set_fact:
default_labels: "{{default_labels | combine( {'traefik.protocol': 'https'} )}}"
when:
- plex_prefs.stat.exists
- plex_pref_content.matches[0]['Preferences']['secureConnections']

- name: Read customConnections
set_fact:
customConnections: "{{plex_pref_content.matches[0]['Preferences']['customConnections']}}"
when:
- plex_prefs.stat.exists
- plex_pref_content.matches[0]['Preferences']['customConnections'] is defined

- name: customConnections missing
set_fact:
customConnections: 'http://{{ipaddress.stdout}}:32400, http://plex.{{domain.stdout}}:32400'
when:
- plex_prefs.stat.exists == False

- name: 'Get Plex Custom URL'
shell: 'echo https://plex.{{domain.stdout}}:443,http://plex.{{domain.stdout}}:80'
register: plex_url
when: servertype.stdout == "remote"

- debug: msg="Your plex_url is {{ plex_url.stdout }}"
when: servertype.stdout == "remote"

- set_fact:
plex_advert_ip: 'http://{{ipaddress.stdout}}:32400'
when: servertype.stdout == "remote"

- set_fact:
plex_advert_ip: '{{plex_url.stdout}}'
when: servertype.stdout == "remote"

- debug: msg="plex_advert_ip is {{plex_advert_ip}}"
when: servertype.stdout == "remote"

########################################## Other
- name: Check /dev/dri exists
stat:
path: '/dev/dri'
register: dev_dri

- name: Create Transcodes Directory
file: 'path={{item}} state=directory mode=0775 owner=1000 group=1000'
with_items:
- /opt/transcodes

- name: customConnections FailSafe
set_fact:
customConnections: 'none'
when: customConnections is undefined

######################################### DEPLOY PLEX REMOTE (ANSWER 2)
- name: Deploy Plex Container (Remote Edition)
docker_container:
name: plex
image: '{{image}}'
pull: yes
published_ports:
- '{{extport}}:{{intport}}'
- '{{ports.stdout}}{{extport2}}:{{intport2}}'
- '{{ports.stdout}}{{extport3}}:{{intport3}}'
- '{{ports.stdout}}{{extport4}}:{{intport4}}'
- '{{ports.stdout}}{{extport5}}:{{intport5}}'
- '{{ports.stdout}}{{extport6}}:{{intport6}}'
- '{{ports.stdout}}{{extport7}}:{{intport7}}'
- '{{ports.stdout}}{{extport8}}:{{intport8}}'
- '{{extport9}}:{{intport9}}'
env:
PLEX_UID: '1000'
PLEX_GID: '1000'
PLEX_CLAIM: '{{claim.stdout}}'
ADVERTISE_IP: '{{customConnections}}'
NVIDIA_VISIBLE_DEVICES: 'all'
NVIDIA_DRIVER_CAPABILITIES: 'compute,video,utility'
purge_networks: yes
networks:
- name: plexguide
aliases:
- plex
volumes: '{{default_volumes}}'
devices: "{{ '/dev/dri:/dev/dri' if dev_dri.stat.exists == True | default(false) else omit }}"
etc_hosts:
{ 'analytics.plex.tv': '127.0.0.1', 'metrics.plex.tv': '127.0.0.1' }
restart_policy: unless-stopped
state: started
labels: '{{default_labels}}'
when: servertype.stdout == "remote"

######################################### DEPLOY PLEX LOCAL (ANSWER 3)
- name: Deploy Plex Container (Local Edition)
docker_container:
name: plex
image: '{{image}}'
pull: yes
published_ports:
- '32400:32400/tcp'
- '3005:3005/tcp'
- '8324:8324/tcp'
- '32469:32469/tcp'
- '1900:1900/udp'
- '32410:32410/udp'
- '32412:32412/udp'
- '32413:32413/udp'
- '32414:32414/udp'
- '33400:33400/tcp'
- '33443:33443/tcp'
env:
PLEX_UID: '1000'
PLEX_GID: '1000'
ADVERTISE_IP: '{{customConnections}}'
NVIDIA_VISIBLE_DEVICES: 'all'
NVIDIA_DRIVER_CAPABILITIES: 'compute,video,utility'
networks:
- name: plexguide
aliases:
- plex
volumes: '{{default_volumes}}'
devices: "{{ '/dev/dri:/dev/dri' if dev_dri.stat.exists == True | default(false) else omit }}"
etc_hosts:
{ 'analytics.plex.tv': '127.0.0.1', 'metrics.plex.tv': '127.0.0.1' }
restart_policy: unless-stopped
state: started
labels: '{{default_labels}}'
when: servertype.stdout == "local"

###################### Plex WebTools
- name: Check WebTools
stat:
path: '/opt/appdata/plex/database/Library/Application Support/Plex Media Server/Plug-ins/WebTools.bundle'
register: webtools

- name: Create Basic Directories
file: 'path={{item}} state=directory mode=0775 owner=1000 group=1000'
with_items:
- '/opt/appdata/plex/database/Library/Application Support/Plex Media Server/Plug-ins/WebTools.bundle'
when:
- webtools.stat.exists == False

- name: Installing WebTools
unarchive:
src: https://github.com/ukdtom/WebTools.bundle/releases/download/3.0.0/WebTools.bundle.zip
dest: /opt/appdata/plex/database/Library/Application Support/Plex Media Server/Plug-ins
keep_newer: no
owner: '1000'
group: '1000'
mode: 0775
copy: no
when:
- webtools.stat.exists == False

- name: Notify User
pause:
prompt: "\nWebTools Installed! [PRESS ENTER] to Continue"
when:
- webtools.stat.exists == False

- debug: msg="Final Notes - Remote Plex | IP Address {{ipaddress.stdout}} | Plex Type {{servertype.stdout}} | Token {{claim.stdout}}"
when: servertype.stdout == "remote"

- debug: msg="Final Notes - Local Plex | IP Address {{ipaddress.stdout}} | Plex Type {{servertype.stdout}}"
when: servertype.stdout == "local"

##PG-Core

##PG-Core