Skip to content

Commit 8ea0394

Browse files
committed
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 <[email protected]>
1 parent 2d50d82 commit 8ea0394

File tree

3 files changed

+34
-65
lines changed

3 files changed

+34
-65
lines changed

tasks/main.yml

Lines changed: 19 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -58,61 +58,24 @@
5858
owner: "{{ __ssh_config_owner | trim }}"
5959
group: "{{ __ssh_config_group | trim }}"
6060
mode: "{{ __ssh_config_mode | trim }}"
61-
validate: >-
62-
{% if __ssh_supports_validate %}
63-
ssh -G -F %s example.com
64-
{% else %}
65-
true %s
66-
{% endif %}
61+
validate: "{{ __ssh_supports_validate | ternary('ssh -G -F %s example.com', 'true %s') }}"
6762
backup: "{{ ssh_backup }}"
6863
vars:
69-
__ssh_skip_defaults: >-
70-
{% if ssh_skip_defaults != 'auto' %}
71-
{{ ssh_skip_defaults }}
72-
{% elif ssh_user is not none %}
73-
true
74-
{% else %}
75-
{% if ssh_drop_in_name is not none and __ssh_supports_drop_in %}
76-
true
77-
{% else %}
78-
false
79-
{% endif %}
80-
{% endif %}
81-
__ssh_config_file: >-
82-
{% if ssh_config_file is not none %}
83-
{{ ssh_config_file }}
84-
{% elif ssh_user is not none and
85-
ansible_facts['getent_passwd'] is defined %}
86-
{{ ansible_facts['getent_passwd'][ssh_user][4] }}/.ssh/config
87-
{% else %}
88-
{% if ssh_drop_in_name is not none and __ssh_supports_drop_in %}
89-
{{ __ssh_drop_in_template | replace("{name}", ssh_drop_in_name) }}
90-
{% else %}
91-
/etc/ssh/ssh_config
92-
{% endif %}
93-
{% endif %}
94-
__ssh_config_owner: >-
95-
{% if ssh_config_owner is not none %}
96-
{{ ssh_config_owner }}
97-
{% elif ssh_user is not none %}
98-
{{ ssh_user }}
99-
{% else %}
100-
root
101-
{% endif %}
102-
__ssh_config_group: >-
103-
{% if ssh_config_group is not none %}
104-
{{ ssh_config_group }}
105-
{% elif ssh_user is not none and
106-
ansible_facts['getent_passwd'] is defined %}
107-
{{ ssh_user }}
108-
{% else %}
109-
root
110-
{% endif %}
111-
__ssh_config_mode: >-
112-
{% if ssh_config_mode is not none %}
113-
{{ ssh_config_mode }}
114-
{% elif ssh_user is not none %}
115-
600
116-
{% else %}
117-
644
118-
{% endif %}
64+
__ssh_skip_defaults: "{{ ssh_skip_defaults if ssh_skip_defaults != 'auto'
65+
else (ssh_user is not none) or
66+
(ssh_drop_in_name is not none and __ssh_supports_drop_in) }}"
67+
__ssh_config_file: "{{ ssh_config_file if ssh_config_file is not none
68+
else ansible_facts['getent_passwd'][ssh_user][4] ~ '/.ssh/config'
69+
if ssh_user is not none and ansible_facts['getent_passwd'] is defined
70+
else __ssh_drop_in_template | replace('{name}', ssh_drop_in_name)
71+
if ssh_drop_in_name is not none and __ssh_supports_drop_in
72+
else '/etc/ssh/ssh_config' }}"
73+
__ssh_config_owner: "{{ ssh_config_owner if ssh_config_owner is not none
74+
else ssh_user if ssh_user is not none
75+
else 'root' }}"
76+
__ssh_config_group: "{{ ssh_config_group if ssh_config_group is not none
77+
else ssh_user if ssh_user is not none and ansible_facts['getent_passwd'] is defined
78+
else 'root' }}"
79+
__ssh_config_mode: "{{ ssh_config_mode if ssh_config_mode is not none
80+
else '0600' if ssh_user is not none
81+
else '0644' }}"

templates/ssh_config.j2

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{ ansible_managed | comment }}
22
{{ "system_role:ssh" | comment(prefix="", postfix="") }}
33
{% macro render_option(key, value, indent=false) %}
4-
{% if value is defined %}
4+
{% if value is defined and value is not none %}
55
{% if value is sameas true %}
66
{% if indent %} {% endif %}
77
{{ key }} yes
@@ -13,22 +13,28 @@
1313
{{ key }} {{ value | string }}
1414
{% else %}
1515
{% for i in value %}
16+
{% if i is none %}
17+
{{- '' -}}
18+
{% else %}
1619
{% if indent %} {% endif %}
1720
{{ key }} {{ i | string }}
21+
{% endif %}
1822
{% endfor %}
1923
{% endif %}
24+
{% else %}
25+
{{- '' -}}
2026
{% endif %}
2127
{% endmacro %}
2228
{% macro body_option(key, override) %}
23-
{% set value = undefined %}
24-
{% if override is defined %}
25-
{% set value = override %}
26-
{% elif ssh[key] is defined %}
27-
{% set value = ssh[key] %}
29+
{% if override is defined and override is not none %}
30+
{{ render_option(key, override) -}}
31+
{% elif ssh[key] is defined and ssh[key] is not none %}
32+
{{ render_option(key, ssh[key]) -}}
2833
{% elif __ssh_defaults[key] is defined and not __ssh_skip_defaults | trim | bool %}
29-
{% set value = __ssh_defaults[key] %}
34+
{{ render_option(key, __ssh_defaults[key]) -}}
35+
{% else %}
36+
{{- '' -}}
3037
{% endif %}
31-
{{ render_option(key, value) -}}
3238
{% endmacro %}
3339
{% macro match_block(match_list) %}
3440
{% if match_list["Condition"] is defined %}

tests/tests_backup.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
ssh_ForwardX11Trusted: 'yes' # noqa var-naming
4141
register: second_run
4242

43-
- name: Find new backups files
43+
- name: Find new backups files again
4444
ansible.builtin.find:
4545
paths: "{{ main_ssh_config_path }}"
4646
patterns: "{{ main_ssh_config_name }}.*@*~"

0 commit comments

Comments
 (0)