08_openclaw.yml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ---
  2. # playbooks/08_openclaw.yml
  3. # Deploy OpenClaw Telegram bot on ai_server (optional)
  4. - name: "OpenClaw | Deploy OpenClaw Telegram bot"
  5. hosts: ai_server
  6. become: true
  7. gather_facts: true
  8. tags:
  9. - openclaw
  10. vars:
  11. vault_token_file: "{{ playbook_dir }}/../vault/.vault-token"
  12. vault_url: "http://{{ ai_server_ip }}:{{ vault_port }}"
  13. openclaw_data_dir: /mnt/ai_data/openclaw
  14. vars_prompt:
  15. - name: telegram_token_input
  16. prompt: "Telegram Bot Token (from @BotFather). Press ENTER to use token already in Vault"
  17. private: false
  18. default: ""
  19. tasks:
  20. # ── Store token in Vault if provided ─────────────────────────────
  21. - name: "OpenClaw | Store Telegram token in Vault"
  22. ansible.builtin.uri:
  23. url: "{{ vault_url }}/v1/{{ vault_secret_prefix }}/openclaw"
  24. method: POST
  25. headers:
  26. X-Vault-Token: "{{ lookup('ansible.builtin.file', vault_token_file) }}"
  27. body_format: json
  28. body:
  29. data:
  30. telegram_token: "{{ telegram_token_input }}"
  31. status_code: [200, 204]
  32. when: telegram_token_input | length > 0
  33. tags:
  34. - openclaw-vault
  35. # ── Read token from Vault (whether just stored or pre-existing) ───
  36. - name: "OpenClaw | Check for Telegram token in Vault"
  37. ansible.builtin.uri:
  38. url: "{{ vault_url }}/v1/{{ vault_secret_prefix }}/openclaw"
  39. method: GET
  40. headers:
  41. X-Vault-Token: "{{ lookup('ansible.builtin.file', vault_token_file) }}"
  42. status_code: [200, 404]
  43. register: vault_openclaw_secret
  44. tags:
  45. - openclaw-vault
  46. - name: "OpenClaw | Set skip flag"
  47. ansible.builtin.set_fact:
  48. skip_openclaw: "{{ vault_openclaw_secret.status == 404 or
  49. vault_openclaw_secret.json.data.data.telegram_token | default('') | length == 0 }}"
  50. tags:
  51. - openclaw-vault
  52. - name: "OpenClaw | Display skip message"
  53. ansible.builtin.debug:
  54. msg: "Skipping OpenClaw installation (no Telegram token in Vault or provided)"
  55. when: skip_openclaw
  56. tags:
  57. - openclaw-vault
  58. - name: "OpenClaw | Set telegram_token fact"
  59. ansible.builtin.set_fact:
  60. telegram_token: "{{ vault_openclaw_secret.json.data.data.telegram_token }}"
  61. when: not skip_openclaw
  62. tags:
  63. - openclaw-vault
  64. # ── Retrieve Ollama API key from Vault ────────────────────────────
  65. - name: "OpenClaw | Retrieve Ollama API key from Vault"
  66. ansible.builtin.set_fact:
  67. ollama_api_key: "{{ lookup('community.hashi_vault.hashi_vault', vault_secret_prefix ~ '/ollama:api_key token=' ~ lookup('ansible.builtin.file', vault_token_file) ~ ' url=' ~ vault_url) }}"
  68. when: not skip_openclaw
  69. tags:
  70. - openclaw-config
  71. - name: "OpenClaw | Load model selection for model assignment"
  72. ansible.builtin.slurp:
  73. src: "{{ playbook_dir }}/../benchmarks/results/model_selection.json"
  74. delegate_to: localhost
  75. become: false
  76. register: _model_sel_raw
  77. ignore_errors: true
  78. when: not skip_openclaw
  79. tags:
  80. - openclaw-config
  81. - name: "OpenClaw | Set openclaw_model from benchmark slot 1 (best general)"
  82. ansible.builtin.set_fact:
  83. openclaw_model: "{{ (_model_sel_raw.content | b64decode | from_json).slot1_general }}"
  84. when:
  85. - not skip_openclaw
  86. - _model_sel_raw is not failed
  87. - _model_sel_raw.content is defined
  88. tags:
  89. - openclaw-config
  90. # ── Install Python dependencies ───────────────────────────────────
  91. - name: "OpenClaw | Install Python dependencies"
  92. ansible.builtin.pip:
  93. name:
  94. - python-telegram-bot
  95. - requests
  96. - pyyaml
  97. state: present
  98. executable: pip3
  99. when: not skip_openclaw
  100. tags:
  101. - openclaw-install
  102. # ── Deploy bot script and config ─────────────────────────────────
  103. - name: "OpenClaw | Create data directory"
  104. ansible.builtin.file:
  105. path: "{{ openclaw_data_dir }}"
  106. state: directory
  107. mode: "0755"
  108. owner: root
  109. group: root
  110. when: not skip_openclaw
  111. tags:
  112. - openclaw-config
  113. - name: "OpenClaw | Create log directory"
  114. ansible.builtin.file:
  115. path: /var/log/openclaw
  116. state: directory
  117. mode: "0755"
  118. owner: root
  119. group: root
  120. when: not skip_openclaw
  121. tags:
  122. - openclaw-config
  123. - name: "OpenClaw | Deploy bot script"
  124. ansible.builtin.copy:
  125. src: "{{ playbook_dir }}/../templates/openclaw/bot.py"
  126. dest: "{{ openclaw_data_dir }}/bot.py"
  127. mode: "0755"
  128. owner: root
  129. group: root
  130. when: not skip_openclaw
  131. tags:
  132. - openclaw-config
  133. - name: "OpenClaw | Template config.yml"
  134. ansible.builtin.template:
  135. src: "{{ playbook_dir }}/../templates/openclaw/config.yml.j2"
  136. dest: "{{ openclaw_data_dir }}/config.yml"
  137. mode: "0640"
  138. owner: root
  139. group: root
  140. when: not skip_openclaw
  141. tags:
  142. - openclaw-config
  143. # ── Systemd service ───────────────────────────────────────────────
  144. - name: "OpenClaw | Template systemd service"
  145. ansible.builtin.template:
  146. src: "{{ playbook_dir }}/../templates/openclaw/openclaw.service.j2"
  147. dest: /etc/systemd/system/openclaw.service
  148. mode: "0644"
  149. owner: root
  150. group: root
  151. when: not skip_openclaw
  152. tags:
  153. - openclaw-service
  154. - name: "OpenClaw | Reload systemd daemon"
  155. ansible.builtin.systemd:
  156. daemon_reload: true
  157. when: not skip_openclaw
  158. tags:
  159. - openclaw-service
  160. - name: "OpenClaw | Enable and start OpenClaw service"
  161. ansible.builtin.systemd:
  162. name: openclaw
  163. enabled: true
  164. state: started
  165. when: not skip_openclaw
  166. tags:
  167. - openclaw-service
  168. - name: "OpenClaw | Display status"
  169. ansible.builtin.debug:
  170. msg: "OpenClaw Telegram bot is installed and running. Message your bot to test it."
  171. when: not skip_openclaw
  172. tags:
  173. - openclaw-service