Example 1: Configure netpland

Create a playbook

shell> cat lp.yml
- hosts: test_01
  become: true
  roles:
    - vbotka.linux_postinstall

Create the file host_vars/test_01/lp-systemd.yml (1). Enable the import of the tasks (2). Configure the file (5). The ownership and mode (6-8) use the default values lp_systemd_* and can be omitted. The attribute reload_service (9) is needed to reload or restart the service when the configuration file changes

 1shell> cat host_vars/test_01/lp-systemd.yml
 2lp_systemd: true
 3
 4lp_systemd_conf:
 5  /etc/systemd/networkd.conf:
 6     owner: "{{ lp_systemd_owner }}"
 7     group: "{{ lp_systemd_group }}"
 8     mode:  "{{ lp_systemd_mode }}"
 9     reload_service: systemd-networkd.service
10     conf:
11       - {section: Network, key: SpeedMeter, val: 'no'}
12       - {section: Network, key: SpeedMeterIntervalSec, val: 10sec}
13       - {section: DHCP, key: DUIDType, val: vendor}
14
15lp_systemd_unit:
16  - name: systemd-networkd
17    control:
18      enabled: true
19      state: started
20      restart_or_reload: restarted

Note

  • The service, which shall be reloaded or restarted when the configuration changes, reload_service (9) can be configured in lp_systemd_unit (16). The handler reload systemd conf needs to know if and how to reload or restart the service.

  • If the attribute reload_service is missing in lp_systemd_unit the handler reload systemd conf will crash.

  • If the reload_service is not configured in lp_systemd_unit the handler reload systemd conf will reload the service by default.

  • If no service needs to be reloaded or restarted set reload_service: noop (9).

See also

  • See the handler reload systemd conf.

Run the playbook with the tag -t lp_systemd_conf to limit the tasks and configure the files from the dictionary lp_systemd_conf

shell> ansible-playbook lp.yml -t lp_systemd_conf

TASK [vbotka.linux_postinstall : systemd: Configure systemd] *****************
changed: [test_01] => (item=/etc/systemd/networkd.conf Network SpeedMeter no)
changed: [test_01] => (item=/etc/systemd/networkd.conf Network SpeedMeterIntervalSec 10sec)
changed: [test_01] => (item=/etc/systemd/networkd.conf DHCP DUIDType vendor)

TASK [vbotka.linux_postinstall : systemd: Debug conf results] ****************
skipping: [test_01]

TASK [vbotka.linux_postinstall : systemd: Debug conf loop results] ***********
skipping: [test_01] => (item=/etc/systemd/networkd.conf)
skipping: [test_01] => (item=/etc/systemd/networkd.conf)
skipping: [test_01] => (item=/etc/systemd/networkd.conf)
skipping: [test_01]

RUNNING HANDLER [vbotka.linux_postinstall : reload systemd conf] *************
changed: [test_01] => (item=systemd-networkd.service)
changed: [test_01] => (item=systemd-networkd.service)
changed: [test_01] => (item=systemd-networkd.service)

PLAY RECAP *******************************************************************
test_01: ok=8 changed=2 unreachable=0 failed=0 skipped=7 rescued=0 ignored=0

Display the configuration file

test_01> cat /etc/systemd/networkd.conf
[Network]
SpeedMeter=no
SpeedMeterIntervalSec=10sec
[DHCP]
DUIDType=vendor