From a2a3b10ac92dc8840b0ebe5d4c71660a27b6c470 Mon Sep 17 00:00:00 2001 From: Salt Date: Fri, 15 May 2020 10:11:32 -0500 Subject: [PATCH] Add a Terraria role Time to blow shit up --- ec2.yml | 9 +++ roles/terraria/defaults/main.yml | 1 + roles/terraria/meta/main.yml | 0 roles/terraria/tasks/main.yml | 72 ++++++++++++++++++++++ roles/terraria/templates/terraria@.service | 33 ++++++++++ 5 files changed, 115 insertions(+) create mode 100644 roles/terraria/defaults/main.yml create mode 100644 roles/terraria/meta/main.yml create mode 100644 roles/terraria/tasks/main.yml create mode 100644 roles/terraria/templates/terraria@.service diff --git a/ec2.yml b/ec2.yml index d9cfef0..a6eafa4 100644 --- a/ec2.yml +++ b/ec2.yml @@ -84,6 +84,15 @@ nextcloud_tarbz2: "https://download.nextcloud.com/server/releases/nextcloud-18.0.0.tar.bz2" nextcloud_url: "nc.9iron.club" nextcloud_webroot: "/var/www/nextcloud" +- hosts: tag_terraria_vanilla + roles: + - role: terraria + vars: + terraria_name: "vanilla1" + terraria_zip: "https://terraria.org/server/terraria-server-1344.zip" + terraria_port: "7777" + terraria_worldsize: "large" + terraria_password: "dicks" - hosts: tag_minecraft_ultreload roles: - role: backups diff --git a/roles/terraria/defaults/main.yml b/roles/terraria/defaults/main.yml new file mode 100644 index 0000000..36046f8 --- /dev/null +++ b/roles/terraria/defaults/main.yml @@ -0,0 +1 @@ +terraria_root: "/var/terraria" diff --git a/roles/terraria/meta/main.yml b/roles/terraria/meta/main.yml new file mode 100644 index 0000000..e69de29 diff --git a/roles/terraria/tasks/main.yml b/roles/terraria/tasks/main.yml new file mode 100644 index 0000000..1486d3e --- /dev/null +++ b/roles/terraria/tasks/main.yml @@ -0,0 +1,72 @@ +#!/usr/bin/ansible-playbook +# vim:ft=ansible: +--- +- name: Set up Terraria user + block: + - name: Install required packages + apt: + name: + - mono-complete + - name: Assure terraria admin group + group: + name: ter-admin + - name: Assure server user + user: + name: terraria + group: ter-admin + system: yes + home: "{{ terraria_root }}" + - name: Assure var directory + file: + path: "{{ terraria_root }}" + state: directory + owner: root + group: terraria + # Sticky, SetGID + mode: "3770" + - name: Assure systemd unit + template: + src: "terraria@.service" + dest: "/etc/systemd/system/terraria@.service" + - name: Reload available daemons + systemd: + daemon_reload: yes + become: yes +- name: Set up Terraria server + block: + - name: Create server directory + file: + path: "{{ terraria_root }}/{{ terraria_name }}" + state: directory + - name: Check for existing install + stat: + path: "{{ terraria_root }}/{{ terraria_name }}/TerrariaServer" + register: stat_server_binary + - name: Unpack archive + block: + - name: Download zip + get_url: + dest: "{{ terraria_root }}/{{ terraria_name }}/pack.zip" + url: "{{ terraria_zip }}" + - name: Unpack zip + unarchive: + src: "{{ terraria_root }}/{{ terraria_name }}/pack.zip" + remote_src: yes + dest: "{{ terraria_root }}/{{ terraria_name }}" + - name: Move zip contents + shell: mv "{{ terraria_root }}/{{ terraria_name }}/Dedicated Server/*" "{{ terraria_root }}/{{ terraria_name }}" + when: not stat_server_binary.stat.exists + - name: Configure server + block: + - name: Template out server config + template: + src: config + dest: "{{ terraria_root }}/{{ terraria_name }}/config" + become: yes +- name: Enable service + systemd: + name: "terraria@{{ terraria_name }}" + daemon_reload: yes + enabled: yes + state: started + become: yes diff --git a/roles/terraria/templates/terraria@.service b/roles/terraria/templates/terraria@.service new file mode 100644 index 0000000..8fb6919 --- /dev/null +++ b/roles/terraria/templates/terraria@.service @@ -0,0 +1,33 @@ +# +# Licensed under the terms of the MIT license +# vim:ft=dosini: +# + +[Unit] +Description=Terraria Server %i +After=network.target + +[Service] +User=terraria +Group=ter-admin +WorkingDirectory={{ terraria_root }}/%i +PrivateUsers=true +ProtectSystem=full +ProtectHome=true +# Implies MountFlags=slave +ProtectKernelTunables=true +# Implies NoNewPrivileges=yes +ProtectKernelModules=true +# Implies MountAPIVFS=yes +ProtectControlGroups=true + +ExecStart=/bin/sh -c '/usr/bin/screen -DmS ter-%i ./TerrariaServer.bin.x86_64' + +ExecStop=/usr/bin/screen -p 0 -S ter-%i -X eval 'stuff "say Server is going down: Service was stopped"\\015' +ExecStop=/usr/bin/screen -p 0 -S ter-%i -X eval 'stuff "exit"\\015' +ExecStop=/bin/sleep 5 + +Restart=always + +[Install] +WantedBy=multi-user.target