Skip to content

Commit 418e903

Browse files
committed
feat: use template for configuration
1 parent 99ff18d commit 418e903

File tree

4 files changed

+63
-14
lines changed

4 files changed

+63
-14
lines changed

defaults/main.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
dhcpcd_static_ip_configuration: |
2-
# interface eth0
3-
# static ip_address=192.168.0.10/24
4-
# static ip6_address=fd51:42f8:caae:d92e::ff/64
5-
# static routers=192.168.0.1
6-
# static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1
1+
dhcpcd_static_ipv4_configuration_enabled: false
2+
dhcpcd_static_ipv4_address: 192.168.0.10/24
3+
dhcpcd_static_ipv4_gateway: 192.168.0.1
4+
dhcpcd_static_ipv4_domain_name_server: 192.168.0.1

molecule/default/converge.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
- name: Converge
22
hosts: all
33
vars:
4-
dhcpcd_static_ip_configuration: |
5-
interface eth0
6-
static ip_address={{ ansible_default_ipv4.address }}/16
7-
static routers={{ ansible_default_ipv4.gateway }}
8-
static domain_name_servers={{ ansible_dns.nameservers[0] }}
4+
dhcpcd_static_ipv4_configuration_enabled: true
5+
dhcpcd_static_ipv4_address: "{{ ansible_default_ipv4.address }}/16"
6+
dhcpcd_static_ipv4_gateway: "{{ ansible_default_ipv4.gateway }}"
7+
dhcpcd_static_ipv4_domain_name_server: "{{ ansible_dns.nameservers[0] }}"
98
roles:
109
- role: ansible-raspberry-dhcpcd
1110
tags: dhcpcd

tasks/main.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
state: present
55
update_cache: true
66

7-
- name: Configure static IP address
8-
ansible.builtin.blockinfile:
7+
- name: Create service configuration
8+
ansible.builtin.template:
9+
src: dhcpcd.conf.j2
910
dest: /etc/dhcpcd.conf
10-
block: "{{ dhcpcd_static_ip_configuration }}"
11+
owner: root
12+
group: root
13+
mode: 0644
1114
notify: Restart dhcpcd service
1215

1316
- name: Start and enable service

templates/dhcpcd.conf.j2

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# A sample configuration for dhcpcd.
2+
# See dhcpcd.conf(5) for details.
3+
4+
# Allow users of this group to interact with dhcpcd via the control socket.
5+
#controlgroup wheel
6+
7+
# Inform the DHCP server of our hostname for DDNS.
8+
hostname
9+
10+
# Use the hardware address of the interface for the Client ID.
11+
clientid
12+
# or
13+
# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
14+
# Some non-RFC compliant DHCP servers do not reply with this set.
15+
# In this case, comment out duid and enable clientid above.
16+
#duid
17+
18+
# Persist interface configuration when dhcpcd exits.
19+
persistent
20+
21+
# Rapid commit support.
22+
# Safe to enable by default because it requires the equivalent option set
23+
# on the server to actually work.
24+
option rapid_commit
25+
26+
# A list of options to request from the DHCP server.
27+
option domain_name_servers, domain_name, domain_search, host_name
28+
option classless_static_routes
29+
# Respect the network MTU. This is applied to DHCP routes.
30+
option interface_mtu
31+
32+
# Most distributions have NTP support.
33+
#option ntp_servers
34+
35+
# A ServerID is required by RFC2131.
36+
require dhcp_server_identifier
37+
38+
# Generate SLAAC address using the Hardware Address of the interface
39+
#slaac hwaddr
40+
# OR generate Stable Private IPv6 Addresses based from the DUID
41+
slaac private
42+
{% if dhcpcd_static_ipv4_configuration_enabled %}
43+
44+
# Static IPv4 configuration
45+
interface eth0
46+
static ip_address={{ dhcpcd_static_ipv4_address }}
47+
static routers={{ dhcpcd_static_ipv4_gateway }}
48+
static domain_name_servers={{ dhcpcd_static_ipv4_domain_name_server }}
49+
{% endif %}

0 commit comments

Comments
 (0)