chore(nginx): add state=absent support

This commit is contained in:
transcaffeine 2024-10-05 10:04:19 +02:00
parent 97526aec36
commit c36e95d7eb
Signed by: transcaffeine
GPG Key ID: 03624C433676E465
4 changed files with 44 additions and 9 deletions

View File

@ -3,6 +3,7 @@ nginx_version: "1.27.2"
nginx_flavour: alpine nginx_flavour: alpine
nginx_base_path: /opt/nginx nginx_base_path: /opt/nginx
nginx_config_file: "{{ nginx_base_path }}/nginx.conf" nginx_config_file: "{{ nginx_base_path }}/nginx.conf"
nginx_state: present
nginx_container_name: nginx nginx_container_name: nginx
nginx_container_image_reference: >- nginx_container_image_reference: >-
@ -25,6 +26,9 @@ nginx_container_image_repository: >-
nginx_container_image_registry: "docker.io" nginx_container_image_registry: "docker.io"
nginx_container_image_name: "nginx" nginx_container_image_name: "nginx"
nginx_container_image_tag: ~ nginx_container_image_tag: ~
nginx_container_image_source: pull
nginx_container_state: >-2
{{ (nginx_state == 'present') | ternary('started', 'absent') }}
nginx_container_restart_policy: "unless-stopped" nginx_container_restart_policy: "unless-stopped"
nginx_container_volumes: nginx_container_volumes:

11
roles/nginx/meta/main.yml Normal file
View File

@ -0,0 +1,11 @@
---
allow_duplicates: true
dependencies: []
galaxy_info:
role_name: nginx
description: Deploy nginx, a webserver
galaxy_tags:
- nginx
- http
- webserver
- docker

View File

@ -1,10 +1,23 @@
--- ---
- name: Check if state is supported
ansible.builtin.fail:
msg: >-2
Unsupported state '{{ nginx_state }}'. Supported
states are {{ nginx_states | join(', ') }}.
when: nginx_state not in nginx_states
- name: Ensure base path '{{ nginx_base_path }}' exists - name: Ensure nginx config file is {{ nginx_state }}
ansible.builtin.file:
path: "{{ nginx_config_file }}"
state: "{{ nginx_state }}"
when: nginx_state == 'absent'
- name: Ensure base path '{{ nginx_base_path }}' is {{ nginx_state }}
ansible.builtin.file: ansible.builtin.file:
path: "{{ nginx_base_path }}" path: "{{ nginx_base_path }}"
state: directory mode: "0755"
mode: 0755 state: >-2
{{ (nginx_state == 'present') | ternary('directory', 'absent') }}
- name: Ensure nginx config file is templated - name: Ensure nginx config file is templated
ansible.builtin.copy: ansible.builtin.copy:
@ -13,15 +26,18 @@
mode: 0640 mode: 0640
notify: notify:
- restart-nginx - restart-nginx
when: nginx_state == 'present'
- name: Ensure docker container image is present - name: Ensure docker container image '{{ nginx_container_image_reference }}' is {{ nginx_state }}
community.docker.docker_image: community.docker.docker_image:
name: "{{ nginx_container_image_reference }}" name: "{{ nginx_container_image_reference }}"
state: present state: "{{ nginx_state }}"
source: pull source: "{{ nginx_container_image_source }}"
force_source: "{{ nginx_container_image_tag is defined and nginx_container_image_tag | string != '' }}" force_source: >-2
{{ nginx_container_image_force_source
| default(nginx_container_image_tag | default(false, true)) }}
- name: Ensure docker container '{{ nginx_container_name }}' is running - name: Ensure docker container '{{ nginx_container_name }}' is {{ nginx_container_state }}
community.docker.docker_container: community.docker.docker_container:
name: "{{ nginx_container_name }}" name: "{{ nginx_container_name }}"
image: "{{ nginx_container_image_reference }}" image: "{{ nginx_container_image_reference }}"
@ -34,4 +50,4 @@
networks: "{{ nginx_container_networks | default(omit, true) }}" networks: "{{ nginx_container_networks | default(omit, true) }}"
purge_networks: "{{ nginx_container_purge_networks | default(omit, true) }}" purge_networks: "{{ nginx_container_purge_networks | default(omit, true) }}"
restart_policy: "{{ nginx_container_restart_policy }}" restart_policy: "{{ nginx_container_restart_policy }}"
state: started state: "{{ nginx_container_state }}"

View File

@ -0,0 +1,4 @@
---
nginx_states:
- present
- absent