Skip to content

Commit e156cc3

Browse files
committed
refactor: support ansible 2.19
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 ``` These are the biggest changes. See the porting guide for others. Signed-off-by: Rich Megginson <rmeggins@redhat.com>
1 parent a692e24 commit e156cc3

36 files changed

Lines changed: 295 additions & 437 deletions

roles/rsyslog/tasks/deploy.yml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@
2727
loop: '{{ __rsyslog_rules | flatten }}'
2828
loop_control:
2929
loop_var: inner_item
30+
vars:
31+
__default_flows:
32+
- name: default_flow
33+
inputs: ["{{ __rsyslog_input.name }}"]
34+
outputs: [default_files]
35+
rsyslog_flows: "{{ logging_flows | d(__default_flows, true) }}"
36+
__output_list: "{{ rsyslog_flows | selectattr('inputs', 'contains', __rsyslog_input.name) |
37+
map(attribute='outputs') | flatten | list }}"
3038
when:
3139
- __rsyslog_enabled | bool
32-
- inner_item.filename | d() or inner_item.name | d()
33-
- inner_item.options | d() or inner_item.sections | d()
40+
- inner_item.filename | d("") | length > 0 or inner_item.name | d("") | length > 0
41+
- inner_item.options | d([]) | length > 0 or inner_item.sections | d([]) | length > 0
3442
- inner_item.state is undefined or inner_item.state != 'absent'
3543
notify: Restart rsyslogd
3644
register: __rsyslog_deploy_templates
@@ -52,9 +60,17 @@
5260
loop: '{{ __rsyslog_rules | flatten }}'
5361
loop_control:
5462
loop_var: inner_item
63+
vars:
64+
__default_flows:
65+
- name: default_flow
66+
inputs: ["{{ __rsyslog_input.name }}"]
67+
outputs: [default_files]
68+
rsyslog_flows: "{{ logging_flows | d(__default_flows, true) }}"
69+
__output_list: "{{ rsyslog_flows | selectattr('inputs', 'contains', __rsyslog_input.name) |
70+
map(attribute='outputs') | flatten | list }}"
5571
when:
5672
- (not __rsyslog_enabled | bool) or
5773
(inner_item.state | d('present') == 'absent')
58-
- inner_item.filename | d() or inner_item.name | d()
59-
- inner_item.options | d() or inner_item.sections | d()
74+
- inner_item.filename | d("") | length > 0 or inner_item.name | d("") | length > 0
75+
- inner_item.options | d([]) | length > 0 or inner_item.sections | d([]) | length > 0
6076
notify: Restart rsyslogd

roles/rsyslog/tasks/deploy_nolog.yml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,19 @@
3232
loop_control:
3333
loop_var: inner_item
3434
no_log: true
35+
vars:
36+
__default_flows:
37+
- name: default_flow
38+
inputs: ["{{ __rsyslog_input.name }}"]
39+
outputs: [default_files]
40+
rsyslog_flows: "{{ logging_flows | d(__default_flows, true) }}"
41+
__output_list: "{{ rsyslog_flows | selectattr('inputs', 'contains', __rsyslog_input.name) |
42+
map(attribute='outputs') | flatten | list }}"
3543
when:
3644
- __rsyslog_enabled | bool
45+
- inner_item.filename | d("") | length > 0 or inner_item.name | d("") | length > 0
46+
- inner_item.options | d([]) | length > 0 or inner_item.sections | d([]) | length > 0
3747
- inner_item.state is undefined or inner_item.state != 'absent'
38-
- inner_item.filename | d() or inner_item.name | d()
39-
- inner_item.options | d() or inner_item.sections | d()
4048
notify: Restart rsyslogd
4149
register: __rsyslog_deploy_nolog_templates
4250

@@ -58,9 +66,17 @@
5866
loop_control:
5967
loop_var: inner_item
6068
no_log: true
69+
vars:
70+
__default_flows:
71+
- name: default_flow
72+
inputs: ["{{ __rsyslog_input.name }}"]
73+
outputs: [default_files]
74+
rsyslog_flows: "{{ logging_flows | d(__default_flows, true) }}"
75+
__output_list: "{{ rsyslog_flows | selectattr('inputs', 'contains', __rsyslog_input.name) |
76+
map(attribute='outputs') | flatten | list }}"
6177
when:
6278
- (not __rsyslog_enabled | bool) or
6379
(inner_item.state | d('present') == 'absent')
64-
- inner_item.filename | d() or inner_item.name | d()
65-
- inner_item.options | d() or inner_item.sections | d()
80+
- inner_item.filename | d("") | length > 0 or inner_item.name | d("") | length > 0
81+
- inner_item.options | d([]) | length > 0 or inner_item.sections | d([]) | length > 0
6682
notify: Restart rsyslogd

roles/rsyslog/tasks/main_core.yml

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,36 @@
109109

110110
- name: Generate global rule to add to __rsyslog_common_rules
111111
vars:
112+
ca_cert_path: |-
113+
{% if logging_pki_files | d([]) | length > 0 and
114+
(logging_pki_files.0.ca_cert_src | d("") | length > 0 or
115+
logging_pki_files.0.ca_cert | d("") | length > 0) %}
116+
{% set __ca_cert_file = logging_pki_files.0.ca_cert_src | d(__rsyslog_default_pki_ca_cert_name) | basename %}
117+
{{- logging_pki_files.0.ca_cert |
118+
d(__rsyslog_default_pki_path + __rsyslog_default_pki_cert_dir + __ca_cert_file) -}}
119+
{% else %}
120+
{{- "" -}}
121+
{% endif %}
122+
cert_path: |-
123+
{% if logging_pki_files | d([]) | length > 0 and
124+
(logging_pki_files.0.cert_src | d("") | length > 0 or
125+
logging_pki_files.0.cert | d("") | length > 0) %}
126+
{% set __cert_file = logging_pki_files.0.cert_src | d(__rsyslog_default_pki_cert_name) | basename %}
127+
{{- logging_pki_files.0.cert |
128+
d(__rsyslog_default_pki_path + __rsyslog_default_pki_cert_dir + __cert_file) -}}
129+
{% else %}
130+
{{- "" -}}
131+
{% endif %}
132+
key_path: |-
133+
{% if logging_pki_files | d([]) | length > 0 and
134+
(logging_pki_files.0.private_key_src | d("") | length > 0 or
135+
logging_pki_files.0.private_key | d("") | length > 0) %}
136+
{% set __key_file = logging_pki_files.0.private_key_src | d(__rsyslog_default_pki_key_name) | basename %}
137+
{{- logging_pki_files.0.private_key |
138+
d(__rsyslog_default_pki_path + __rsyslog_default_pki_key_dir + __key_file) -}}
139+
{% else %}
140+
{{- "" -}}
141+
{% endif %}
112142
__rsyslog_global_common_rule:
113143
- filename: '00-global.conf'
114144
comment: 'Global options'
@@ -152,10 +182,10 @@
152182
loop_var: inner_item
153183
when:
154184
- __rsyslog_enabled | bool
155-
- rsyslog_inputs | d([])
156-
- inner_item.filename | d() or inner_item.name | d()
185+
- rsyslog_inputs | d([]) | length > 0
186+
- inner_item.filename | d("") | length > 0 or inner_item.name | d("") | length > 0
157187
- inner_item.state is undefined or inner_item.state != 'absent'
158-
- inner_item.options | d() or inner_item.sections | d()
188+
- inner_item.options | d([]) | length > 0 or inner_item.sections | d([]) | length > 0
159189
notify: Restart rsyslogd
160190
register: __rsyslog_templates
161191

@@ -167,7 +197,7 @@
167197
file:
168198
path: |-
169199
{{ __rsyslog_config_dir }}/{{ inner_item.filename |
170-
d((inner_item.weight if inner_item.weight | d() else
200+
d((inner_item.weight if inner_item.weight | d("") | length > 0 else
171201
rsyslog_weight_map[inner_item.type | d("rules")]) +
172202
"-" + (inner_item.name | d("rules")) + "." +
173203
(inner_item.suffix | d("conf"))) }}
@@ -177,8 +207,8 @@
177207
loop_var: inner_item
178208
when:
179209
- not __rsyslog_enabled | bool
180-
- inner_item.filename | d() or inner_item.name | d()
181-
- inner_item.options | d() or inner_item.sections | d()
210+
- inner_item.filename | d("") | length > 0 or inner_item.name | d("") | length > 0
211+
- inner_item.options | d([]) | length > 0 or inner_item.sections | d([]) | length > 0
182212
notify: Stop rsyslogd
183213

184214
- name: Include input sub-vars
@@ -189,8 +219,7 @@
189219
loop: "{{ rsyslog_inputs }}"
190220
loop_control:
191221
loop_var: input_item
192-
when:
193-
- input_item | d([])
222+
when: input_item | d([]) | length > 0
194223

195224
- name: Run input sub-tasks
196225
include_tasks:
@@ -203,8 +232,8 @@
203232
extended: true
204233
loop_var: input_item
205234
when:
206-
- input_item | d([])
207-
- input_item.type | d()
235+
- input_item | d([]) | length > 0
236+
- input_item.type | d("") | length > 0
208237
- input_item.type != "basics" or
209238
(input_item.type == "basics" and
210239
(ansible_loop.previtem is not defined or
@@ -220,8 +249,8 @@
220249
loop_control:
221250
loop_var: output_item
222251
when:
223-
- output_item | d([])
224-
- output_item.type | d()
252+
- output_item | d([]) | length > 0
253+
- output_item.type | d("") | length > 0
225254

226255
- name: Run output sub-tasks
227256
include_tasks:
@@ -233,8 +262,7 @@
233262
loop: "{{ rsyslog_outputs }}"
234263
loop_control:
235264
loop_var: output_item
236-
when:
237-
- output_item | d([])
265+
when: output_item | d([]) | length > 0
238266

239267
- name: Get rsyslog config files not owned by any package
240268
shell: |
@@ -316,8 +344,8 @@
316344
loop: "{{ [__logging_forwards_tls, __logging_remote_tls] }}"
317345
when:
318346
- item | length > 0
319-
- not (logging_pki_files.0.ca_cert_src | d() or
320-
logging_pki_files.0.ca_cert | d())
347+
- logging_pki_files.0.ca_cert_src | d("") | length == 0
348+
- logging_pki_files.0.ca_cert | d("") | length == 0
321349

322350
- name: Copy local ca_cert file to the target if needed
323351
vars:
@@ -332,7 +360,7 @@
332360
when:
333361
- __logging_forwards_tls | length > 0 or
334362
__logging_remote_tls | length > 0
335-
- logging_pki_files.0.ca_cert_src | d("")
363+
- logging_pki_files.0.ca_cert_src | d("") | length > 0
336364
notify: Restart rsyslogd
337365

338366
- name: Copy local cert file to the target if needed
@@ -348,7 +376,7 @@
348376
when:
349377
- __logging_forwards_tls | length > 0 or
350378
__logging_remote_tls | length > 0
351-
- logging_pki_files.0.cert_src | d("")
379+
- logging_pki_files.0.cert_src | d("") | length > 0
352380
notify: Restart rsyslogd
353381

354382
- name: Copy local key file to the target if needed
@@ -364,7 +392,7 @@
364392
when:
365393
- __logging_forwards_tls | length > 0 or
366394
__logging_remote_tls | length > 0
367-
- logging_pki_files.0.private_key_src | d("")
395+
- logging_pki_files.0.private_key_src | d("") | length > 0
368396
notify: Restart rsyslogd
369397

370398
- name: Enable rsyslog service

roles/rsyslog/tasks/set_certs.yml

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
---
22
- name: Deploy certificates
3-
when: __rsyslog_cert_subject | d([])
3+
when: __rsyslog_cert_subject | d({}) | length > 0
44
block:
55
# This block collect certificates from local location and
66
# copies them to the target host
77
- name: Manage certificates if TLS is enabled
8-
when: __rsyslog_cert_subject.tls is defined |
9-
ternary(__rsyslog_cert_subject.tls,
10-
__rsyslog_cert_subject.use_cert | d(true))
8+
when: __rsyslog_cert_subject.tls | d((__rsyslog_cert_subject.use_cert | d(true, true)), true)
119
block:
1210
- name: "Copy ca_cert on the control host to the specified path
1311
on the target host"
@@ -16,7 +14,7 @@
1614
dest: '{{ __rsyslog_cert_subject.ca_cert |
1715
d(__rsyslog_default_pki_path + __rsyslog_default_pki_cert_dir) }}'
1816
mode: '0444'
19-
when: __rsyslog_cert_subject.ca_cert_src | d()
17+
when: __rsyslog_cert_subject.ca_cert_src | d("") | length > 0
2018

2119
- name: "Copy cert on the control host to the specified path
2220
on the target host"
@@ -25,7 +23,7 @@
2523
dest: '{{ __rsyslog_cert_subject.cert |
2624
d(__rsyslog_default_pki_path + __rsyslog_default_pki_cert_dir) }}'
2725
mode: '0444'
28-
when: __rsyslog_cert_subject.cert_src | d()
26+
when: __rsyslog_cert_subject.cert_src | d("") | length > 0
2927

3028
- name: "Copy key on the control host to the specified path
3129
on the target host"
@@ -34,33 +32,31 @@
3432
dest: '{{ __rsyslog_cert_subject.private_key |
3533
d(__rsyslog_default_pki_path + __rsyslog_default_pki_key_dir) }}'
3634
mode: '0400'
37-
when: __rsyslog_cert_subject.private_key_src | d()
35+
when: __rsyslog_cert_subject.private_key_src | d("") | length > 0
3836

3937
- name: Check certs - tls is true, but triplets are not given
4038
fail:
4139
msg: "Error: you specified tls: true; you must specify all
4240
3 of ca_cert, cert, private_key, or all 3 of ca_cert_src,
4341
cert_src, private_key_src, or set tls: false in the
4442
configuration named {{ __rsyslog_cert_subject.name }}"
45-
when: not ((__rsyslog_cert_subject.ca_cert | d() and
46-
__rsyslog_cert_subject.cert | d() and
47-
__rsyslog_cert_subject.private_key | d()) or
48-
(__rsyslog_cert_subject.ca_cert_src | d() and
49-
__rsyslog_cert_subject.cert_src | d() and
50-
__rsyslog_cert_subject.private_key_src | d()))
43+
when: not ((__rsyslog_cert_subject.ca_cert | d("") | length > 0 and
44+
__rsyslog_cert_subject.cert | d("") | length > 0 and
45+
__rsyslog_cert_subject.private_key | d("") | length > 0) or
46+
(__rsyslog_cert_subject.ca_cert_src | d("") | length > 0 and
47+
__rsyslog_cert_subject.cert_src | d("") | length > 0 and
48+
__rsyslog_cert_subject.private_key_src | d("") | length > 0))
5149

5250
- name: Check certs - key/certs data are provided, but tls is false
5351
debug:
5452
msg: "WARNING: you specified tls: false and also specified one or
5553
more cert/key files in the configuration named
5654
{{ __rsyslog_cert_subject.name }}"
5755
when:
58-
- not (__rsyslog_cert_subject.tls is defined |
59-
ternary(__rsyslog_cert_subject.tls,
60-
__rsyslog_cert_subject.use_cert | d(true)))
61-
- (__rsyslog_cert_subject.ca_cert | d() or
62-
__rsyslog_cert_subject.cert | d() or
63-
__rsyslog_cert_subject.private_key | d()) or
64-
(__rsyslog_cert_subject.ca_cert_src | d() or
65-
__rsyslog_cert_subject.cert_src | d() or
66-
__rsyslog_cert_subject.private_key_src | d())
56+
- not (__rsyslog_cert_subject.tls | d((__rsyslog_cert_subject.use_cert | d(true, true)), true))
57+
- (__rsyslog_cert_subject.ca_cert | d("") | length > 0 or
58+
__rsyslog_cert_subject.cert | d("") | length > 0 or
59+
__rsyslog_cert_subject.private_key | d("") | length > 0) or
60+
(__rsyslog_cert_subject.ca_cert_src | d("") | length > 0 or
61+
__rsyslog_cert_subject.cert_src | d("") | length > 0 or
62+
__rsyslog_cert_subject.private_key_src | d("") | length > 0)

roles/rsyslog/tasks/set_vars.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
- name: "Include /vars/main.yml from {{ role_path }}"
3535
include_vars: "{{ item }}"
3636
loop: "{{ lookup('lines',
37-
'/bin/ls -1 {{ role_path }}/vars/main.yml
38-
{{ role_path }}/vars/*/*/main.yml').split(',') }}"
37+
'/bin/ls -1 ' ~ role_path ~ '/vars/main.yml
38+
' ~ role_path ~ '/vars/*/*/main.yml').split(',') }}"
3939
when: __snapshot_gather_vars is defined and
4040
__snapshot_gather_vars | bool

roles/rsyslog/templates/general_action_params.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ action.{{ option }}="{{ value }}"
55
{% endif %}
66
{% endfor %}
77
{% endif %}
8+
{{- "" -}}

roles/rsyslog/templates/general_queue_params.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ queue.{{ option }}="{{ value }}"
55
{% endif %}
66
{% endfor %}
77
{% endif %}
8+
{{- "" -}}

roles/rsyslog/templates/global.j2

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
{% set ca_cert_path = '' %}
2-
{% set cert_path = '' %}
3-
{% set key_path = '' %}
4-
{% if logging_pki_files.0.ca_cert_src | d() or logging_pki_files.0.ca_cert | d() %}
5-
{% set __ca_cert_file = logging_pki_files.0.ca_cert_src | d(__rsyslog_default_pki_ca_cert_name) | basename %}
6-
{% set ca_cert_path = logging_pki_files.0.ca_cert |
7-
d(__rsyslog_default_pki_path + __rsyslog_default_pki_cert_dir + __ca_cert_file) %}
8-
{% endif %}
9-
{% if logging_pki_files.0.cert_src | d() or logging_pki_files.0.cert | d() %}
10-
{% set __cert_file = logging_pki_files.0.cert_src | d(__rsyslog_default_pki_cert_name) | basename %}
11-
{% set cert_path = logging_pki_files.0.cert |
12-
d(__rsyslog_default_pki_path + __rsyslog_default_pki_cert_dir + __cert_file) %}
13-
{% endif %}
14-
{% if logging_pki_files.0.private_key_src | d() or logging_pki_files.0.private_key | d() %}
15-
{% set __key_file = logging_pki_files.0.private_key_src | d(__rsyslog_default_pki_key_name) | basename %}
16-
{% set key_path = logging_pki_files.0.private_key |
17-
d(__rsyslog_default_pki_path + __rsyslog_default_pki_key_dir + __key_file) %}
18-
{% endif %}
191
global(
202
workDirectory="{{ __rsyslog_work_dir }}"
213
{% if ca_cert_path != '' %}

roles/rsyslog/templates/input_ovirt.j2

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,14 @@ if $syslogtag == "{{ __rsyslog_input.name }}" then {
6969
}
7070
}
7171
{% endif %}
72-
{% set outdict = {} %}
73-
{% for flow in logging_flows %}
74-
{% if flow.inputs | intersect([ __rsyslog_input.name ]) %}
75-
{% for oname in flow.outputs %}
76-
{% set _ = outdict.__setitem__(oname, outdict.get(oname,[])|union([ __rsyslog_input.name ])) %}
77-
{% endfor %}
78-
{% endif %}
79-
{% endfor %}
80-
{% for output in rsyslog_outputs %}
81-
{% if outdict[output.name] | d(false) %}
72+
{% for output_name in __output_list %}
8273
if
83-
{% for inputname in outdict[output.name] %}
84-
{% if inputname == __rsyslog_input.name %}
85-
{% if not loop.first %}
86-
or
87-
{% endif %}
88-
{% if __rsyslog_ovirt_subtype == "collectd" %}
74+
{% if __rsyslog_ovirt_subtype == "collectd" %}
8975
($inputname == "{{ __rsyslog_input.name }}")
90-
{% elif __rsyslog_ovirt_subtype == "engine" or __rsyslog_ovirt_subtype == "vdsm" %}
76+
{% elif __rsyslog_ovirt_subtype == "engine" or __rsyslog_ovirt_subtype == "vdsm" %}
9177
($syslogtag == "{{ __rsyslog_input.name }}")
92-
{% endif %}
93-
{% endif %}
94-
{% endfor %}
78+
{% endif %}
9579
then {
96-
call {{ output.name }}
80+
call {{ output_name }}
9781
}
98-
{% endif %}
9982
{% endfor %}

0 commit comments

Comments
 (0)