02_infrastructure.yml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ---
  2. # playbooks/02_infrastructure.yml
  3. # Install Docker and configure Ollama on ai_server
  4. - name: "Infrastructure | Docker and Ollama setup on ai_server"
  5. hosts: ai_server
  6. become: true
  7. gather_facts: true
  8. tags:
  9. - infrastructure
  10. vars:
  11. vault_token_file: "{{ playbook_dir }}/../vault/.vault-token"
  12. vault_url: "http://{{ ai_server_ip }}:{{ vault_port }}"
  13. ollama_num_threads: 28
  14. ollama_num_parallel: 4
  15. ollama_max_loaded_models: 4
  16. ollama_keep_alive: "-1"
  17. ollama_numa_node: "1"
  18. ollama_cpu_affinity: "1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55"
  19. pre_tasks:
  20. - name: "Infrastructure | Install Python Docker SDK prerequisites"
  21. ansible.builtin.dnf:
  22. name:
  23. - python3-pip
  24. - python3-requests
  25. state: present
  26. tags: always
  27. - name: "Infrastructure | Install Python docker SDK via pip"
  28. ansible.builtin.pip:
  29. name: docker
  30. state: present
  31. executable: pip3
  32. tags: always
  33. tasks:
  34. # ── Docker installation ──────────────────────────────────────────
  35. - name: "Docker | Check if Docker CE repo is already configured"
  36. ansible.builtin.stat:
  37. path: /etc/yum.repos.d/docker-ce.repo
  38. register: docker_repo_file
  39. tags:
  40. - docker
  41. - name: "Docker | Add Docker CE repository"
  42. ansible.builtin.command:
  43. cmd: dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
  44. when: not docker_repo_file.stat.exists
  45. changed_when: true
  46. tags:
  47. - docker
  48. - name: "Docker | Install Docker CE packages"
  49. ansible.builtin.dnf:
  50. name:
  51. - docker-ce
  52. - docker-ce-cli
  53. - containerd.io
  54. - docker-compose-plugin
  55. state: present
  56. tags:
  57. - docker
  58. - name: "Docker | Add {{ ansible_user }} to docker group"
  59. ansible.builtin.user:
  60. name: "{{ ansible_user }}"
  61. groups: docker
  62. append: true
  63. tags:
  64. - docker
  65. - name: "Docker | Add ollama user to docker group"
  66. ansible.builtin.user:
  67. name: ollama
  68. groups: docker
  69. append: true
  70. tags:
  71. - docker
  72. - name: "Docker | Start and enable docker.service"
  73. ansible.builtin.systemd:
  74. name: docker
  75. state: started
  76. enabled: true
  77. tags:
  78. - docker
  79. # ── Ollama installation and configuration ────────────────────────
  80. - name: "Ollama | Check if ollama binary exists"
  81. ansible.builtin.stat:
  82. path: "{{ item }}"
  83. loop:
  84. - /usr/local/bin/ollama
  85. - /usr/bin/ollama
  86. register: ollama_binary_check
  87. tags:
  88. - ollama
  89. - name: "Ollama | Set ollama installed fact"
  90. ansible.builtin.set_fact:
  91. ollama_installed: "{{ ollama_binary_check.results | selectattr('stat.exists', 'equalto', true) | list | length > 0 }}"
  92. tags:
  93. - ollama
  94. - name: "Ollama | Install Ollama"
  95. ansible.builtin.shell:
  96. cmd: curl -fsSL https://ollama.ai/install.sh | sh
  97. when: not ollama_installed
  98. changed_when: true
  99. tags:
  100. - ollama
  101. - name: "Ollama | Retrieve OLLAMA_API_KEY from Vault"
  102. ansible.builtin.set_fact:
  103. 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) }}"
  104. tags:
  105. - ollama
  106. - name: "Ollama | Create systemd override directory"
  107. ansible.builtin.file:
  108. path: /etc/systemd/system/ollama.service.d
  109. state: directory
  110. mode: "0755"
  111. owner: root
  112. group: root
  113. tags:
  114. - ollama
  115. - name: "Ollama | Template systemd override configuration"
  116. ansible.builtin.template:
  117. src: "{{ playbook_dir }}/../templates/ollama/override.conf.j2"
  118. dest: /etc/systemd/system/ollama.service.d/override.conf
  119. mode: "0644"
  120. owner: root
  121. group: root
  122. notify:
  123. - Reload systemd and restart ollama
  124. tags:
  125. - ollama
  126. - name: "Ollama | Ensure Ollama is running"
  127. ansible.builtin.systemd:
  128. name: ollama
  129. state: started
  130. enabled: true
  131. tags:
  132. - ollama
  133. - name: "Ollama | Wait for Ollama API to be ready"
  134. ansible.builtin.uri:
  135. url: "http://localhost:11434/api/tags"
  136. method: GET
  137. status_code: 200
  138. timeout: 10
  139. register: ollama_ready
  140. retries: 24
  141. delay: 5
  142. until: ollama_ready.status == 200
  143. tags:
  144. - ollama
  145. handlers:
  146. - name: Reload systemd and restart ollama
  147. ansible.builtin.systemd:
  148. name: ollama
  149. state: restarted
  150. daemon_reload: true