diff --git a/nginx-menu/files/nginx.conf.j2 b/nginx-menu/files/nginx.conf.j2 new file mode 100644 index 0000000..d0c04f6 --- /dev/null +++ b/nginx-menu/files/nginx.conf.j2 @@ -0,0 +1,24 @@ +server { + listen 80; + server_name {{ host }}; + + location /bar { + root /var/lib/menu.greatbear.in; + index index.html; + } + + location /food { + root /var/lib/menu.greatbear.in; + index index.html; + } + + location /delivery { + root /var/lib/menu.greatbear.in; + index index.html; + } + + location /qr { + root /var/lib/menu.greatbear.in; + index index.html; + } +} \ No newline at end of file diff --git a/nginx-menu/playbook.yml b/nginx-menu/playbook.yml new file mode 100755 index 0000000..cb2e759 --- /dev/null +++ b/nginx-menu/playbook.yml @@ -0,0 +1,10 @@ +--- +- hosts: all + become: true + vars_files: + - vars/default.yml + + roles: + - network + - nginx + - caddy diff --git a/nginx-menu/readme.md b/nginx-menu/readme.md new file mode 100644 index 0000000..f6d7ecc --- /dev/null +++ b/nginx-menu/readme.md @@ -0,0 +1,46 @@ +# Docker on Ubuntu 18.04 + +This playbook will install Docker an Ubuntu 18.04 machine, as explained in the guide on +[How to Use Ansible to Install and Set Up Docker on Ubuntu 18.04](https://www.digitalocean.com/community/tutorials/how-to-use-ansible-to-install-and-set-up-docker-on-ubuntu-18-04). +A number of containers will be created with the options specified in the `vars/default.yml` variable file. + +## Settings + +- `create_containers`: number of containers to create. +- `default_container_name`: default name for new containers. +- `default_container_image`: default image for new containers. +- `default_container_command`: default command to run on new containers. + + +## Running this Playbook + +Quick Steps: + +### 1. Obtain the playbook +```shell +git clone https://github.com/do-community/ansible-playbooks.git +cd ansible-playbooks/docker_ubuntu1804 +``` + +### 2. Customize Options + +```shell +nano vars/default.yml +``` + +```yml +#vars/default.yml +--- +create_containers: 4 +default_container_name: docker +default_container_image: ubuntu +default_container_command: sleep 1d +``` + +### 3. Run the Playbook + +```command +ansible-playbook -l [target] -i [inventory file] -u [remote user] playbook.yml +``` + +For more information on how to run this Ansible setup, please check this guide: [How to Use Ansible to Install and Set Up Docker on Ubuntu 18.04](https://www.digitalocean.com/community/tutorials/how-to-use-ansible-to-install-and-set-up-docker-on-ubuntu-18-04). \ No newline at end of file diff --git a/nginx-menu/roles/caddy/handlers/main.yaml b/nginx-menu/roles/caddy/handlers/main.yaml new file mode 100644 index 0000000..294bf14 --- /dev/null +++ b/nginx-menu/roles/caddy/handlers/main.yaml @@ -0,0 +1,5 @@ +--- +# Handlers for Caddy role +- name: Reload Caddy configuration + ansible.builtin.command: "docker exec -w /etc/caddy {{ caddy_container }} caddy reload" + listen: "Reload Caddy" \ No newline at end of file diff --git a/nginx-menu/roles/caddy/tasks/main.yaml b/nginx-menu/roles/caddy/tasks/main.yaml new file mode 100644 index 0000000..9616151 --- /dev/null +++ b/nginx-menu/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: "Reload Caddy" \ No newline at end of file diff --git a/nginx-menu/roles/network/tasks/main.yaml b/nginx-menu/roles/network/tasks/main.yaml new file mode 100644 index 0000000..ebda867 --- /dev/null +++ b/nginx-menu/roles/network/tasks/main.yaml @@ -0,0 +1,8 @@ +--- +- name: "Ensure {{ docker_container }} Docker network exists" + docker_network: + name: "{{ docker_network }}" + state: present + connected: + - "{{ caddy_container }}" + appends: yes diff --git a/nginx-menu/roles/nginx/tasks/main.yaml b/nginx-menu/roles/nginx/tasks/main.yaml new file mode 100644 index 0000000..d2c0847 --- /dev/null +++ b/nginx-menu/roles/nginx/tasks/main.yaml @@ -0,0 +1,30 @@ +--- +# Tasks for Nginx role to serve static files +- name: Pull Nginx image + docker_image: + name: "{{ image_name }}" + source: pull + force_source: yes + +- name: Ensure Host Directory exist + file: + path: "{{ host_directory }}" + state: directory + mode: 0755 + +# - name: Upload the Nginx config file +# template: +# src: "files/nginx.conf.j2" +# dest: "{{ host_directory }}/nginx.conf" + +- name: Create caddy container + docker_container: + name: "{{ docker_container }}" + image: "{{ image_name }}" + state: started + restart_policy: unless-stopped + volumes: + # - "{{ host_directory }}/nginx.conf:/etc/nginx/nginx.conf:ro" + - "{{ host_directory }}:/usr/share/nginx/html:ro" + networks: + - name: "{{ docker_network }}" diff --git a/nginx-menu/vars/default.yml b/nginx-menu/vars/default.yml new file mode 100644 index 0000000..c9c76af --- /dev/null +++ b/nginx-menu/vars/default.yml @@ -0,0 +1,17 @@ +--- +title: hngmohali.com +host: "menu.{{ title }}" + +docker_container: "nginx-{{ host }}" +image_name: nginx:alpine + +caddy_container: caddy +docker_network: "{{ host }}_net" + +caddyfile_path: /var/lib/caddy/conf/Caddyfile +snippet_block: | + {{ host }} { + reverse_proxy {{ docker_container }}:80 + } + +host_directory: "/var/lib/{{ host }}"