- hosts: localhost vars: meta: application_name: 2FAuth playbook_version: 0.1 app_domain: "{{ uberspace_default_web_domain }}" app_admin_email: "{{ uberspace_mail_sysmail }}" _home: "/home/{{ uberspace_asteroid_name }}" _root: "/var/www/virtual/{{ uberspace_asteroid_name }}/2fauth" tasks: # Required by `uberspace app install`; skipped during installation. - name: _Return resolved playbook vars debug: var: vars tags: [playbook-vars] - name: Validate installation options assert: that: - app_admin_email is match('^[A-Za-z0-9_.+%-]+@[A-Za-z0-9.-]+$') - app_domain is match('^[a-zA-Z0-9][a-zA-Z0-9.-]*[a-zA-Z0-9]$') fail_msg: "Use a valid domain." - name: Inspect installation and service paths stat: path: "{{ item }}" loop: - "{{ _root }}" register: existing - name: Refuse to overwrite an existing installation or service assert: that: not item.stat.exists fail_msg: "{{ item.item }} already exists. This playbook only supports fresh installations." loop: "{{ existing.results }}" loop_control: label: "{{ item.item }}" - name: Inspect the document root find: paths: "/var/www/virtual/{{ uberspace_asteroid_name }}/html" hidden: true file_type: any excludes: [nocontent.html] register: contents - name: Require an empty document root assert: that: contents.matched == 0 fail_msg: "Use a fresh Asteroid with an empty document root." - name: Inspect existing web routes command: argv: [uberspace, web, backend, list, --json] register: routes changed_when: false check_mode: false - name: Refuse to replace another application's route assert: that: item.path != '/' or item.destination == 'APACHE' fail_msg: "{{ app_domain }}/ already serves another backend." loop: "{{ (routes.stdout | from_json) | selectattr('domain', 'equalto', app_domain) | list }}" loop_control: label: "{{ item.path }}" - name: List registered web domains command: argv: [uberspace, web, domain, list, --json] register: domains changed_when: false check_mode: false - name: Install 2FAuth when: not ansible_check_mode block: - name: Find the latest stable release uri: url: https://api.github.com/repos/Bubka/2FAuth/releases/latest return_content: true register: release - name: Validate the release assert: that: - not release.json.draft - not release.json.prerelease - release.json.tag_name is match('^v[0-9]+\.[0-9]+\.[0-9]+$') - name: Select PHP 8.4 command: argv: [uberspace, tool, php, version, set, '8.4'] - name: Clone the stable release git: repo: https://github.com/Bubka/2FAuth.git dest: "{{ _root }}" version: "{{ release.json.tag_name }}" depth: 1 - name: Install production dependencies command: argv: [composer, install, --prefer-dist, --no-dev, --no-scripts, --no-interaction, --optimize-autoloader] chdir: "{{ _root }}" - name: Create the SQLite database file: path: "{{ _root }}/database/database.sqlite" state: touch mode: '0600' - name: Write private configuration copy: dest: "{{ _root }}/.env" mode: '0600' content: | APP_NAME=2FAuth APP_ENV=production APP_DEBUG=false APP_KEY= APP_URL=https://{{ app_domain }} SITE_OWNER={{ app_admin_email }} DB_CONNECTION=sqlite DB_DATABASE={{ _root }}/database/database.sqlite CACHE_DRIVER=file SESSION_DRIVER=file SESSION_SECURE_COOKIE=true QUEUE_DRIVER=sync LOG_CHANNEL=daily MAIL_MAILER=sendmail MAIL_SENDMAIL_PATH="/usr/bin/sendmail -t -i" MAIL_FROM_ADDRESS={{ uberspace_asteroid_name }}@uber.space MAIL_FROM_NAME=2FAuth AUTHENTICATION_GUARD=web-guard TRUSTED_PROXIES=* WEBAUTHN_NAME=2FAuth WEBAUTHN_ID={{ app_domain }} - name: _Initialize application keys and database command: argv: "{{ ['php', 'artisan'] + item }}" chdir: "{{ _root }}" loop: - [key:generate, --force] - [package:discover] - [migrate, --force] - [passport:keys, --no-interaction] - [passport:client, --personal, --name, 2FAuth, --provider, users, --no-interaction] - ['2fauth:fix-passport-key-permissions'] - [storage:link] no_log: true - name: Write the administrator setup script copy: dest: "{{ _root }}/initialize-admin.php" mode: '0600' content: | make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); if (App\Models\User::count() !== 0) { throw new RuntimeException('Users already exist'); } $password = bin2hex(random_bytes(24)); $user = new App\Models\User(); $user->name = 'Administrator'; $user->email = {{ app_admin_email | to_json }}; $user->password = $password; $user->is_admin = true; $user->email_verified_at = now(); $user->save(); App\Facades\Settings::set('disableRegistration', true); App\Facades\Settings::set('useEncryption', true); App\Facades\Settings::set('checkForUpdate', false); file_put_contents(__DIR__ . '/admin-password', $password . PHP_EOL); chmod(__DIR__ . '/admin-password', 0600); - name: _Create the administrator before publishing command: argv: [php, initialize-admin.php] chdir: "{{ _root }}" no_log: true - name: Remove the setup script file: path: "{{ _root }}/initialize-admin.php" state: absent - name: Cache the configuration command: argv: [php, artisan, config:cache] chdir: "{{ _root }}" - name: Schedule application maintenance cron: name: 2FAuth scheduler job: "/usr/bin/php {{ _root }}/artisan schedule:run >> {{ _home }}/logs/2fauth-scheduler.log 2>&1" - name: Remove the empty default document root file: path: "/var/www/virtual/{{ uberspace_asteroid_name }}/html" state: absent - name: Serve only the application's public directory file: src: "{{ _root }}/public" dest: "/var/www/virtual/{{ uberspace_asteroid_name }}/html" state: link - name: Register the domain command: argv: [uberspace, web, domain, add, "{{ app_domain }}", --wait] when: app_domain not in (domains.stdout | from_json | map(attribute='name') | list) - name: Route the domain to Apache command: argv: [uberspace, web, backend, add, "{{ app_domain }}/", apache, --force, --wait] - name: Check public availability uri: url: "https://{{ app_domain }}/" register: site until: site.status == 200 retries: 12 delay: 5 - name: _Report debug: msg: | 2FAuth: https://{{ app_domain }} Version: {{ release.json.tag_name }} Administrator email: {{ app_admin_email }} Read /var/www/virtual/{{ uberspace_asteroid_name }}/2fauth/admin-password over SSH for the initial password. Registration is disabled and database encryption is enabled. Back up .env, storage and database together; do not regenerate APP_KEY. Guide: https://u8lab.uberspace.de/guide_2fauth/