Skip to content

Commit 0fd8636

Browse files
committed
refactor: support ansible 2.19, new ansible-lint
Ansible 2.19 introduces some big changes https://docs.ansible.com/ansible/devel/porting_guides/porting_guide_core_2.19.html One big change is that data structures are no longer mutable by the use of python methods such as `__setitem__`, `setdefault`, `update`, etc. in Jinja constructs. Instead, items must use filters or other Jinja operations. One common idiom is to mutate each element in a list. Since we cannot do this "in-place" anymore, a common way to do this is: ```yaml - name: Construct a new list from an existing list and mutate each element set_fact: __new_list: "{{ __new_list | d([]) + [mutated_item] }}" loop: "{{ old_list }}" mutated_item: "{{ some value based on item from old list }}" - name: Reset original old list set_fact: old_list: "{{ __new_list }}" ``` Similarly with `dict` items: ```yaml - name: Construct a new dict from an existing dict and mutate each element set_fact: __new_dict: "{{ __new_dict | d({}) | combine(mutated_item) }}" loop: "{{ old_dict | dict2items }}" mutated_item: "{{ {item.key: mutation of item.value} }}" - name: Reset original old dict set_fact: old_dict: "{{ __new_dict }}" ``` Another big change is that a boolean expression in a `when` or similar construct must be converted to a boolean - we cannot rely on the implicit evaluation in a boolean context. For example, if `var` is some iterable, like a `dict`, `list`, or `string`, you used to be able to evaluate an empty value in a boolean context: ```yaml when: var # do this only if var is not empty ``` You now have to explicitly test for empty using `length`: ```yaml when: var | length > 0 # do this only if var is not empty ``` Similarly for `int` values - you cannot rely on `0` being evaluated as false and non-zero true - you must explicitly compare the values with `==` or `!=` These are the biggest changes. See the porting guide for others. Signed-off-by: Rich Megginson <[email protected]>
1 parent 6edeb8d commit 0fd8636

File tree

4 files changed

+47
-66
lines changed

4 files changed

+47
-66
lines changed

tasks/get_update_user_info.yml

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,11 @@
1515
- name: Update systemd user info with new user info
1616
when: __systemd_user_name not in __systemd_user_info
1717
set_fact:
18-
__systemd_user_info: |
19-
{% set getent = ansible_facts["getent_passwd"][__systemd_user_name] %}
20-
{% set rv = __systemd_user_info | d({}) %}
21-
{% if __systemd_user_name not in rv %}
22-
{% set _ = rv.__setitem__(__systemd_user_name, {}) %}
23-
{% endif %}
24-
{% if "xdg_dir" not in rv[__systemd_user_name] %}
25-
{% set xdg_dir = "/run/user/" ~ getent[1] %}
26-
{% set _ = rv[__systemd_user_name].update({"xdg_dir": xdg_dir}) %}
27-
{% endif %}
28-
{% if "units_dir" not in rv[__systemd_user_name] %}
29-
{% if __systemd_user_name == "root" %}
30-
{% set _ = rv[__systemd_user_name].update({"units_dir": __admin_units_dir}) %}
31-
{% else %}
32-
{% set units_dir = getent[4] ~ '/' ~ __user_units_dir %}
33-
{% set _ = rv[__systemd_user_name].update({"units_dir": units_dir}) %}
34-
{% endif %}
35-
{% endif %}
36-
{% if "group" not in rv[__systemd_user_name] %}
37-
{% set group = getent[2] %}
38-
{% set _ = rv[__systemd_user_name].update({"group": group}) %}
39-
{% endif %}
40-
{{ rv }}
18+
__systemd_user_info: "{{ __systemd_user_info | combine({__systemd_user_name: user_dict}) }}"
19+
vars:
20+
getent: "{{ ansible_facts['getent_passwd'][__systemd_user_name] }}"
21+
user_dict:
22+
xdg_dir: /run/user/{{ getent[1] }}
23+
units_dir: "{{ __admin_units_dir if __systemd_user_name == 'root'
24+
else getent[4] ~ '/' ~ __user_units_dir }}"
25+
group: "{{ getent[2] }}"

tasks/manage_user_info.yml

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,39 @@
3232
loop_control:
3333
loop_var: __systemd_user_name
3434

35+
- name: Reset __systemd_dict_list
36+
set_fact:
37+
__systemd_dict_list: []
38+
3539
# convert to the output format
3640
- name: Convert to list of dict with user data
3741
set_fact:
38-
__systemd_dict_list: |
39-
{% set rv = [] %}
40-
{% for item in __systemd_list %}
41-
{% set newitem = {} %}
42-
{% if item is mapping %}
43-
{% set _ = newitem.update(item) %}
44-
{% else %}
45-
{% set _ = newitem.update({"item": item}) %}
46-
{% endif %}
47-
{% if "state" not in newitem %}
48-
{% set _ = newitem.update({"state": "present"}) %}
49-
{% endif %}
50-
{% if "user" not in newitem %}
51-
{% set _ = newitem.update({"user": "root"}) %}
52-
{% endif %}
53-
{% set user = newitem["user"] %}
54-
{% if "group" not in newitem %}
55-
{% set _ = newitem.update({"group": __systemd_user_info[user]["group"]}) %}
56-
{% endif %}
57-
{% if "xdg_dir" not in newitem %}
58-
{% set _ = newitem.update({"xdg_dir": __systemd_user_info[user]["xdg_dir"]}) %}
59-
{% endif %}
60-
{% if "units_dir" not in newitem %}
61-
{% set _ = newitem.update({"units_dir": __systemd_user_info[user]["units_dir"]}) %}
62-
{% endif %}
63-
{% if "mode" not in newitem %}
64-
{% set _ = newitem.update({"mode": (user == "root") | ternary("0644", "0600")}) %}
65-
{% endif %}
66-
{% if "dir_mode" not in newitem %}
67-
{% set _ = newitem.update({"dir_mode": (user == "root") | ternary("0755", "0700")}) %}
68-
{% endif %}
69-
{% set _ = rv.append(newitem) %}
70-
{% endfor %}
71-
{{ rv }}
42+
__systemd_dict_list: "{{ __systemd_dict_list +
43+
[newitem | combine(newstate, newuser, newgroup, newxdg_dir, newunits_dir, newmode, newdir_mode)] }}"
44+
loop: "{{ __systemd_list }}"
45+
vars:
46+
newitem: "{{ item if item is mapping else {'item': item} }}"
47+
newstate:
48+
state: "{{ item['state'] if item is mapping and 'state' in item
49+
else 'present' }}"
50+
newuser:
51+
user: "{{ item['user'] if item is mapping and 'user' in item
52+
else 'root' }}"
53+
newgroup:
54+
group: "{{ item['group'] if item is mapping and 'group' in item
55+
else __systemd_user_info[newuser['user']]['group'] }}"
56+
newxdg_dir:
57+
xdg_dir: "{{ item['xdg_dir'] if item is mapping and 'xdg_dir' in item
58+
else __systemd_user_info[newuser['user']]['xdg_dir'] }}"
59+
newunits_dir:
60+
units_dir: "{{ item['units_dir'] if item is mapping and 'units_dir' in item
61+
else __systemd_user_info[newuser['user']]['units_dir'] }}"
62+
newmode:
63+
mode: "{{ item['mode'] if item is mapping and 'mode' in item
64+
else (newuser['user'] == 'root') | ternary('0644', '0600') }}"
65+
newdir_mode:
66+
dir_mode: "{{ item['dir_mode'] if item is mapping and 'dir_mode' in item
67+
else (newuser['user'] == 'root') | ternary('0755', '0700') }}"
7268

7369
- name: Enable linger if needed
7470
command: loginctl enable-linger {{ item | quote }}

tests/tests_basic.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
roles:
138138
- linux-system-roles.systemd
139139
tasks:
140-
- name: Get UnitFileState=
140+
- name: Get UnitFileState= - 2
141141
# noqa command-instead-of-module
142142
command: systemctl show -p UnitFileState foo.service
143143
register: unit_file_state_now
@@ -158,13 +158,13 @@
158158
roles:
159159
- linux-system-roles.systemd
160160
pre_tasks:
161-
- name: Save UnitFileState before calling role
161+
- name: Save UnitFileState before calling role - 2
162162
# noqa command-instead-of-module
163163
command: systemctl show -p UnitFileState sshd.service
164164
register: sshd_state_before
165165
changed_when: false
166166
tasks:
167-
- name: Get UnitFileState=
167+
- name: Get UnitFileState= - 3
168168
# noqa command-instead-of-module
169169
command: systemctl show -p UnitFileState sshd.service
170170
register: sshd_state_after
@@ -187,7 +187,7 @@
187187
roles:
188188
- linux-system-roles.systemd
189189
tasks:
190-
- name: Get UnitFileState=
190+
- name: Get UnitFileState= - 4
191191
# noqa command-instead-of-module
192192
command: systemctl show -p UnitFileState sshd.service
193193
register: sshd_state_now
@@ -277,7 +277,7 @@
277277
systemd_masked_units:
278278
- "{{ test_unit }}"
279279

280-
- name: Get test unit state
280+
- name: Get test unit state - 2
281281
# noqa command-instead-of-module
282282
command: systemctl show -p UnitFileState -p SubState "{{ test_unit }}"
283283
register: test_unit_state
@@ -290,7 +290,7 @@
290290
test_unit_state.stdout is search("UnitFileState=bad")
291291
- test_unit_state.stdout is search("SubState=dead")
292292

293-
- name: Ensure test unit is running and unmasked
293+
- name: Ensure test unit is running and unmasked - 2
294294
include_role:
295295
name: linux-system-roles.systemd
296296
vars:
@@ -299,13 +299,13 @@
299299
systemd_unmasked_units:
300300
- "{{ test_unit }}"
301301

302-
- name: Get test unit state
302+
- name: Get test unit state - 3
303303
# noqa command-instead-of-module
304304
command: systemctl show -p UnitFileState -p SubState "{{ test_unit }}"
305305
register: test_unit_state
306306
changed_when: false
307307

308-
- name: Ensure test unit running and unmasked
308+
- name: Ensure test unit running and unmasked - 2
309309
assert:
310310
that:
311311
- test_unit_state.stdout is search("UnitFileState=enabled") or

tests/tests_user_units.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
vars:
162162
systemd_disabled_units: "{{ __systemd_disabled_units }}"
163163

164-
- name: Get unit file state of units after
164+
- name: Get unit file state of units after - 2
165165
# noqa command-instead-of-module
166166
command: systemctl {{ scope }} show -p UnitFileState {{ item.item }}
167167
changed_when: false

0 commit comments

Comments
 (0)