08_openclaw.yml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. # ── Install Python dependencies ───────────────────────────────────
  72. - name: "OpenClaw | Install Python dependencies"
  73. ansible.builtin.pip:
  74. name:
  75. - python-telegram-bot
  76. - requests
  77. - pyyaml
  78. state: present
  79. executable: pip3
  80. when: not skip_openclaw
  81. tags:
  82. - openclaw-install
  83. # ── Deploy bot script and config ─────────────────────────────────
  84. - name: "OpenClaw | Create data directory"
  85. ansible.builtin.file:
  86. path: "{{ openclaw_data_dir }}"
  87. state: directory
  88. mode: "0755"
  89. owner: root
  90. group: root
  91. when: not skip_openclaw
  92. tags:
  93. - openclaw-config
  94. - name: "OpenClaw | Create log directory"
  95. ansible.builtin.file:
  96. path: /var/log/openclaw
  97. state: directory
  98. mode: "0755"
  99. owner: root
  100. group: root
  101. when: not skip_openclaw
  102. tags:
  103. - openclaw-config
  104. - name: "OpenClaw | Deploy bot script"
  105. ansible.builtin.copy:
  106. src: "{{ playbook_dir }}/../templates/openclaw/bot.py"
  107. dest: "{{ openclaw_data_dir }}/bot.py"
  108. mode: "0755"
  109. owner: root
  110. group: root
  111. when: not skip_openclaw
  112. tags:
  113. - openclaw-config
  114. - name: "OpenClaw | Template config.yml"
  115. ansible.builtin.template:
  116. src: "{{ playbook_dir }}/../templates/openclaw/config.yml.j2"
  117. dest: "{{ openclaw_data_dir }}/config.yml"
  118. mode: "0640"
  119. owner: root
  120. group: root
  121. when: not skip_openclaw
  122. tags:
  123. - openclaw-config
  124. # ── Systemd service ───────────────────────────────────────────────
  125. - name: "OpenClaw | Template systemd service"
  126. ansible.builtin.template:
  127. src: "{{ playbook_dir }}/../templates/openclaw/openclaw.service.j2"
  128. dest: /etc/systemd/system/openclaw.service
  129. mode: "0644"
  130. owner: root
  131. group: root
  132. when: not skip_openclaw
  133. tags:
  134. - openclaw-service
  135. - name: "OpenClaw | Reload systemd daemon"
  136. ansible.builtin.systemd:
  137. daemon_reload: true
  138. when: not skip_openclaw
  139. tags:
  140. - openclaw-service
  141. - name: "OpenClaw | Enable and start OpenClaw service"
  142. ansible.builtin.systemd:
  143. name: openclaw
  144. enabled: true
  145. state: started
  146. when: not skip_openclaw
  147. tags:
  148. - openclaw-service
  149. - name: "OpenClaw | Display status"
  150. ansible.builtin.debug:
  151. msg: "OpenClaw Telegram bot is installed and running. Message your bot to test it."
  152. when: not skip_openclaw
  153. tags:
  154. - openclaw-service