From 6346684af53e134b4445bca0b499d326c6c770a1 Mon Sep 17 00:00:00 2001 From: Haitao Pan Date: Sun, 14 Jun 2026 11:09:52 +0800 Subject: [PATCH] fix: support standalone postgres and dynamic litellm path --- roles/vhosts/litellm/tasks/main.yml | 22 +++++++++- .../templates/litellm-proxy.service.j2 | 4 +- roles/vhosts/litellm/templates/litellm.env.j2 | 2 +- roles/vhosts/postgres/defaults/main.yml | 20 ++++++++++ roles/vhosts/postgres/tasks/main.yml | 40 +++++++++++++++++++ setup-ai-workspace-all-in-one.yml | 2 +- setup-postgres-standalone.yaml | 8 ++++ 7 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 roles/vhosts/postgres/defaults/main.yml create mode 100644 setup-postgres-standalone.yaml diff --git a/roles/vhosts/litellm/tasks/main.yml b/roles/vhosts/litellm/tasks/main.yml index 8c7612b..d06f651 100644 --- a/roles/vhosts/litellm/tasks/main.yml +++ b/roles/vhosts/litellm/tasks/main.yml @@ -66,12 +66,32 @@ become: true become_user: "{{ litellm_service_user }}" +- name: Resolve LiteLLM Python site-packages path + ansible.builtin.shell: | + python3 - <<'PY' + import glob + import os + paths = glob.glob(os.path.expanduser("~/.local/lib/python*/site-packages/litellm/proxy")) + if not paths: + raise SystemExit("litellm proxy package path not found") + print(sorted(paths)[-1]) + PY + register: litellm_proxy_package_path + changed_when: false + become: true + become_user: "{{ litellm_service_user }}" + +- name: Set LiteLLM Python paths + ansible.builtin.set_fact: + litellm_proxy_dir: "{{ litellm_proxy_package_path.stdout | trim }}" + litellm_python_site_packages: "{{ (litellm_proxy_package_path.stdout | trim) | dirname | dirname }}" + - name: Generate Prisma Python Client ansible.builtin.shell: | export PATH={{ litellm_service_home }}/.local/bin:$PATH prisma generate args: - chdir: "{{ litellm_service_home }}/.local/lib/python3.12/site-packages/litellm/proxy/" + chdir: "{{ litellm_proxy_dir }}" become: true become_user: "{{ litellm_service_user }}" changed_when: false diff --git a/roles/vhosts/litellm/templates/litellm-proxy.service.j2 b/roles/vhosts/litellm/templates/litellm-proxy.service.j2 index 8fff2d1..01e14e3 100644 --- a/roles/vhosts/litellm/templates/litellm-proxy.service.j2 +++ b/roles/vhosts/litellm/templates/litellm-proxy.service.j2 @@ -8,7 +8,7 @@ User={{ litellm_service_user }} Group={{ litellm_service_group }} WorkingDirectory={{ litellm_service_home }} EnvironmentFile={{ litellm_env_file }} -Environment=PYTHONPATH={{ litellm_service_home }}/.local/lib/python3.12/site-packages +Environment=PYTHONPATH={{ litellm_python_site_packages | default(litellm_service_home ~ '/.local/lib/python3.12/site-packages') }} Environment=PYTHONUSERBASE={{ litellm_service_home }}/.local Environment=PATH={{ litellm_service_home }}/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin ExecStart={{ litellm_service_home }}/.local/bin/litellm --host {{ litellm_listen_host }} --port {{ litellm_listen_port }} --config {{ litellm_config_file }} @@ -19,4 +19,4 @@ StandardError=journal SyslogIdentifier=litellm-proxy [Install] -WantedBy=multi-user.target \ No newline at end of file +WantedBy=multi-user.target diff --git a/roles/vhosts/litellm/templates/litellm.env.j2 b/roles/vhosts/litellm/templates/litellm.env.j2 index 3bf4683..755876a 100644 --- a/roles/vhosts/litellm/templates/litellm.env.j2 +++ b/roles/vhosts/litellm/templates/litellm.env.j2 @@ -6,5 +6,5 @@ LITELLM_DB_PASSWORD={{ litellm_database_password }} {% if litellm_database_url | trim | length > 0 %} DATABASE_URL={{ litellm_database_url }} {% endif %} -PYTHONPATH={{ litellm_service_home }}/.local/lib/python3.12/site-packages +PYTHONPATH={{ litellm_python_site_packages | default(litellm_service_home ~ '/.local/lib/python3.12/site-packages') }} STORE_MODEL_IN_DB=True diff --git a/roles/vhosts/postgres/defaults/main.yml b/roles/vhosts/postgres/defaults/main.yml new file mode 100644 index 0000000..8ca3458 --- /dev/null +++ b/roles/vhosts/postgres/defaults/main.yml @@ -0,0 +1,20 @@ +--- +postgresql_use_official_repo: false +postgresql_package_dependencies: + - ca-certificates + - gnupg +postgresql_repo_key_url: https://www.postgresql.org/media/keys/ACCC4CF8.asc +postgresql_repo_key_path: /etc/apt/trusted.gpg.d/postgresql.asc +postgresql_repo: "deb http://apt.postgresql.org/pub/repos/apt {{ ansible_distribution_release }}-pgdg main" +postgresql_packages_base: + - postgresql + - postgresql-contrib +postgresql_extra_packages: [] +postgresql_service_name: postgresql +postgresql_conf_path: /etc/postgresql/17/main/postgresql.conf +postgresql_hba_path: /etc/postgresql/17/main/pg_hba.conf +postgresql_listen_addresses: "127.0.0.1" +postgresql_port: 5432 +postgresql_password_encryption: scram-sha-256 +postgresql_allowed_hosts: [] +postgresql_auth_method: scram-sha-256 diff --git a/roles/vhosts/postgres/tasks/main.yml b/roles/vhosts/postgres/tasks/main.yml index 6122c26..e884d7d 100644 --- a/roles/vhosts/postgres/tasks/main.yml +++ b/roles/vhosts/postgres/tasks/main.yml @@ -66,6 +66,26 @@ path: "{{ postgresql_conf_path }}" register: postgresql_conf_file +- name: Discover PostgreSQL configuration file when default path is absent + ansible.builtin.shell: | + set -e + find /etc/postgresql -path '*/main/postgresql.conf' -type f | sort -V | tail -n 1 + register: postgresql_conf_discovery + changed_when: false + when: not postgresql_conf_file.stat.exists + +- name: Use discovered PostgreSQL configuration file + ansible.builtin.set_fact: + postgresql_conf_path: "{{ postgresql_conf_discovery.stdout | trim }}" + when: + - not postgresql_conf_file.stat.exists + - postgresql_conf_discovery.stdout | trim | length > 0 + +- name: Refresh facts for PostgreSQL configuration file + ansible.builtin.stat: + path: "{{ postgresql_conf_path }}" + register: postgresql_conf_file + - name: Configure listen_addresses in postgresql.conf ansible.builtin.lineinfile: path: "{{ postgresql_conf_path }}" @@ -97,6 +117,26 @@ path: "{{ postgresql_hba_path }}" register: postgresql_hba_file +- name: Discover PostgreSQL pg_hba.conf when default path is absent + ansible.builtin.shell: | + set -e + find /etc/postgresql -path '*/main/pg_hba.conf' -type f | sort -V | tail -n 1 + register: postgresql_hba_discovery + changed_when: false + when: not postgresql_hba_file.stat.exists + +- name: Use discovered PostgreSQL pg_hba.conf + ansible.builtin.set_fact: + postgresql_hba_path: "{{ postgresql_hba_discovery.stdout | trim }}" + when: + - not postgresql_hba_file.stat.exists + - postgresql_hba_discovery.stdout | trim | length > 0 + +- name: Refresh facts for PostgreSQL pg_hba.conf + ansible.builtin.stat: + path: "{{ postgresql_hba_path }}" + register: postgresql_hba_file + - name: Configure pg_hba.conf access rules ansible.builtin.blockinfile: path: "{{ postgresql_hba_path }}" diff --git a/setup-ai-workspace-all-in-one.yml b/setup-ai-workspace-all-in-one.yml index 8fea8ac..baade90 100644 --- a/setup-ai-workspace-all-in-one.yml +++ b/setup-ai-workspace-all-in-one.yml @@ -40,7 +40,7 @@ # 基础数据与密钥设施 - import_playbook: setup-vault.yaml -- import_playbook: deploy_postgresql_svc_plus.yml +- import_playbook: setup-postgres-standalone.yaml - import_playbook: setup-litellm.yaml # 大模型与 AI Agents diff --git a/setup-postgres-standalone.yaml b/setup-postgres-standalone.yaml new file mode 100644 index 0000000..c5fcb43 --- /dev/null +++ b/setup-postgres-standalone.yaml @@ -0,0 +1,8 @@ +--- +- name: Deploy standalone PostgreSQL + hosts: all + become: true + gather_facts: true + roles: + - role: roles/vhosts/postgres + tags: [postgresql]