--- # repo tasks file for n8n - name: Ensure n8n group exists ansible.builtin.group: name: "{{ n8n_group }}" system: true - name: Ensure n8n user exists ansible.builtin.user: name: "{{ n8n_user }}" group: "{{ n8n_group }}" system: true create_home: false shell: /usr/sbin/nologin - name: Ensure n8n directory exists ansible.builtin.file: path: "{{ n8n_home }}" state: directory owner: "{{ n8n_user }}" group: "{{ n8n_group }}" mode: "0755" - name: Check n8n repo directory ansible.builtin.stat: path: "{{ n8n_home }}" register: n8n_repo_home - name: Check n8n git metadata ansible.builtin.stat: path: "{{ n8n_home }}/.git" register: n8n_repo_git - name: Check n8n directory contents ansible.builtin.find: paths: "{{ n8n_home }}" hidden: true recurse: false register: n8n_repo_contents when: n8n_repo_home.stat.exists - name: Remove n8n directory when not a git repo ansible.builtin.file: path: "{{ n8n_home }}" state: absent when: - n8n_repo_git.stat.exists is not defined or not n8n_repo_git.stat.exists - (n8n_app_options.clean_path | default(false)) - name: Abort when n8n directory is not a git repo ansible.builtin.fail: msg: "n8n path {{ n8n_home }} exists but is not a git repo. Set app_options.clean_path: true to wipe it." when: - n8n_repo_home.stat.exists - n8n_repo_git.stat.exists is not defined or not n8n_repo_git.stat.exists - (n8n_repo_contents.files | default([]) | length) > 0 - not (n8n_app_options.clean_path | default(false)) - name: Ensure n8n repo ownership ansible.builtin.file: path: "{{ n8n_home }}" state: directory owner: "{{ n8n_user }}" group: "{{ n8n_group }}" recurse: true when: n8n_repo_git.stat.exists - name: Mark n8n repo as safe for git ansible.builtin.command: "git config --global --add safe.directory {{ n8n_home }}" become_user: "{{ n8n_user }}" when: n8n_repo_git.stat.exists - name: Ensure n8n repo origin exists ansible.builtin.command: "git remote add origin {{ n8n_repo_path }}" args: chdir: "{{ n8n_home }}" become_user: "{{ n8n_user }}" changed_when: false failed_when: false when: n8n_repo_git.stat.exists - name: Ensure n8n repo origin matches ansible.builtin.command: "git remote set-url origin {{ n8n_repo_path }}" args: chdir: "{{ n8n_home }}" become_user: "{{ n8n_user }}" when: n8n_repo_git.stat.exists - name: Install pnpm globally npm: name: pnpm global: true state: present become: true - name: Clone n8n repo ansible.builtin.git: repo: "{{ n8n_repo_path }}" dest: "{{ n8n_home }}" version: "{{ n8n_repo_version }}" update: true force: true become_user: "{{ n8n_user }}" - name: Ensure n8n repo ownership ansible.builtin.file: path: "{{ n8n_home }}" state: directory owner: "{{ n8n_user }}" group: "{{ n8n_group }}" recurse: true - name: Create n8n env file ansible.builtin.template: src: "{{ n8n_app_options.env_template | default('n8n.env.j2') }}" dest: "{{ n8n_home }}/.env" owner: "{{ n8n_user }}" group: "{{ n8n_group }}" mode: "0640" - name: Install n8n dependencies (pnpm install) ansible.builtin.command: pnpm install --frozen-lockfile=false args: chdir: "{{ n8n_home }}" become_user: "{{ n8n_user }}" - name: Build n8n ansible.builtin.command: /bin/sh -lc "{{ n8n_build_command }}" args: chdir: "{{ n8n_home }}" become_user: "{{ n8n_user }}" - name: Write n8n repo systemd unit ansible.builtin.template: src: "{{ n8n_app_options.systemd_template | default('n8n-repo.service.j2') }}" dest: "/etc/systemd/system/{{ n8n_unit_name }}.service" mode: "0644" become: true when: n8n_systemd - name: Reload systemd for n8n repo ansible.builtin.systemd: daemon_reload: true become: true when: n8n_systemd - name: Enable and start n8n repo ansible.builtin.systemd: name: "{{ n8n_unit_name }}.service" enabled: true state: started become: true when: n8n_systemd