add nginx role

This commit is contained in:
Sergey Malyuk
2025-12-16 13:49:01 +03:00
parent 7f35854d6c
commit 433f22dca0
20 changed files with 334 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
---
- name: Converge
hosts: all
post_tasks:
- name: "Create a test site root"
ansible.builtin.file:
path: /var/www/html
state: directory
owner: nginx
group: nginx
mode: "0755"
- name: "Push a test index file"
ansible.builtin.copy:
src: index.html
dest: /var/www/html
owner: nginx
group: nginx
mode: "0644"
roles:
# using default NGINX verison
- role: genlab.nginx
site:
confname: test-var
content: |
server {
listen 80;
server_name example.com www.example.com;
location / {
root /var/www/html;
index index.html;
}
}
dir_sites: '{{ lookup("env", "MOLECULE_PROJECT_DIRECTORY") }}/molecule/default/site-configs/'

View File

@@ -0,0 +1 @@
Hello from ansible-nginx!

View File

@@ -0,0 +1,27 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: ubuntu
image: geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2404}-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,9 @@
server {
listen 8080;
server_name test.com;
location / {
root /var/www/html;
index index.html;
}
}

View File

@@ -0,0 +1,73 @@
# kics-scan disable=2e8d4922-8362-4606-8c14-aa10466a1ce3
---
- name: Verify
hosts: all
gather_facts: false
any_errors_fatal: true
pre_tasks:
- name: "Include default vars"
ansible.builtin.include_vars:
dir: '{{ lookup("env", "MOLECULE_PROJECT_DIRECTORY") }}/defaults/'
extensions:
- 'yml'
tasks:
- name: "Verify NGINX version"
register: nginx_version_output
changed_when: false
failed_when: nginx_version_output.rc != 0
ansible.builtin.command: /usr/sbin/nginx -v
# version is displayed as e.g. 'nginx version: nginx/1.26.3'
- name: "Extract NGINX version"
ansible.builtin.set_fact:
nginx_version: "{{ nginx_version_output.stderr.split('/')[1] }}"
- name: "Check expected NGINX version"
ansible.builtin.assert:
that: nginx_version.startswith(nginx_version)
fail_msg: "Unexpected NGINX version found: '{{ nginx_version }}'"
- name: "Gather service facts"
ansible.builtin.service_facts:
- name: "Assert NGINX service is running"
ansible.builtin.assert:
that: ansible_facts.services['nginx.service'].state == 'running'
fail_msg: "NGINX service is not running."
- name: "Assert NGINX service is enabled"
ansible.builtin.assert:
that: ansible_facts.services['nginx.service'].status == 'enabled'
fail_msg: "NGINX service is not enabled."
- name: "Get an HTTP response from the test site (single site deployment from a variable)"
register: response
ansible.builtin.uri:
url: http://localhost:80
status_code: 200
return_content: true
- name: "Check the response correctness"
vars:
message: "{{ response.content | trim }}"
ansible.builtin.assert:
that: message == 'Hello from ansible-nginx!'
success_msg: "{{ message }}"
fail_msg: "Unexpected response from the test site: '{{ message }}'"
- name: "Get an HTTP response from the test site (site deployment from a dictionary)"
register: response
ansible.builtin.uri:
url: http://localhost:8080
status_code: 200
return_content: true
- name: "Check the response correctness"
vars:
message: "{{ response.content | trim }}"
ansible.builtin.assert:
that: message == 'Hello from ansible-nginx!'
success_msg: "{{ message }}"
fail_msg: "Unexpected response from the test site: '{{ message }}'"