personal_infra/ansible/services/lnbits/deploy_lnbits_playbook.yml

245 lines
7.4 KiB
YAML

- name: Deploy LNBits with Poetry and configure Caddy reverse proxy
hosts: vipy
become: yes
vars_files:
- ../../infra_vars.yml
- ./lnbits_vars.yml
vars:
lnbits_domain: "{{ lnbits_subdomain }}.{{ root_domain }}"
tasks:
- name: Create lnbits directory
file:
path: "{{ lnbits_dir }}"
state: directory
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
mode: '0755'
- name: Install required system packages
apt:
name:
- python3
- python3-pip
- git
- curl
- build-essential
- pkg-config
- libffi-dev
- libssl-dev
- zlib1g-dev
- libbz2-dev
- libreadline-dev
- libsqlite3-dev
- libncursesw5-dev
- xz-utils
- tk-dev
- libxml2-dev
- libxmlsec1-dev
- liblzma-dev
state: present
update_cache: yes
- name: Install pyenv
shell: |
curl https://pyenv.run | bash
args:
creates: "/home/{{ ansible_user }}/.pyenv"
become: yes
become_user: "{{ ansible_user }}"
environment:
HOME: "/home/{{ ansible_user }}"
- name: Add pyenv to PATH
lineinfile:
path: "/home/{{ ansible_user }}/.bashrc"
line: 'export PYENV_ROOT="$HOME/.pyenv"'
state: present
become: yes
become_user: "{{ ansible_user }}"
- name: Add pyenv init to bashrc
lineinfile:
path: "/home/{{ ansible_user }}/.bashrc"
line: 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"'
state: present
become: yes
become_user: "{{ ansible_user }}"
- name: Add pyenv init to bashrc (second line)
lineinfile:
path: "/home/{{ ansible_user }}/.bashrc"
line: 'eval "$(pyenv init -)"'
state: present
become: yes
become_user: "{{ ansible_user }}"
- name: Install Python 3.12 via pyenv
shell: |
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
pyenv install -s 3.12.7
pyenv global 3.12.7
args:
creates: "/home/{{ ansible_user }}/.pyenv/versions/3.12.7/bin/python3.12"
become: yes
become_user: "{{ ansible_user }}"
environment:
HOME: "/home/{{ ansible_user }}"
- name: Install Poetry
shell: |
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PYENV_ROOT/versions/3.12.7/bin:$PATH"
eval "$(pyenv init -)"
curl -sSL https://install.python-poetry.org | python3 -
args:
creates: "/home/{{ ansible_user }}/.local/bin/poetry"
become: yes
become_user: "{{ ansible_user }}"
environment:
HOME: "/home/{{ ansible_user }}"
- name: Add Poetry to PATH
lineinfile:
path: "/home/{{ ansible_user }}/.bashrc"
line: 'export PATH="$HOME/.local/bin:$PATH"'
state: present
become: yes
become_user: "{{ ansible_user }}"
- name: Clone LNBits repository
git:
repo: https://github.com/lnbits/lnbits.git
dest: "{{ lnbits_dir }}/lnbits"
version: main
accept_hostkey: yes
- name: Change ownership of LNBits directory to user
file:
path: "{{ lnbits_dir }}/lnbits"
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
recurse: yes
- name: Configure Poetry to use Python 3.12
command: /home/{{ ansible_user }}/.local/bin/poetry env use /home/{{ ansible_user }}/.pyenv/versions/3.12.7/bin/python3.12
args:
chdir: "{{ lnbits_dir }}/lnbits"
become: yes
become_user: "{{ ansible_user }}"
environment:
HOME: "/home/{{ ansible_user }}"
PATH: "/home/{{ ansible_user }}/.local/bin:/home/{{ ansible_user }}/.pyenv/versions/3.12.7/bin:/home/{{ ansible_user }}/.pyenv/bin:{{ ansible_env.PATH }}"
PYENV_ROOT: "/home/{{ ansible_user }}/.pyenv"
- name: Install LNBits dependencies
command: /home/{{ ansible_user }}/.local/bin/poetry install --only main
args:
chdir: "{{ lnbits_dir }}/lnbits"
become: yes
become_user: "{{ ansible_user }}"
environment:
HOME: "/home/{{ ansible_user }}"
PATH: "/home/{{ ansible_user }}/.local/bin:/home/{{ ansible_user }}/.pyenv/versions/3.12.7/bin:/home/{{ ansible_user }}/.pyenv/bin:{{ ansible_env.PATH }}"
PYENV_ROOT: "/home/{{ ansible_user }}/.pyenv"
- name: Copy .env.example to .env
copy:
src: "{{ lnbits_dir }}/lnbits/.env.example"
dest: "{{ lnbits_dir }}/lnbits/.env"
remote_src: yes
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
mode: '0644'
- name: Configure LNBits environment variables
lineinfile:
path: "{{ lnbits_dir }}/lnbits/.env"
regexp: "^{{ item.key }}="
line: "{{ item.key }}={{ item.value }}"
state: present
loop:
- key: "LNBITS_BACKEND_WALLET_CLASS"
value: "FakeWallet"
- key: "LNBITS_ADMIN_UI"
value: "true"
- key: "HOST"
value: "0.0.0.0"
- key: "PORT"
value: "{{ lnbits_port }}"
- key: "LNBITS_DATA_FOLDER"
value: "{{ lnbits_data_dir }}"
- key: "LNBITS_EXTENSIONS_PATH"
value: "{{ lnbits_data_dir }}/extensions"
- name: Create systemd service file for LNBits
copy:
dest: /etc/systemd/system/lnbits.service
content: |
[Unit]
Description=LNBits
After=network.target
[Service]
Type=simple
User={{ ansible_user }}
WorkingDirectory={{ lnbits_dir }}/lnbits
ExecStart=/home/{{ ansible_user }}/.local/bin/poetry run lnbits
Restart=always
RestartSec=30
Environment=PYTHONUNBUFFERED=1
Environment="PATH=/home/{{ ansible_user }}/.local/bin:/home/{{ ansible_user }}/.pyenv/versions/3.12.7/bin:/home/{{ ansible_user }}/.pyenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Environment="PYENV_ROOT=/home/{{ ansible_user }}/.pyenv"
[Install]
WantedBy=multi-user.target
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
mode: '0644'
- name: Reload systemd
systemd:
daemon_reload: yes
- name: Enable and start LNBits service
systemd:
name: lnbits
enabled: yes
state: started
- name: Ensure Caddy sites-enabled directory exists
file:
path: "{{ caddy_sites_dir }}"
state: directory
owner: root
group: root
mode: '0755'
- name: Ensure Caddyfile includes import directive for sites-enabled
lineinfile:
path: /etc/caddy/Caddyfile
line: 'import sites-enabled/*'
insertafter: EOF
state: present
backup: yes
create: yes
mode: '0644'
- name: Create Caddy reverse proxy configuration for lnbits
copy:
dest: "{{ caddy_sites_dir }}/lnbits.conf"
content: |
{{ lnbits_domain }} {
reverse_proxy localhost:{{ lnbits_port }} {
header_up X-Forwarded-Host {{ lnbits_domain }}
}
}
owner: root
group: root
mode: '0644'
- name: Reload Caddy to apply new config
command: systemctl reload caddy