Merge pull request #54 from corvus-migratorius/role-nginx-docker

add nginx_docker role
This commit is contained in:
Fogucoco
2025-12-18 18:06:43 +03:00
committed by GitHub
18 changed files with 262 additions and 1 deletions

View File

@@ -19,6 +19,7 @@
- [mkfs](roles/mkfs/README.md)
- [mount_device](roles/mount_device/README.md)
- [nginx](roles/nginx/README.md)
- [nginx_docker](roles/nginx_docker/README.md)
- [node_exporter](roles/node_exporter/README.md)
- [prometheus](roles/prometheus/README.md)
- [promtail](roles/promtail/README.md)

View File

@@ -1,7 +1,7 @@
---
namespace: genlab
name: common
version: 0.28.0
version: 0.29.0
readme: README.md
authors:
- Alexander Gorelyshev (corvus-migratorius@proton.me)

View File

@@ -0,0 +1,34 @@
ansible-nginx-docker
=========
Deploy NGINX as a Docker container
Requirements
------------
Docker
Role Variables
--------------
None
Dependencies
------------
Soft dependency on `genlab.docker_ubuntu` (any means of installing Docker on the managed machine should be fine).
Example Playbook
----------------
see [converge.yml](molecule/default/converge.yml)
License
-------
BSD
Author Information
------------------
corvus-migratorius@proton.me

View File

@@ -0,0 +1,4 @@
---
nginx_docker_project_path: "/root/nginx-docker"
nginx_docker_image_tag: "alpine3.19"
nginx_docker_custom_volumes: []

View File

@@ -0,0 +1 @@
---

View File

@@ -0,0 +1,17 @@
---
galaxy_info:
role_name: "nginx_docker"
namespace: genlab
author: "Alexander Gorelyshev"
company: "Genlab, LLC"
description: ""
license: "MIT"
min_ansible_version: "2.1"
platforms:
- name: "Ubuntu"
versions: ["jammy", "noble"]
galaxy_tags: []
dependencies: []

View File

@@ -0,0 +1,10 @@
---
- name: Converge
hosts: all
roles:
- role: genlab.common.docker_ubuntu
- role: genlab.common.nginx_docker
site_config: "nginx/test.conf"
nginx_docker_custom_volumes:
- {from: /mnt, to: /mnt, mode: ro}

View File

@@ -0,0 +1,27 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: ubuntu
image: geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2204}-ansible:latest
pre_build_image: true
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
cgroupns_mode: host
privileged: true
provisioner:
name: ansible
verifier:
name: ansible
lint: |
set -e
yamllint .
ansible-lint .

View File

@@ -0,0 +1,19 @@
server {
listen 80;
# listen [::]:80;
server_name localhost;
access_log /var/log/nginx/host.access.log main;
location /health/startup {
add_header Content-Type text/plain;
return 200 'healthy';
}
## Redirect server error pages to the static page /50x.html
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root /usr/share/nginx/html;
# }
}

View File

@@ -0,0 +1 @@
this is a test

View File

@@ -0,0 +1,23 @@
---
- name: Verify
hosts: all
gather_facts: false
any_errors_fatal: true
pre_tasks:
- name: "Copy test file"
ansible.builtin.copy:
src: ./test.txt
dest: /mnt/test.txt
owner: root
group: root
mode: "0660"
tasks:
- name: "Verify custom directory was mounted inside container successfully"
register: nginx_docker_this
failed_when: nginx_docker_this.stdout != 'this is a test'
changed_when: false
community.docker.docker_container_exec:
container: nginx
command: cat /mnt/test.txt

View File

@@ -0,0 +1,8 @@
---
- name: Converge
hosts: all
roles:
- role: genlab.common.docker_ubuntu
- role: genlab.common.nginx_docker
site_config: "nginx/test.conf"

View File

@@ -0,0 +1,27 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: ubuntu
image: geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2204}-ansible:latest
pre_build_image: true
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
cgroupns_mode: host
privileged: true
provisioner:
name: ansible
verifier:
name: ansible
lint: |
set -e
yamllint .
ansible-lint .

View File

@@ -0,0 +1,19 @@
server {
listen 80;
# listen [::]:80;
server_name localhost;
access_log /var/log/nginx/host.access.log main;
location /health/startup {
add_header Content-Type text/plain;
return 200 'healthy';
}
## Redirect server error pages to the static page /50x.html
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root /usr/share/nginx/html;
# }
}

View File

@@ -0,0 +1,15 @@
---
- name: Verify
hosts: all
gather_facts: false
any_errors_fatal: true
tasks:
# kics-scan ignore-block
- name: "Make an HTTP query"
register: nginx_docker_this
failed_when: nginx_docker_this is failed or 'healthy' not in nginx_docker_this.content
ansible.builtin.uri:
url: "http://localhost:80/health/startup"
method: "GET"
return_content: true

View File

@@ -0,0 +1,39 @@
---
- name: "Create project directory"
ansible.builtin.file:
state: directory
path: "{{ nginx_docker_project_path }}/conf.d/"
owner: root
group: root
mode: "0660"
- name: "Template project stack"
ansible.builtin.template:
src: "docker-compose.yml.j2"
dest: "{{ nginx_docker_project_path }}/docker-compose.yml"
owner: root
group: root
mode: "0660"
- name: "Push Nginx configuration file"
ansible.builtin.copy:
src: "{{ site_config }}"
dest: "{{ nginx_docker_project_path }}/conf.d/{{ site_config | basename }}"
owner: root
group: www-data
mode: "0660"
- name: "Build and start the Compose stack"
community.docker.docker_compose_v2:
tls_hostname: localhost
project_src: "{{ nginx_docker_project_path }}"
files:
- "docker-compose.yml"
build: always
state: present
- name: "Reload Nginx configuration"
changed_when: false
community.docker.docker_container_exec:
container: nginx
command: "nginx -s reload"

View File

@@ -0,0 +1,15 @@
---
services:
nginx:
image: nginx:{{ nginx_docker_image_tag }}
container_name: nginx
hostname: nginx
restart: unless-stopped
network_mode: host
volumes:
- "{{ nginx_docker_project_path }}/conf.d/:/etc/nginx/conf.d/:ro"
- "/var/log/nginx/:/var/log/nginx/:rw"
{% for v in nginx_docker_custom_volumes %}
- {{ v.from}}:{{v.to}}:{{v.mode | default('rw')}}
{% endfor %}

View File

@@ -0,0 +1 @@
---