Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Linux support #2

Merged
merged 4 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
skip_list:
- 'risky-shell-pipe'
- 'role-name'

warn_list:
- package-latest
- unnamed-task
- command-instead-of-shell
- no-handler
47 changes: 47 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
name: Test
run-name: Run molecule tests on the role
on:
push:
workflow_dispatch:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install dependencies.
run: pip install yamllint ansible-lint ansible

- name: Run ansible-lint
run: "ansible-lint"

molecule:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install dependencies.
run: |
python -m pip install --upgrade pip
pip install ansible docker molecule molecule-plugins
sudo apt install rsync

- name: Install Galaxy dependencies.
run: ansible-galaxy collection install community.docker community.mysql

- name: Run molecule
run: "molecule test"
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
repos:
- repo: https://github.com/ansible/ansible-lint.git
rev: v6.17.2
hooks:
- id: ansible-lint
33 changes: 33 additions & 0 deletions .yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
# Based on ansible-lint config
extends: default

rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
colons:
max-spaces-after: -1
level: error
commas:
max-spaces-after: -1
level: error
comments: disable
comments-indentation: disable
document-start: disable
empty-lines:
max: 3
level: error
hyphens:
level: error
indentation: disable
key-duplicates: enable
line-length: disable
new-line-at-end-of-file: disable
new-lines:
type: unix
trailing-spaces: disable
truthy: disable
15 changes: 13 additions & 2 deletions defaults/main.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
---
varnish:
service_name: >-
{%- if ansible_system == 'Linux' -%}
varnish
{%- else -%}
varnishd
{%- endif -%}
prefix:
config: /usr/local/etc/varnish
config: >-
{%- if ansible_system == 'Linux' -%}
/etc/varnish
{%- else -%}
/usr/local/etc/varnish
{%- endif -%}
bind_addr: "localhost:8080"
#logformat: '%h "%{X-Forwarded-For}i" %u %t "%r" %s %b "%{Referer}i" "%{User-agent}i" "%{Varnish:time_firstbyte}x" %{Varnish:handling}x "%{Content-Type}o" "%{Cookie}i"'
logformat: >
{"time": %{"%FT%T%z"}t, "remote_addr": "%h","remote_user": "%u","x_forwarded_for": "%{X-Forwarded-For}i","x_real_ip": "%{X-Real-IP}i","x_request_id": "%{X-Request-ID}i","hit_miss": "%{Varnish:hitmiss}x","body_bytes_sent": "%b","request_time": "%{Varnish:time_firstbyte}x","status": "%s","content_type": "%{Content-Type}o","request": "%r","host": "%{host}i","request_method": "%m","time_first_byte": "%{Varnish:time_firstbyte}x","handling": "%{Varnish:handling}x","http_referrer": "%{Referrer}i","http_user_agent": "%{User-agent}i"}
storage: malloc,512M
storage: malloc,512M
12 changes: 6 additions & 6 deletions handlers/main.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
---
- name: Start Varnish
service:
name: varnishd
ansible.builtin.service:
name: "{{ varnish.service_name }}"
state: started
register: varnishd_service_result
notify: Start Varnishncsa

- name: Restart Varnish
service:
name: varnishd
ansible.builtin.service:
name: "{{ varnish.service_name }}"
state: restarted
when: not (varnishd_service_result is defined and varnishd_service_result.changed)
notify: Restart Varnishncsa

- name: Start Varnishncsa
service:
ansible.builtin.service:
name: varnishncsa
state: started
register: varnishncsa_service_result

- name: Restart Varnishncsa
service:
ansible.builtin.service:
name: varnishncsa
state: restarted
when: not (varnishncsa_service_result is defined and varnishncsa_service_result.changed)
10 changes: 10 additions & 0 deletions meta/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
dependencies: []

galaxy_info:
author: "Punkt.de"
license: "MIT"
description: "Varnish role for Proserver"
role_name: "proserver_varnish"
namespace: "punktde"
min_ansible_version: "2.15"
7 changes: 7 additions & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Converge
hosts: all
tasks:
- name: "Include ansible-proserver-varnish"
ansible.builtin.include_role:
name: "ansible-proserver-varnish"
27 changes: 27 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance
image: geerlingguy/docker-ubuntu2204-ansible
command: /lib/systemd/systemd
pre_build_image: true
privileged: true
cgroupns_mode: host
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
provisioner:
name: ansible
playbooks:
converge: ${MOLECULE_PLAYBOOK:-converge.yml}
verifier:
name: ansible
scenario:
name: default
test_sequence:
- destroy
- create
- converge
- verify
26 changes: 26 additions & 0 deletions molecule/default/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
# This is an example playbook to execute Ansible tests.

- name: Verify
hosts: all
gather_facts: false
tasks:
- name: Check Varnish status
register: varnish_systemd
changed_when: no
ansible.builtin.command:
cmd: systemctl status varnish

- name: Check Varnishncsa status
register: varnishncsa_systemd
changed_when: no
ansible.builtin.command:
cmd: systemctl status varnishncsa

- name: Debug
ansible.builtin.debug:
var: varnish_systemd.stdout

- name: Debug
ansible.builtin.debug:
var: varnishncsa_systemd.stdout
7 changes: 7 additions & 0 deletions tasks/install.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- name: Install varnish
notify:
- Start Varnish
- Start Varnishncsa
ansible.builtin.apt:
name: varnish
update_cache: yes
9 changes: 8 additions & 1 deletion tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
---
- import_tasks: service.yaml
- ansible.builtin.include_tasks: install.yaml

Check warning on line 2 in tasks/main.yaml

View workflow job for this annotation

GitHub Actions / lint

name[missing]

All tasks should be named.
when: "ansible_system == 'Linux'"

- ansible.builtin.include_tasks: service-Debian.yaml

Check warning on line 5 in tasks/main.yaml

View workflow job for this annotation

GitHub Actions / lint

name[missing]

All tasks should be named.
when: "ansible_os_family == 'Debian'"

- ansible.builtin.include_tasks: service-FreeBSD.yaml

Check warning on line 8 in tasks/main.yaml

View workflow job for this annotation

GitHub Actions / lint

name[missing]

All tasks should be named.
when: "ansible_os_family == 'FreeBSD'"
48 changes: 48 additions & 0 deletions tasks/service-Debian.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
- name: Create systemd override folders
ansible.builtin.file:
path: /etc/systemd/system/{{ item }}.service.d
state: directory
owner: root
mode: "0755"
loop:
- varnish
- varnishncsa

- name: Configure Varnish service override
notify:
- Restart Varnish
ansible.builtin.template:
dest: /etc/systemd/system/varnish.service.d/override.conf
src: varnish.service.d/override.conf
mode: "0644"
owner: root

- name: Template a Varnishncsa wrapper script
notify:
- Restart Varnishncsa
ansible.builtin.copy:
content: |
#! /bin/sh
/usr/bin/varnishncsa -a -w /var/log/varnish/varnishncsa.log -D -P /run/varnishncsa/varnishncsa.pid -F {{ varnish.logformat | quote }}
dest: "/usr/local/bin/varnishncsa_systemd_wrapper"
mode: "0755"
owner: "root"

- name: Configure Varnishncsa service override
notify:
- Restart Varnishncsa
ansible.builtin.template:
dest: /etc/systemd/system/varnishncsa.service.d/override.conf
src: varnishncsa.service.d/override.conf
mode: "0644"
owner: root

- name: Enable services
loop:
- "{{ varnish.service_name }}"
- varnishncsa
ansible.builtin.systemd:
daemon_reload: yes
name: "{{ item }}"
enabled: yes
31 changes: 31 additions & 0 deletions tasks/service-FreeBSD.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
- name: Configure Varnish service
ansible.builtin.lineinfile:
path: "{{ item.rc_conf }}"
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
create: yes
mode: "0755"
owner: "root"
with_items:
- rc_conf: /usr/local/etc/rc.conf.d/varnishd
regexp: "^varnishd_listen="
line: >
varnishd_listen={{ varnish.bind_addr | trim | quote }}
- rc_conf: /usr/local/etc/rc.conf.d/varnishd
regexp: "^varnishd_storage="
line: >
varnishd_storage="{{ varnish.storage | trim | quote }}"
- rc_conf: /usr/local/etc/rc.conf.d/varnishncsa
regexp: "^varnishncsa_logformat="
line: >
varnishncsa_logformat={{ varnish.logformat | trim | replace('"', '\"') | quote }}
notify:
- Restart Varnish

- name: Enable service
notify:
- Start Varnish
ansible.builtin.service:
name: "{{ varnish.service_name }}"
enabled: yes
32 changes: 0 additions & 32 deletions tasks/service.yaml

This file was deleted.

10 changes: 10 additions & 0 deletions templates/varnish.service.d/override.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd \
-j unix,user=vcache \
-F \
-a :6081 \
-T {{ varnish.bind_addr | trim }} \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s {{ varnish.storage | trim }}
3 changes: 3 additions & 0 deletions templates/varnishncsa.service.d/override.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Service]
ExecStart=
ExecStart=/usr/local/bin/varnishncsa_systemd_wrapper
Loading