Skip to content

Commit fc19dfb

Browse files
committed
fix: files and templates in nested directories are not placed correctly
Cause: If the user specified a file or template source within a nested directory, the role was using the whole path and not just the basename for the name of the destination file. Consequence: The role was trying to place files and templates in the same directory structure on the destination, which is not supported by systemd. Fix: Ensure the basename of the given file or template is used to construct the path to the destination. Result: The role works even when the source files and templates are in nested directories. NOTE: If you were relying on the old broken code to make deeply nested systemd and drop-in directories on the destination, and that somehow was working, it will no longer be working. Signed-off-by: Rich Megginson <[email protected]>
1 parent f2c6153 commit fc19dfb

File tree

7 files changed

+28
-23
lines changed

7 files changed

+28
-23
lines changed

examples/foo.service.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../tests/templates/foo.service.j2
1+
../tests/nested/dir/templates/foo.service.j2

tasks/manage_unit_files.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
- name: Manage files and templates
2121
when: __systemd_dict_list | length > 0
2222
vars:
23-
__unit: "{{ item.item | regex_replace('[.]j2$', '')
23+
__unit: "{{ item.item | basename | regex_replace('[.]j2$', '')
2424
if __systemd_list_name in ['systemd_unit_file_templates', 'systemd_dropins']
25-
else item.item }}"
25+
else item.item | basename }}"
2626
__file: "{{ '99-override.conf' if __systemd_list_name == 'systemd_dropins'
2727
else __unit }}"
2828
__path: "{{ item.units_dir ~ '/' ~ '.'.join(__unit.split('.')[:-1]) ~ '.d'
File renamed without changes.

tests/tests_basic.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
gather_facts: false
66
vars:
77
systemd_unit_file_templates:
8-
- foo.service.j2
8+
- nested/dir/templates/foo.service.j2
99
roles:
1010
- linux-system-roles.systemd
1111
tasks:
@@ -18,7 +18,7 @@
1818
gather_facts: false
1919
vars:
2020
systemd_dropins:
21-
- foo.service.conf.j2
21+
- nested/dir/templates/foo.service.conf.j2
2222
roles:
2323
- linux-system-roles.systemd
2424
tasks:

tests/tests_user_units.yml

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,50 @@
55
gather_facts: false
66
vars:
77
systemd_fail_if_too_old: false # allow test to pass on el7
8+
__bar_service_name: bar.service
9+
__bar_file_src: nested/dir/files/{{ __bar_service_name }}
10+
__service_name: foo.service
11+
__template_src: nested/dir/templates/{{ __service_name }}.j2
12+
__template_conf_src: nested/dir/templates/{{ __service_name }}.conf.j2
813
__users:
914
- name: systemd_user1
1015
uid: 5411
1116
- name: systemd_user2
1217
uid: 5412
1318
__all_units:
1419
- user: "{{ __users[0].name }}"
15-
item: bar.service
20+
item: "{{ __bar_service_name }}"
1621
- user: root
17-
item: bar.service
22+
item: "{{ __bar_service_name }}"
1823
- user: "{{ __users[1].name }}"
19-
item: bar.service
24+
item: "{{ __bar_service_name }}"
2025
- user: "{{ __users[0].name }}"
21-
item: foo.service
26+
item: "{{ __service_name }}"
2227
- user: root
23-
item: foo.service
28+
item: "{{ __service_name }}"
2429
- user: "{{ __users[1].name }}"
25-
item: foo.service
30+
item: "{{ __service_name }}"
2631
__systemd_unit_files:
2732
- user: "{{ __users[0].name }}"
28-
item: bar.service
33+
item: "{{ __bar_file_src }}"
2934
- user: root
30-
item: bar.service
35+
item: "{{ __bar_file_src }}"
3136
- user: "{{ __users[1].name }}"
32-
item: bar.service
37+
item: "{{ __bar_file_src }}"
3338
__systemd_unit_file_templates:
3439
- user: "{{ __users[0].name }}"
35-
item: foo.service.j2
40+
item: "{{ __template_src }}"
3641
- user: root
37-
item: foo.service.j2
42+
item: "{{ __template_src }}"
3843
- user: "{{ __users[1].name }}"
39-
item: foo.service.j2
44+
item: "{{ __template_src }}"
4045
__systemd_dropins:
4146
- user: "{{ __users[0].name }}"
42-
item: foo.service.conf.j2
47+
item: "{{ __template_conf_src }}"
4348
- user: root
44-
item: foo.service.conf.j2
49+
item: "{{ __template_conf_src }}"
4550
- user: "{{ __users[1].name }}"
46-
item: foo.service.conf.j2
51+
item: "{{ __template_conf_src }}"
4752
__systemd_started_units: "{{ __all_units }}"
4853
__systemd_stopped_units: "{{ __all_units }}"
4954
__systemd_restarted_units: "{{ __all_units }}"
@@ -86,7 +91,7 @@
8691

8792
- name: Verify files are present
8893
stat:
89-
path: "{{ __units_dir }}/{{ item.item }}"
94+
path: "{{ __units_dir }}/{{ item.item | basename }}"
9095
register: __stat
9196
failed_when: not __stat.stat.exists
9297
loop: "{{ __systemd_unit_files }}"
@@ -101,7 +106,7 @@
101106
loop: "{{ __systemd_unit_file_templates }}"
102107
vars:
103108
__units_dir: "{{ __systemd_user_info[item.user]['units_dir'] }}"
104-
__dest: "{{ item.item | regex_replace('[.]j2$', '') }}"
109+
__dest: "{{ item.item | basename | regex_replace('[.]j2$', '') }}"
105110

106111
- name: Verify dropin files are present
107112
stat:
@@ -111,7 +116,7 @@
111116
loop: "{{ __systemd_dropins }}"
112117
vars:
113118
__units_dir: "{{ __systemd_user_info[item.user]['units_dir'] }}"
114-
__dest: "{{ item.item | regex_replace('[.]j2$', '') }}"
119+
__dest: "{{ item.item | basename | regex_replace('[.]j2$', '') }}"
115120
__path: "{{ __units_dir }}/\
116121
{{ '.'.join(__dest.split('.')[:-1]) }}.d/\
117122
99-override.conf"

0 commit comments

Comments
 (0)