Skip to content

Commit

Permalink
Add dnsmasq role (#1750)
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Berendt <[email protected]>
  • Loading branch information
berendt authored Feb 5, 2025
1 parent a183877 commit ebd2804
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .zuul.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@
- '^roles\/dnsdist\/.*$'
- '^molecule\/delegated\/tests\/dnsdist.*$'

- job:
name: ansible-collection-services-molecule-dnsmasq
parent: abstract-ansible-collection-services-molecule
vars:
ansible_role: dnsmasq
files:
- '^roles\/dnsmasq\/.*$'
- '^molecule\/delegated\/tests\/dnsmasq.*$'

- job:
name: ansible-collection-services-molecule-docker
parent: abstract-ansible-collection-services-molecule
Expand Down Expand Up @@ -424,6 +433,7 @@
- ansible-collection-services-molecule-clamav
- ansible-collection-services-molecule-containerd
- ansible-collection-services-molecule-dnsdist
- ansible-collection-services-molecule-dnsmasq
- ansible-collection-services-molecule-docker
- ansible-collection-services-molecule-fail2ban
- ansible-collection-services-molecule-falco
Expand Down Expand Up @@ -468,6 +478,7 @@
- ansible-collection-services-molecule-clamav
- ansible-collection-services-molecule-containerd
- ansible-collection-services-molecule-dnsdist
- ansible-collection-services-molecule-dnsmasq
- ansible-collection-services-molecule-docker
- ansible-collection-services-molecule-fail2ban
- ansible-collection-services-molecule-falco
Expand Down
15 changes: 15 additions & 0 deletions molecule/delegated/prepare/dnsmasq.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- name: Ensure docker
ansible.builtin.include_role:
name: docker
vars:
docker_user: zuul
docker_group: zuul
docker_python_install: false

- name: Ensure docker-compose
ansible.builtin.include_role:
name: osism.commons.docker_compose
vars:
docker_compose_service_user: zuul
docker_compose_service_group: zuul
25 changes: 25 additions & 0 deletions molecule/delegated/tests/dnsmasq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from .util.util import get_ansible, get_variable

testinfra_runner, testinfra_hosts = get_ansible()


def test_dnsmasq_directories(host):
directories = [
get_variable(host, "dnsmasq_docker_compose_directory"),
get_variable(host, "dnsmasq_configuration_directory"),
]

for d in directories:
f = host.file(d)
assert f.exists
assert f.is_directory
assert f.mode == 0o750
assert f.user == get_variable(host, "operator_user")
assert f.group == get_variable(host, "operator_group")


def test_dnsmasq_service(host):
service = host.service(get_variable(host, "dnsmasq_service_name"))

assert service.is_running
assert service.is_enabled
3 changes: 3 additions & 0 deletions molecule/delegated/vars/dnsmasq.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
operator_user: zuul
operator_group: zuul
1 change: 1 addition & 0 deletions molecule/delegated/verify/dnsmasq.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
Empty file added roles/dnsmasq/README.md
Empty file.
26 changes: 26 additions & 0 deletions roles/dnsmasq/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
##########################
# operator

operator_user: dragon
operator_group: "{{ operator_user }}"

##########################
# dnsmasq

dnsmasq_configuration_directory: /opt/dnsmasq/configuration
dnsmasq_docker_compose_directory: /opt/dnsmasq

dnsmasq_service_name: "docker-compose@dnsmasq"

dnsmasq_container_name: dnsmasq

dnsmasq_interface: "{{ ansible_default_ipv4.interface }}"
dnsmasq_dhcp_range: "192.168.42.100,192.168.42.200,3h"
dnsmasq_dhcp_hosts:
- aa:bb:cc:dd:ee:ff,testbed-manager,192.168.42.10

dnsmasq_docker_registry: quay.io
# renovate: datasource=docker depName=quay.io/osism/dnsmasq-osism
dnsmasq_tag: '2.90'
dnsmasq_image: "{{ dnsmasq_docker_registry }}/osism/dnsmasq-osism:{{ dnsmasq_tag }}"
10 changes: 10 additions & 0 deletions roles/dnsmasq/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Restart dnsmasq service
become: true
ansible.builtin.service:
name: "{{ dnsmasq_service_name }}"
state: restarted
register: result
until: result["status"]["ActiveState"] == "active"
retries: 10
delay: 20
22 changes: 22 additions & 0 deletions roles/dnsmasq/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
galaxy_info:
author: Christian Berendt
description: Role osism.services.dnsmasq
company: OSISM GmbH
license: Apache License 2.0
min_ansible_version: 2.16.0
platforms:
- name: Ubuntu
versions:
- jammy
- noble
- name: Debian
versions:
- bookworm
- name: EL
versions:
- "9"
galaxy_tags:
- osism
- system
dependencies: []
22 changes: 22 additions & 0 deletions roles/dnsmasq/tasks/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
- name: Create required directories
become: true
ansible.builtin.file:
path: "{{ item }}"
state: directory
owner: "{{ operator_user }}"
group: "{{ operator_group }}"
mode: 0750
loop:
- "{{ dnsmasq_docker_compose_directory }}"
- "{{ dnsmasq_configuration_directory }}"

- name: Copy configuration files
ansible.builtin.template:
src: "{{ item }}.j2"
dest: "{{ dnsmasq_configuration_directory }}/{{ item }}"
mode: 0644
owner: "{{ operator_user }}"
group: "{{ operator_group }}"
loop:
- dnsmasq.conf
8 changes: 8 additions & 0 deletions roles/dnsmasq/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Include config tasks
ansible.builtin.include_tasks: config.yml
tags: config

- name: Include service tasks
ansible.builtin.include_tasks: service.yml
tags: service
20 changes: 20 additions & 0 deletions roles/dnsmasq/tasks/service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
- name: Copy docker-compose.yml file
ansible.builtin.template:
src: docker-compose.yml.j2
dest: "{{ dnsmasq_docker_compose_directory }}/docker-compose.yml"
owner: "{{ operator_user }}"
group: "{{ operator_group }}"
mode: 0640
notify: Restart dnsmasq service

- name: Manage dnsmasq service
become: true
ansible.builtin.service:
name: "{{ dnsmasq_service_name }}"
state: started
enabled: true
register: result
until: result["status"]["ActiveState"] == "active"
retries: 10
delay: 20
6 changes: 6 additions & 0 deletions roles/dnsmasq/templates/dnsmasq.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
port=0
interface={{ dnsmasq_interface }}
dhcp-range={{ dnsmasq_dhcp_range }}
{% for dhcp_host in dnsmasq_dhcp_hosts %}
dhcp-host={{ dhcp_host }}
{% endfor %}
12 changes: 12 additions & 0 deletions roles/dnsmasq/templates/docker-compose.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
services:
dnsmasq:
image: "{{ dnsmasq_image }}"
container_name: "{{ dnsmasq_container_name }}"
restart: unless-stopped
network_mode: host
cap_add:
- NET_ADMIN
volumes:
- "/etc/hosts:/etc/hosts:ro"
- "{{ dnsmasq_configuration_directory }}/dnsmasq.conf:/etc/dnsmasq.conf:ro"

0 comments on commit ebd2804

Please sign in to comment.