diff --git a/tandoor/files/.env b/tandoor/files/.env index 911722c..87db883 100644 --- a/tandoor/files/.env +++ b/tandoor/files/.env @@ -5,3 +5,4 @@ POSTGRES_PORT=5432 POSTGRES_USER=postgres POSTGRES_PASSWORD=123456 POSTGRES_DB=tandoor +ALLOWED_HOSTS={{ host }},localhost,127.0.0.1 \ No newline at end of file diff --git a/tandoor/playbook.yml b/tandoor/playbook.yml index 84e4fe6..9ab2848 100755 --- a/tandoor/playbook.yml +++ b/tandoor/playbook.yml @@ -2,72 +2,12 @@ # DO Community Playbooks: Docker ################################################# --- -- hosts: all +- hosts: monoco become: true vars_files: - vars/default.yml - tasks: - - name: Pull Tandoor.dev image - docker_image: - name: "{{ image_name }}" - source: pull - state: present - force_source: yes - - - name: Ensure Host Directory exists - file: - path: "/var/lib/{{ host_directory }}" - state: directory - group: www-data - owner: www-data - mode: 0655 - - - name: Upload the .env file - template: - src: "files/.env" - dest: "/var/lib/{{ host_directory }}/.env" - - - name: Create Tandoor.dev container - docker_container: - name: "{{ container_name }}" - image: "{{ image_name }}" - state: started - restart_policy: "unless-stopped" - env_file: "/var/lib/{{ host_directory }}/.env" - links: - - "postgres:db" - published_ports: - - "127.0.0.1:8880:8080" - volumes: - - "/var/lib/{{ host_directory }}/staticfiles:/opt/recipes/staticfiles" - - "/var/lib/{{ host_directory }}/mediafiles:/opt/recipes/mediafiles" - - "/var/lib/{{ host_directory }}/externalfiles:/opt/recipes/externalfiles" - - - name: Check if Nginx conf file exists - stat: path="/etc/nginx/sites-available/{{ http_conf }}" - register: status - - - name: No need to reload Nginx - debug: msg= {{ "No need to reload Nginx as sites-available entries have already been created" }} - - - name: Set Nginx conf file - when: status.stat.exists == false - template: - src: "files/nginx.conf.j2" - dest: "/etc/nginx/sites-available/{{ http_conf }}" - - - name: Enable new site - when: status.stat.exists == false - file: - src: "/etc/nginx/sites-available/{{ http_conf }}" - dest: "/etc/nginx/sites-enabled/{{ http_conf }}" - state: link - notify: Reload Nginx - - handlers: - - name: Reload Nginx - service: - name: nginx - state: reloaded - + roles: + - network + - tandoor + - caddy diff --git a/tandoor/roles/caddy/handlers/main.yaml b/tandoor/roles/caddy/handlers/main.yaml new file mode 100644 index 0000000..c3a3720 --- /dev/null +++ b/tandoor/roles/caddy/handlers/main.yaml @@ -0,0 +1,7 @@ +--- +# Handlers for Nginx role +- name: Restart caddy container + docker_container: + name: "{{ caddy_container }}" + state: started + restart: true \ No newline at end of file diff --git a/tandoor/roles/caddy/tasks/main.yaml b/tandoor/roles/caddy/tasks/main.yaml new file mode 100644 index 0000000..c3ccc90 --- /dev/null +++ b/tandoor/roles/caddy/tasks/main.yaml @@ -0,0 +1,22 @@ +--- +- name: Read current Caddyfile + ansible.builtin.slurp: + path: "{{ caddyfile_path }}" + register: caddyfile_raw + +- name: Decode Caddyfile content + set_fact: + caddyfile_content: "{{ caddyfile_raw['content'] | b64decode }}" + +- name: Check if snippet already exists + set_fact: + snippet_present: "{{ snippet_block in caddyfile_content }}" + +- name: Add snippet if missing + ansible.builtin.blockinfile: + path: "{{ caddyfile_path }}" + marker: "# {mark} Ansible managed Caddy snippet for {{ host }}" + block: "{{ snippet_block }}" + create: yes + when: not snippet_present + notify: Restart caddy container \ No newline at end of file diff --git a/tandoor/roles/network/tasks/main.yaml b/tandoor/roles/network/tasks/main.yaml new file mode 100644 index 0000000..4d8ce98 --- /dev/null +++ b/tandoor/roles/network/tasks/main.yaml @@ -0,0 +1,11 @@ +--- +- name: Ensure tandoor, caddy, postgres are on Docker network + docker_network: + name: "{{ docker_network }}" + state: present + connected: + - name: "{{ docker_container }}" + - name: "{{ postgres_container }}" + alias: db + - name: "{{ caddy_container }}" + appends: yes diff --git a/tandoor/roles/tandoor/tasks/main.yaml b/tandoor/roles/tandoor/tasks/main.yaml new file mode 100644 index 0000000..14b02b0 --- /dev/null +++ b/tandoor/roles/tandoor/tasks/main.yaml @@ -0,0 +1,42 @@ +--- +# Tasks for a Tandoor.dev role +- name: Pull image + docker_image: + name: "{{ docker_image }}" + source: pull + state: present + force_source: yes + +- name: Ensure Host Directory exists + file: + path: "/var/lib/{{ host_directory }}" + state: directory + mode: 0755 + +- name: Upload the .env file + template: + src: "files/.env" + dest: "/var/lib/{{ host_directory }}/.env" + + +# - name: Ensure Docker network exists +# docker_network: +# name: "{{ docker_network }}" +# connected: +# - name: "{{ postgres_container }}" +# alias: db +# - name: "{{ caddy_container }}" + +- name: Create container + docker_container: + name: "{{ docker_container }}" + image: "{{ docker_image }}" + state: started + restart_policy: "unless-stopped" + env_file: "/var/lib/{{ host_directory }}/.env" + volumes: + - "/var/lib/{{ host_directory }}/staticfiles:/opt/recipes/staticfiles" + - "/var/lib/{{ host_directory }}/mediafiles:/opt/recipes/mediafiles" + - "/var/lib/{{ host_directory }}/externalfiles:/opt/recipes/externalfiles" + networks: + - name: "{{ docker_network }}" diff --git a/tandoor/vars/default.yml b/tandoor/vars/default.yml index 3493f55..a51c438 100644 --- a/tandoor/vars/default.yml +++ b/tandoor/vars/default.yml @@ -1,9 +1,15 @@ --- -http_host: "recipes.tanshu.com" -http_conf: "recipes.tanshu.com.conf" -http_url: "https://recipes.tanshu.com" - -container_name: tandoor -image_name: vabene1111/recipes:latest +host: recipes.tanshu.com +docker_image: vabene1111/recipes:latest +docker_container: tandoor +docker_network: "{{ docker_container }}_net" host_directory: tandoor + +postgres_container: postgres +caddy_container: caddy +caddyfile_path: /var/lib/caddy/conf/Caddyfile +snippet_block: | + {{ host }} { + reverse_proxy {{ docker_container }}:80 + }