I want to configure systemd configuration files in a DRY way. I have this dictionary:
systemd_configurations:
journald:
SystemMaxUse: 100M
Storage: persistent
sleep:
AllowSuspend: no
AllowHybridSleep: no
AllowHibernation: no
resolved:
DNS: 1.1.1.1
FallbackDNS: 1.0.0.1
Domains: "~."
DNSOverTLS: opportunistic
system:
DumpCore: no
And I want to use a lineinfile task to apply the settings:
- name: Configure systemd
lineinfile:
path: /etc/systemd/{{ item.key }}.conf
regexp: "^#{{ item.value.key }}="
line: "{{ item.value.key + '=' + item.value.value }}"
loop: "{{ systemd_configurations }}"
So the task would for every .conf file separately apply all the settings defined for that config file. e.g. for journald.conf, it would apply SystemMaxUse=100M and Storage=persistent
A working solution is to modify the variable a bit, according to this structure:
systemd_configurations:
journald:
- SystemMaxUse=100M
- Storage=persistent
- ForwardToSyslog=yes
sleep:
- AllowSuspend=no
- AllowHybridSleep=no
- AllowHibernation=no
resolved:
- DNS=1.1.1.1
- FallbackDNS=1.0.0.1
- Domains=~.
- DNSOverTLS=opportunistic
system:
- DumpCore=no
And with the following task:
- name: Configure systemd
lineinfile:
path: /etc/systemd/{{ item.0.key }}.conf
regexp: "{{ item.1.split('=')[0] }}="
line: "{{ item.1 }}"
loop: "{{ systemd_configurations | dict2items | subelements('value') }}"
But I'd like to understand how to achieve it with the original variable structure.