From 0aa621b5104ffdf6a0015c34a1d4c0b5188fbbf3 Mon Sep 17 00:00:00 2001 From: Johanna Dorothea Reichmann Date: Mon, 6 Feb 2023 21:22:21 +0100 Subject: [PATCH] feat(ghost): add role for deployment using docker --- roles/ghost/defaults/main.yml | 38 ++++++++++++++++++++ roles/ghost/tasks/main.yml | 56 ++++++++++++++++++++++++++++++ roles/ghost/templates/ghost.env.j2 | 3 ++ roles/ghost/vars/main.yml | 10 ++++++ 4 files changed, 107 insertions(+) create mode 100644 roles/ghost/defaults/main.yml create mode 100644 roles/ghost/tasks/main.yml create mode 100644 roles/ghost/templates/ghost.env.j2 create mode 100644 roles/ghost/vars/main.yml diff --git a/roles/ghost/defaults/main.yml b/roles/ghost/defaults/main.yml new file mode 100644 index 0000000..16f4694 --- /dev/null +++ b/roles/ghost/defaults/main.yml @@ -0,0 +1,38 @@ +--- + +ghost_domain: ~ +ghost_version: "5.33.6" +ghost_user: ghost +ghost_user_group: ghost +ghost_base_path: /opt/ghost +ghost_data_path: "{{ ghost_base_path }}/data" +ghost_config_path: "{{ ghost_base_path }}/config" +ghost_config_file: "{{ ghost_config_path }}/ghost.env" +ghost_database_username: ghost +ghost_database_password: ~ +ghost_database_database: ghost +ghost_database_host: ~ +ghost_base_config: + url: "https://{{ ghost_domain }}" + database__client: mysql + database__connection__host: "{{ ghost_database_host }}" + database__connection__user: "{{ ghost_database_username }}" + database__connection__password: "{{ ghost_database_password }}" + database__connection__database: "{{ ghost_database_database }}" +ghost_config: {} + +ghost_container_name: ghost +ghost_container_image_name: docker.io/ghost +ghost_container_image_tag: ~ +ghost_container_base_volumes: + - "{{ ghost_data_path }}:{{ ghost_container_data_directory }}:rw" +ghost_container_extra_volumes: [] +ghost_container_volumes: + "{{ ghost_container_base_volumes + ghost_container_extra_volumes }}" +ghost_container_base_labels: + version: "{{ ghost_version }}" +ghost_container_extra_labels: {} +ghost_container_restart_policy: "unless-stopped" +ghost_container_networks: ~ +ghost_container_purge_networks: ~ +ghost_container_etc_hosts: ~ diff --git a/roles/ghost/tasks/main.yml b/roles/ghost/tasks/main.yml new file mode 100644 index 0000000..61fedff --- /dev/null +++ b/roles/ghost/tasks/main.yml @@ -0,0 +1,56 @@ +--- +- name: Ensure ghost group is created + ansible.builtin.group: + name: "{{ ghost_user_group }}" + state: present + system: true + +- name: Ensure ghost user is created + ansible.builtin.user: + name: "{{ ghost_user }}" + groups: + - "{{ ghost_user_group }}" + append: true + state: present + system: true + +- name: Ensure host paths for docker volumes exist for ghost + ansible.builtin.file: + path: "{{ item }}" + state: directory + mode: "0750" + owner: "{{ ghost_user }}" + group: "{{ ghost_user_group }}" + loop: + - "{{ ghost_base_path }}" + - "{{ ghost_data_path }}" + - "{{ ghost_config_path }}" + +- name: Ensure ghost configuration file is templated + ansible.builtin.template: + src: "ghost.env.j2" + dest: "{{ ghost_config_file }}" + owner: "{{ ghost_user }}" + group: "{{ ghost_user_group }}" + mode: "0644" + +- name: Ensure ghost container image is present on host + community.docker.docker_image: + name: "{{ ghost_container_image }}" + state: present + source: pull + force_source: "{{ ghost_container_image_tag is defined }}" + +- name: Ensure ghost container is running + community.docker.docker_container: + name: "{{ ghost_container_name }}" + image: "{{ ghost_container_image }}" + ports: "{{ ghost_container_ports | default(omit, true) }}" + labels: "{{ ghost_container_labels }}" + volumes: "{{ ghost_container_volumes }}" + env_file: "{{ ghost_config_file }}" + etc_hosts: "{{ ghost_container_etc_hosts | default(omit, true) }}" + networks: "{{ ghost_container_networks | default(omit, true) }}" + purge_networks: "{{ ghost_container_purge_networks | default(omit, true) }}" + restart_policy: "{{ ghost_container_restart_policy }}" + state: started diff --git a/roles/ghost/templates/ghost.env.j2 b/roles/ghost/templates/ghost.env.j2 new file mode 100644 index 0000000..e504abd --- /dev/null +++ b/roles/ghost/templates/ghost.env.j2 @@ -0,0 +1,3 @@ +{% for key, value in ghost_config_complete.items() %} +{{ key }}={{ value }} +{% endfor %} diff --git a/roles/ghost/vars/main.yml b/roles/ghost/vars/main.yml new file mode 100644 index 0000000..9708f49 --- /dev/null +++ b/roles/ghost/vars/main.yml @@ -0,0 +1,10 @@ +--- + +ghost_container_image: "{{ ghost_container_image_name}}:{{ ghost_container_image_tag | default(ghost_version, true) }}" +ghost_container_labels: >-2 + {{ ghost_container_base_labels + | combine(ghost_container_extra_labels) }} + +ghost_container_data_directory: "/var/lib/ghost/content" +ghost_config_complete: >-2 + {{ ghost_base_config | combine(ghost_config, recursive=True) }}