From afe72f554eee06aaf30e1daeb622f670e25aec2f Mon Sep 17 00:00:00 2001 From: transcaffeine Date: Sat, 5 Oct 2024 10:15:48 +0200 Subject: [PATCH] chore(nginx): add deployment_method=podman --- roles/nginx/README.md | 5 +++++ roles/nginx/defaults/main.yml | 1 + roles/nginx/meta/main.yml | 1 + roles/nginx/tasks/deploy-docker.yml | 28 ++++++++++++++++++++++++ roles/nginx/tasks/deploy-podman.yml | 27 +++++++++++++++++++++++ roles/nginx/tasks/main.yml | 33 +++++++++-------------------- roles/nginx/vars/main.yml | 3 +++ 7 files changed, 75 insertions(+), 23 deletions(-) create mode 100644 roles/nginx/tasks/deploy-docker.yml create mode 100644 roles/nginx/tasks/deploy-podman.yml diff --git a/roles/nginx/README.md b/roles/nginx/README.md index c64c185..0da2300 100644 --- a/roles/nginx/README.md +++ b/roles/nginx/README.md @@ -26,3 +26,8 @@ For exposing this server to the host and/or internet, the `nginx_container_ports from host to container), `nginx_container_networks` (docker networking) or `nginx_container_labels` (for label-based routing discovery like traefik) can be used. The options correspond to the arguments of the `community.docker.docker_container` module. + +## Deployment methods + +Set `nginx_deployment_method` to either `docker` or `podman` to use the respective ansible modules for +creating and managing the container and its image. See all supported methods in `nginx_deployment_methods`. diff --git a/roles/nginx/defaults/main.yml b/roles/nginx/defaults/main.yml index ec74323..109cf73 100644 --- a/roles/nginx/defaults/main.yml +++ b/roles/nginx/defaults/main.yml @@ -4,6 +4,7 @@ nginx_flavour: alpine nginx_base_path: /opt/nginx nginx_config_file: "{{ nginx_base_path }}/nginx.conf" nginx_state: present +nginx_deployment_method: docker nginx_container_name: nginx nginx_container_image_reference: >- diff --git a/roles/nginx/meta/main.yml b/roles/nginx/meta/main.yml index b411852..ce53198 100644 --- a/roles/nginx/meta/main.yml +++ b/roles/nginx/meta/main.yml @@ -9,3 +9,4 @@ galaxy_info: - http - webserver - docker + - podman diff --git a/roles/nginx/tasks/deploy-docker.yml b/roles/nginx/tasks/deploy-docker.yml new file mode 100644 index 0000000..a91b301 --- /dev/null +++ b/roles/nginx/tasks/deploy-docker.yml @@ -0,0 +1,28 @@ +--- +- name: Ensure docker container image '{{ nginx_container_image_reference }}' is {{ nginx_state }} + community.docker.docker_image: + name: "{{ nginx_container_image_reference }}" + state: "{{ nginx_state }}" + source: "{{ nginx_container_image_source }}" + force_source: >-2 + {{ nginx_container_image_force_source + | default(nginx_container_image_tag | default(false, true)) }} + register: nginx_container_image_info + until: nginx_container_image_info is success + retries: 5 + delay: 3 + +- name: Ensure docker container '{{ nginx_container_name }}' is {{ nginx_container_state }} + community.docker.docker_container: + name: "{{ nginx_container_name }}" + image: "{{ nginx_container_image_reference }}" + env: "{{ nginx_container_env | default(omit, true) }}" + user: "{{ nginx_container_user | default(omit, true) }}" + ports: "{{ nginx_container_ports | default(omit, true) }}" + labels: "{{ nginx_container_labels | default(omit, true) }}" + volumes: "{{ nginx_container_volumes | default(omit, true) }}" + etc_hosts: "{{ nginx_container_etc_hosts | default(omit, true) }}" + networks: "{{ nginx_container_networks | default(omit, true) }}" + purge_networks: "{{ nginx_container_purge_networks | default(omit, true) }}" + restart_policy: "{{ nginx_container_restart_policy }}" + state: "{{ nginx_container_state }}" diff --git a/roles/nginx/tasks/deploy-podman.yml b/roles/nginx/tasks/deploy-podman.yml new file mode 100644 index 0000000..2b9206a --- /dev/null +++ b/roles/nginx/tasks/deploy-podman.yml @@ -0,0 +1,27 @@ +--- +- name: Ensure container image '{{ nginx_container_image_reference }}' is {{ nginx_state }} + containers.podman.podman_image: + name: "{{ nginx_container_image_reference }}" + state: "{{ nginx_state }}" + pull: "{{ nginx_container_image_source == 'pull' }}" + force: >-2 + {{ nginx_container_image_force_source + | default(nginx_container_image_tag | default(false, true)) }} + register: nginx_container_image_info + until: nginx_container_image_info is success + retries: 5 + delay: 3 + +- name: Ensure container '{{ nginx_container_name }}' is {{ nginx_container_state }} + containers.podman.podman_container: + name: "{{ nginx_container_name }}" + image: "{{ nginx_container_image_reference }}" + env: "{{ nginx_container_env | default(omit, true) }}" + user: "{{ nginx_container_user | default(omit, true) }}" + ports: "{{ nginx_container_ports | default(omit, true) }}" + labels: "{{ nginx_container_labels | default(omit, true) }}" + volumes: "{{ nginx_container_volumes | default(omit, true) }}" + etc_hosts: "{{ nginx_container_etc_hosts | default(omit, true) }}" + network: "{{ nginx_container_networks | default(omit, true) }}" + restart_policy: "{{ nginx_container_restart_policy }}" + state: "{{ nginx_container_state }}" diff --git a/roles/nginx/tasks/main.yml b/roles/nginx/tasks/main.yml index 9b95af9..52463a8 100644 --- a/roles/nginx/tasks/main.yml +++ b/roles/nginx/tasks/main.yml @@ -6,6 +6,13 @@ states are {{ nginx_states | join(', ') }}. when: nginx_state not in nginx_states +- name: Check if deployment_method is supported + ansible.builtin.fail: + msg: >-2 + Unsupported state '{{ nginx_deployment_method }}'. Supported + states are {{ nginx_deployment_methods | join(', ') }}. + when: nginx_deployment_method not in nginx_deployment_methods + - name: Ensure nginx config file is {{ nginx_state }} ansible.builtin.file: path: "{{ nginx_config_file }}" @@ -28,26 +35,6 @@ - restart-nginx when: nginx_state == 'present' -- name: Ensure docker container image '{{ nginx_container_image_reference }}' is {{ nginx_state }} - community.docker.docker_image: - name: "{{ nginx_container_image_reference }}" - state: "{{ nginx_state }}" - source: "{{ nginx_container_image_source }}" - force_source: >-2 - {{ nginx_container_image_force_source - | default(nginx_container_image_tag | default(false, true)) }} - -- name: Ensure docker container '{{ nginx_container_name }}' is {{ nginx_container_state }} - community.docker.docker_container: - name: "{{ nginx_container_name }}" - image: "{{ nginx_container_image_reference }}" - env: "{{ nginx_container_env | default(omit, true) }}" - user: "{{ nginx_container_user | default(omit, true) }}" - ports: "{{ nginx_container_ports | default(omit, true) }}" - labels: "{{ nginx_container_labels | default(omit, true) }}" - volumes: "{{ nginx_container_volumes | default(omit, true) }}" - etc_hosts: "{{ nginx_container_etc_hosts | default(omit, true) }}" - networks: "{{ nginx_container_networks | default(omit, true) }}" - purge_networks: "{{ nginx_container_purge_networks | default(omit, true) }}" - restart_policy: "{{ nginx_container_restart_policy }}" - state: "{{ nginx_container_state }}" +- name: Deploy using {{ nginx_deployment_method }} + ansible.builtin.include_tasks: + file: "deploy-{{ nginx_deployment_method }}.yml" diff --git a/roles/nginx/vars/main.yml b/roles/nginx/vars/main.yml index 022ab75..793def4 100644 --- a/roles/nginx/vars/main.yml +++ b/roles/nginx/vars/main.yml @@ -2,3 +2,6 @@ nginx_states: - present - absent +nginx_deployment_methods: + - docker + - podman