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

dhcp-server: Enable support for dhcpd6 #656

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 25 additions & 9 deletions roles/dhcp-server/README.rst

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions roles/dhcp-server/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
enable_ipv6: false
72 changes: 72 additions & 0 deletions roles/dhcp-server/tasks/ipv4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
- name: Install/update packages
yum:
name: dhcp
state: latest
register: dhcp_yum_transaction
tags: packages

- name: Check for firewalld
command: firewall-cmd --state
register: firewalld_state
ignore_errors: true

- name: Check for iptables
command: systemctl status iptables
register: iptables_state
ignore_errors: true

- name: Make sure firewalld is running
service:
name: firewalld
state: started
enabled: yes
when:
- not ansible_check_mode
- iptables_state.rc != 0
- not (firewalld_state.msg is defined and "'No such file or directory' in firewalld_state.msg")

- name: Configure firewalld
firewalld:
service: dhcp
state: enabled
permanent: true
immediate: yes
when:
- not ansible_check_mode
- iptables_state.rc != 0
- not (firewalld_state.msg is defined and "'No such file or directory' in firewalld_state.msg")

- name: Write global dhcpd.conf
template:
src: dhcpd.conf.j2
dest: /etc/dhcp/dhcpd.conf
backup: yes
register: dhcp_global_config

# NOTE: This will write the IPv6 configs too just to avoid iterating over `dhcp_subnets: {}` twice.
# ipv6.yml will configure and restart the dhcpd6 service
- name: Write each subnet config
template:
src: dhcpd.subnet.conf.j2
dest: "/etc/dhcp/dhcpd.{{ item }}.conf"
backup: yes
with_items: "{{ dhcp_subnets }}"
register: dhcp_subnet_config

- name: Test new config
command: dhcpd -t -cf /etc/dhcp/dhcpd.conf
register: dhcpd_config_test_result
when:
- not ansible_check_mode
- (dhcp_global_config is changed or dhcp_subnet_config is changed)

- name: Restart dhcpd
service:
name: dhcpd
state: restarted
when:
- not ansible_check_mode
- (dhcp_global_config is changed or dhcp_subnet_config is changed)
- dhcpd_config_test_result is defined
- dhcpd_config_test_result.rc == 0
26 changes: 26 additions & 0 deletions roles/dhcp-server/tasks/ipv6.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
- name: Write global dhcpd6.conf
template:
src: dhcpd6.conf.j2
dest: /etc/dhcp/dhcpd6.conf
backup: yes
register: dhcp6_global_config

- name: Test new config
command: dhcpd -t -6 -cf /etc/dhcp/dhcpd6.conf
register: dhcpd6_config_test_result
when:
- not ansible_check_mode
- (dhcp6_global_config is changed or dhcp_subnet_config is changed)

- name: Restart dhcpd6
service:
name: dhcpd6
state: restarted
when:
- not ansible_check_mode
- dhcp6_global_config is defined
- dhcp_subnet_config is defined
- (dhcp6_global_config is changed or dhcp_subnet_config is changed)
- dhcpd6_config_test_result is defined
- dhcpd6_config_test_result.rc == 0
67 changes: 5 additions & 62 deletions roles/dhcp-server/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,64 +1,7 @@
---
- name: Install/update packages
yum:
name: dhcp
state: latest
register: dhcp_yum_transaction
- name: Run IPv4 DHCP server tasks
include_tasks: ipv4.yml

- name: Check for firewalld
command: firewall-cmd --state
register: firewalld_state
ignore_errors: true

- name: Check for iptables
command: systemctl status iptables
register: iptables_state
ignore_errors: true

- name: Make sure firewalld is running
service:
name: firewalld
state: started
enabled: yes
when:
- iptables_state.rc != 0
- not (firewalld_state.msg is defined and "'No such file or directory' in firewalld_state.msg")

- name: Configure firewalld
firewalld:
service: dhcp
state: enabled
permanent: true
immediate: yes
when:
- iptables_state.rc != 0
- not (firewalld_state.msg is defined and "'No such file or directory' in firewalld_state.msg")

- name: Write global dhcpd.conf
template:
src: dhcpd.conf.j2
dest: /etc/dhcp/dhcpd.conf
backup: yes
register: dhcp_global_config

- name: Write each subnet config
template:
src: dhcpd.subnet.conf.j2
dest: "/etc/dhcp/dhcpd.{{ item }}.conf"
backup: yes
with_items: "{{ dhcp_subnets }}"
register: dhcp_subnet_config

- name: Test new config
command: dhcpd -t -cf /etc/dhcp/dhcpd.conf
register: dhcpd_config_test_result
when: dhcp_global_config is changed or dhcp_subnet_config is changed

- name: Restart dhcpd
service:
name: dhcpd
state: restarted
when:
- (dhcp_global_config is changed or dhcp_subnet_config is changed)
- dhcpd_config_test_result is defined
- dhcpd_config_test_result.rc == 0
- name: Run IPv6 DHCP server tasks
include_tasks: ipv6.yml
when: enable_ipv6|bool
2 changes: 2 additions & 0 deletions roles/dhcp-server/templates/dhcpd.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
{% endfor %}

{% for key, value in dhcp_subnets.items() %}
{% if value.version == "4" %}
include "/etc/dhcp/dhcpd.{{ key }}.conf";
{% endif %}
{% endfor %}
14 changes: 9 additions & 5 deletions roles/dhcp-server/templates/dhcpd.subnet.conf.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{% for subnet, subnet_item in dhcp_subnets.items() %}
{% if subnet == item %}
{% if subnet_item.version == "6" %}{% set is_six = "6" %}
subnet6 {{ subnet_item.cidr }} {
{% else %}{% set is_six = "" %}
subnet {{ subnet_item.cidr | ipaddr('network') }} netmask {{ subnet_item.cidr | ipaddr('netmask') }} {
{% endif %}
{% if subnet_item.domain_name is defined -%}
option domain-name "{{ subnet_item.domain_name }}";
{% endif -%}
Expand All @@ -10,13 +14,13 @@ subnet {{ subnet_item.cidr | ipaddr('network') }} netmask {{ subnet_item.cidr |
{% if subnet_item.domain_name_servers is defined -%}
option domain-name-servers {{ subnet_item.domain_name_servers|join(', ') }};
{% endif -%}
{% if subnet_item.routers is defined -%}
{% if subnet_item.routers is defined and not subnet_item.version == "6" -%}
option routers {{ subnet_item.routers }};
{% endif -%}
{% if subnet_item.next_server is defined -%}
{% if subnet_item.next_server is defined and not subnet_item.version == "6" -%}
next-server {{ subnet_item.next_server }};
{% endif -%}
{% if subnet_item.filename is defined -%}
{% if subnet_item.filename is defined and not subnet_item.version == "6" -%}
filename "{{ subnet_item.filename }}";
{% endif %}

Expand Down Expand Up @@ -54,7 +58,7 @@ subnet {{ subnet_item.cidr | ipaddr('network') }} netmask {{ subnet_item.cidr |
{%- endif -%}

{% for host in groups['all'] | sort | unique -%}
{% if hostvars[host][subnet_item.macvar] is defined -%}
{% if hostvars[host][subnet_item.macvar] is defined and hostvars[host][subnet_item.ipvar] is defined -%}
{% if hostvars[host][subnet_item.ipvar] | ipaddr(subnet_item.cidr) | ipaddr('bool') -%}
host {{ host.split('.')[0] }}-{{ subnet }} {
{% if hostvars[host]['dhcp_next_server'] is defined -%}
Expand All @@ -65,7 +69,7 @@ subnet {{ subnet_item.cidr | ipaddr('network') }} netmask {{ subnet_item.cidr |
option domain-name-servers {{ hostvars[host]['domain_name_servers']|join(', ') }};
{% endif -%}
hardware ethernet {{ hostvars[host][subnet_item.macvar] }};
fixed-address {{ hostvars[host][subnet_item.ipvar] }};
fixed-address{{ is_six }} {{ hostvars[host][subnet_item.ipvar] }};
{% if hostvars[host]['dhcp_option_hostname'] is defined and hostvars[host]['dhcp_option_hostname'] == true %}
option host-name "{{ host.split('.')[0] }}";
{% endif -%}
Expand Down
11 changes: 11 additions & 0 deletions roles/dhcp-server/templates/dhcpd6.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% for item in dhcp_global_options %}
{% for key, value in item.items() %}
{{ key }} {{ value }};
{% endfor %}
{% endfor %}

{% for key, value in dhcp_subnets.items() %}
{% if value.version == "6" %}
include "/etc/dhcp/dhcpd.{{ key }}.conf";
{% endif %}
{% endfor %}