Skip to content

Commit 1f975b4

Browse files
vojtechtrefnyrichm
authored andcommitted
refactor: Ansible 2.19 support
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 <rmeggins@redhat.com>
1 parent 396f834 commit 1f975b4

10 files changed

+55
-34
lines changed

tasks/main-blivet.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
- name: Show storage_pools
1212
debug:
13-
var: storage_pools
13+
var: storage_pools | d([])
1414
verbosity: 1
1515

1616
- name: Show storage_volumes
1717
debug:
18-
var: storage_volumes
18+
var: storage_volumes | d([])
1919
verbosity: 1
2020

2121
- name: Get required packages
@@ -177,7 +177,7 @@
177177
- name: Tell systemd to refresh its view of /etc/fstab
178178
systemd:
179179
daemon_reload: true
180-
when: blivet_output['mounts']
180+
when: blivet_output['mounts'] | length > 0
181181

182182
- name: Set up new/current mounts
183183
mount: # noqa fqcn
@@ -209,7 +209,7 @@
209209
- name: Tell systemd to refresh its view of /etc/fstab
210210
systemd:
211211
daemon_reload: true
212-
when: blivet_output['mounts']
212+
when: blivet_output['mounts'] | length > 0
213213

214214
#
215215
# Manage /etc/crypttab

tests/test-verify-pool-members.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@
5454
- name: Set expected pv type
5555
set_fact:
5656
_storage_test_expected_pv_type: "{{ storage_test_pool.raid_level }}"
57-
when: storage_test_pool.type == 'lvm' and storage_test_pool.raid_level
57+
when:
58+
- storage_test_pool.type == 'lvm'
59+
- not storage_test_pool.raid_level is none
60+
- storage_test_pool.raid_level | length > 0
5861

5962
- name: Check the type of each PV
6063
assert:

tests/test-verify-volume-encryption.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
when:
6565
- _storage_test_volume_present
6666
- storage_test_volume.encryption
67-
- storage_test_volume.encryption_luks_version
67+
- not storage_test_volume.encryption_luks_version is none
68+
- storage_test_volume.encryption_luks_version | length > 0
6869

6970
- name: Check LUKS key size
7071
assert:
@@ -79,7 +80,8 @@
7980
when:
8081
- _storage_test_volume_present
8182
- storage_test_volume.encryption
82-
- storage_test_volume.encryption_key_size
83+
- not storage_test_volume.encryption_key_size is none
84+
- storage_test_volume.encryption_key_size > 0
8385

8486
- name: Check LUKS cipher
8587
assert:
@@ -96,7 +98,8 @@
9698
when:
9799
- _storage_test_volume_present
98100
- storage_test_volume.encryption
99-
- storage_test_volume.encryption_cipher
101+
- not storage_test_volume.encryption_cipher is none
102+
- storage_test_volume.encryption_cipher | length > 0
100103

101104
- name: Set test variables
102105
set_fact:

tests/test-verify-volume-fs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
(storage_test_blkinfo.info[storage_test_volume._device].fstype | length
1111
== 0 and storage_test_volume.fs_type == "unformatted")
1212
when:
13-
- storage_test_volume.fs_type
13+
- storage_test_volume.fs_type | length > 0
1414
- _storage_test_volume_present
1515

1616
# label

tests/test-verify-volume-mount.yml

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@
3232
stat:
3333
path: "{{ storage_test_volume.mount_point }}"
3434
register: storage_test_found_mount_stat
35-
when: _storage_test_volume_present and
36-
storage_test_volume.mount_point and
37-
(storage_test_volume.mount_user or
38-
storage_test_volume.mount_group or
39-
storage_test_volume.mount_mode)
35+
when:
36+
- _storage_test_volume_present | bool
37+
- not storage_test_volume.mount_point is none
38+
- storage_test_volume.mount_point | length > 0
39+
- (not storage_test_volume.mount_user is none and storage_test_volume.mount_user | length > 0) or
40+
(not storage_test_volume.mount_group is none and storage_test_volume.mount_group | length > 0) or
41+
(not storage_test_volume.mount_mode is none and storage_test_volume.mount_mode | length > 0)
4042

4143
#
4244
# Verify mount presence.
@@ -58,9 +60,12 @@
5860
{{ storage_test_volume.name }}) has unexpected owner
5961
(expected: {{ storage_test_volume.mount_user }}, found:
6062
{{ storage_test_found_mount_stat.stat.pw_name }})"
61-
when: _storage_test_volume_present and
62-
storage_test_volume.mount_point and
63-
storage_test_volume.mount_user
63+
when:
64+
- _storage_test_volume_present
65+
- not storage_test_volume.mount_point is none
66+
- storage_test_volume.mount_point | length > 0
67+
- not storage_test_volume.mount_user is none
68+
- storage_test_volume.mount_user | length > 0
6469

6570
- name: Verify mount directory group
6671
assert:
@@ -70,9 +75,12 @@
7075
{{ storage_test_volume.name }}) has unexpected group
7176
(expected: {{ storage_test_volume.mount_group }}, found:
7277
{{ storage_test_found_mount_stat.stat.gr_name }})"
73-
when: _storage_test_volume_present and
74-
storage_test_volume.mount_point and
75-
storage_test_volume.mount_group
78+
when:
79+
- _storage_test_volume_present
80+
- not storage_test_volume.mount_point is none
81+
- storage_test_volume.mount_point | length > 0
82+
- not storage_test_volume.mount_group is none
83+
- storage_test_volume.mount_group | length > 0
7684

7785
- name: Verify mount directory permissions
7886
assert:
@@ -82,9 +90,12 @@
8290
{{ storage_test_volume.name }}) has unexpected permissions (expected:
8391
{{ storage_test_volume.mount_mode }}, found:
8492
{{ storage_test_found_mount_stat.stat.mode }})"
85-
when: _storage_test_volume_present and
86-
storage_test_volume.mount_point and
87-
storage_test_volume.mount_mode
93+
when:
94+
- _storage_test_volume_present
95+
- not storage_test_volume.mount_point is none
96+
- storage_test_volume.mount_point | length > 0
97+
- not storage_test_volume.mount_mode is none
98+
- storage_test_volume.mount_mode | length > 0
8899

89100
#
90101
# Verify swap status.

tests/test-verify-volume-size.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262

6363
- name: Process thin pool sizes when applicable
6464
when:
65-
- storage_test_volume.thin
65+
- not storage_test_volume.thin is none
66+
- storage_test_volume.thin | bool
6667
- _storage_test_volume_present | bool
6768
block:
6869
- name: Default thin pool reserved space values

tests/test-verify-volume.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
# compression
1616
# deduplication
1717
_storage_test_volume_present: "{{ storage_test_volume.state == 'present' and
18-
(not storage_test_pool | default() or
18+
(storage_test_pool | default({}) | length == 0 or
1919
storage_test_pool.state == 'present') }}"
2020

21-
- name: Run test verify for {{ storage_test_volume_subset }}
21+
- name: Run test verify for storage_test_volume_subset
2222
include_tasks: test-verify-volume-{{ storage_test_volume_subset }}.yml
2323
loop: "{{ _storage_volume_tests }}"
2424
loop_control:

tests/verify-pool-member-encryption.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
when:
3636
- storage_test_pool.state == 'present'
3737
- storage_test_pool.encryption
38-
- storage_test_pool.encryption_luks_version
38+
- not storage_test_pool.encryption_luks_version is none
39+
- storage_test_pool.encryption_luks_version | length > 0
3940

4041
- name: Check LUKS key size
4142
assert:
@@ -50,7 +51,8 @@
5051
when:
5152
- storage_test_pool.state == 'present'
5253
- storage_test_pool.encryption
53-
- storage_test_pool.encryption_key_size
54+
- not storage_test_pool.encryption_key_size is none
55+
- storage_test_pool.encryption_key_size > 0
5456

5557
- name: Check LUKS cipher
5658
assert:
@@ -67,4 +69,5 @@
6769
when:
6870
- storage_test_pool.state == 'present'
6971
- storage_test_pool.encryption
70-
- storage_test_pool.encryption_cipher
72+
- not storage_test_pool.encryption_cipher is none
73+
- storage_test_pool.encryption_cipher | length > 0

tests/verify-role-failed.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@
4444

4545
- name: Verify correct exception or error message
4646
assert:
47-
that: exception is search(__storage_failed_exception) or
47+
that: msg_msg is search(__storage_failed_exception) or
48+
exception is search(__storage_failed_exception) or
4849
msg_stdout is search(__storage_failed_exception) or
4950
msg_stderr is search(__storage_failed_exception) or
5051
stdout is search(__storage_failed_exception) or
5152
stderr is search(__storage_failed_exception)
5253
when: __storage_failed_exception is defined
5354
vars:
55+
msg_msg: "{{ ansible_failed_result.msg.msg | d('') }}"
5456
exception: "{{ ansible_failed_result.msg.exception | d('') }}"
5557
msg_stdout: "{{ ansible_failed_result.msg.module_stdout | d('') }}"
5658
msg_stderr: "{{ ansible_failed_result.msg.module_stderr | d('') }}"

tests/verify-role-results.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,18 @@
3333
#
3434
- name: Verify the volumes listed in storage_pools were correctly managed
3535
include_tasks: "test-verify-pool.yml"
36-
loop: "{{ _storage_pools_list }}"
36+
loop: "{{ _storage_pools_list | d([]) }}"
3737
loop_control:
3838
loop_var: storage_test_pool
39-
when: _storage_pools_list is defined and _storage_pools_list | length > 0
4039

4140
#
4241
# Verify standalone volumes.
4342
#
4443
- name: Verify the volumes with no pool were correctly managed
4544
include_tasks: "test-verify-volume.yml"
46-
loop: "{{ _storage_volumes_list }}"
45+
loop: "{{ _storage_volumes_list | d([]) }}"
4746
loop_control:
4847
loop_var: storage_test_volume
49-
when: _storage_volumes_list is defined and _storage_volumes_list | length > 0
5048

5149
#
5250
# Clean up.

0 commit comments

Comments
 (0)