Skip to content

Commit 88f79f8

Browse files
cjeanneropenshift-merge-bot[bot]
authored andcommitted
Refine a bit dnsmasq role
An assertion block was missing a condition, leading to issue when we wanted to just remove a network from the configuration. In the network configuration template, the test to know more about ipv4 and ipv6 ranges was a bit too loose, leading to issues under certain conditions. We faced the same issue in the host management, leading to potentially wrong DHCP configuration. In order to ease debugging, we now generate a fact, display its content, and use that content to inject the host data in the configuration. During a re-run, the service is already stopped/disabled, and the unit file is removed. Ansible will then fail, since systemd can't find the service to stop.
1 parent 7570d14 commit 88f79f8

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

roles/dnsmasq/tasks/configure.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@
7474
become: true
7575
when:
7676
- _act == 'cleanup'
77+
register: _dnsmasq_stop
7778
ansible.builtin.systemd_service:
7879
name: cifmw-dnsmasq.service
7980
state: stopped
8081
enabled: false
82+
failed_when:
83+
- _dnsmasq_stop.msg is defined and
84+
_dnsmasq_stop.msg is not match('Could not find the requested service cifmw-dnsmasq.service')
8185

8286
- name: Remove unit file
8387
become: true

roles/dnsmasq/tasks/manage_host.yml

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,36 @@
3939
- _network_exists.stat.exists
4040
quiet: true
4141
msg: >-
42-
You must create the network first, calling
42+
You must create {{ cifmw_dnsmasq_host_network }} network first, calling
4343
dnsmasq/manage_network.yml task.
4444
45-
- name: Manage host entry
46-
become: true
47-
notify: Restart dnsmasq
48-
ansible.builtin.blockinfile:
49-
block: |
50-
{% if cifmw_dnsmasq_host_ipv4 is defined -%}
51-
{% set data = [cifmw_dnsmasq_host_mac] -%}
52-
{% set _ = data.append(cifmw_dnsmasq_host_ipv4) -%}
45+
- name: Compute entry
46+
ansible.builtin.set_fact:
47+
_host_entry: |
48+
{% if cifmw_dnsmasq_host_ipv4 is defined and cifmw_dnsmasq_host_ipv4 | length > 0 -%}
49+
{% set data = [cifmw_dnsmasq_host_mac] -%}
50+
{% set _ = data.append(cifmw_dnsmasq_host_ipv4) -%}
5351
{% set _ = data.append(cifmw_dnsmasq_host_name) if
54-
cifmw_dnsmasq_host_name is defined -%}
52+
cifmw_dnsmasq_host_name is defined and cifmw_dnsmasq_host_name | length > 0 -%}
5553
dhcp-host={{ data | join(',') }}
5654
{% endif -%}
57-
{% if cifmw_dnsmasq_host_ipv6 is defined -%}
58-
{% set data = [cifmw_dnsmasq_host_mac] -%}
59-
{% set _ = data.append('['~cifmw_dnsmasq_host_ipv6~']') -%}
55+
{% if cifmw_dnsmasq_host_ipv6 is defined and cifmw_dnsmasq_host_ipv6 | length > 0 -%}
56+
{% set data = [cifmw_dnsmasq_host_mac] -%}
57+
{% set _ = data.append('['~cifmw_dnsmasq_host_ipv6~']') -%}
6058
{% set _ = data.append(cifmw_dnsmasq_host_name) if
61-
cifmw_dnsmasq_host_name is defined -%}
59+
cifmw_dnsmasq_host_name is defined and cifmw_dnsmasq_host_name | length > 0 -%}
6260
dhcp-host={{ data | join(',') }}
6361
{% endif -%}
62+
63+
- name: Debug _host_entry
64+
ansible.builtin.debug:
65+
var: _host_entry
66+
67+
- name: Manage host entry
68+
become: true
69+
notify: Restart dnsmasq
70+
ansible.builtin.blockinfile:
71+
block: "{{ _host_entry }}"
6472
dest: "{{ cifmw_dnsmasq_basedir }}/{{ cifmw_dnsmasq_host_network }}-hosts.conf"
6573
create: true
6674
mode: "0644"

roles/dnsmasq/tasks/manage_network.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
- cifmw_dnsmasq_network_state in ['present', 'absent']
2323

2424
- name: Assert mandatory parameters for new network
25+
when:
26+
- cifmw_dnsmasq_network_state == 'present'
2527
ansible.builtin.assert:
2628
that:
2729
- cifmw_dnsmasq_network_definition is defined

roles/dnsmasq/templates/network.conf.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Managed by ci-framework/dnsmasq
22
{% for range in cifmw_dnsmasq_network_definition['ranges'] %}
3-
{% if range.start_v4 is defined %}
3+
{% if range.start_v4 is defined and range.start_v4 | length > 0 %}
44
dhcp-range=set:{{ range.label }},{{ range.start_v4 }},static,{{ (range.start_v4 + "/" + range.prefix_length_v4 | default(24) | string) | ansible.utils.ipaddr('netmask') }},{{ range.ttl | default('1h') }}
55
{% endif %}
6-
{% if range.start_v6 is defined %}
6+
{% if range.start_v6 is defined and range.start_v6 | length > 0 %}
77
dhcp-range=set:{{ range.label }},{{ range.start_v6 }},static,{{ range.prefix_length_v6 | default('64') }},{{ range.ttl | default('1h') }}
88
{% endif %}
99
{% for option in range['options'] | default([]) %}

0 commit comments

Comments
 (0)