Skip to content

Commit 183c404

Browse files
kaamforkaamfor
and
kaamfor
authored
Replace embedded template conditionals (#97)
* Refactor templated conditionals for Ansible 9 * Fix Terraform template syntax --------- Co-authored-by: kaamfor <[email protected]>
1 parent ce33197 commit 183c404

File tree

2 files changed

+53
-53
lines changed

2 files changed

+53
-53
lines changed

roles/terrible/tasks/assert_vars.yml

+52-52
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,30 @@
44
- name: Assert VMs Variables
55
assert:
66
quiet: yes
7-
that: "{{ item }} is defined"
7+
that:
8+
- "item.value is not none"
9+
- "item.value | string | length > 0"
810
fail_msg: >
9-
You must specify {{ item }}
10-
with_items:
11-
- hostvars['terraform_node']
12-
- hostvars['terraform_node']['ansible_host']
13-
- hostvars['terraform_node']['ansible_connection']
14-
- ansible_host
15-
- base_deploy_path
16-
- disk_source
17-
- network_interfaces
18-
- pool_name
19-
- ssh_password
20-
- ssh_port
21-
- ssh_public_key_file
22-
- ssh_user
23-
- state_save_file
24-
- vm_autostart
11+
You must specify {{ item.key }}
12+
loop: "{{ _mandantory_variables | dict2items }}"
13+
vars:
14+
_mandantory_variables:
15+
"hostvars['terraform_node']": "{{ hostvars['terraform_node'] | default }}"
16+
"hostvars['terraform_node']['ansible_host']": "{{ hostvars['terraform_node']['ansible_host'] | default }}"
17+
"hostvars['terraform_node']['ansible_connection']": "{{ hostvars['terraform_node']['ansible_connection'] | default }}"
18+
"ansible_host": "{{ ansible_host | default }}"
19+
"base_deploy_path": "{{ base_deploy_path | default }}"
20+
"disk_source": "{{ disk_source | default }}"
21+
"network_interfaces": "{{ network_interfaces | default }}"
22+
"pool_name": "{{ pool_name | default }}"
23+
"ssh_password": "{{ ssh_password | default }}"
24+
"ssh_port": "{{ ssh_port | default }}"
25+
"ssh_public_key_file": "{{ ssh_public_key_file | default }}"
26+
"ssh_user": "{{ ssh_user | default }}"
27+
"state_save_file": "{{ state_save_file | default }}"
28+
"vm_autostart": "{{ vm_autostart | default }}"
2529

26-
# Verifiy the correctness of the parameters.
30+
# Verify the correctness of the parameters.
2731
- name: Validate 'ssh_port' parameter
2832
assert:
2933
quiet: yes
@@ -33,7 +37,7 @@
3337
fail_msg: >
3438
You are trying to use an unsupported ssh_port value.
3539
36-
# Verifiy the correctness of the parameters.
40+
# Verify the correctness of the parameters.
3741
- name: Validate 'ansible_port' parameter
3842
assert:
3943
quiet: yes
@@ -50,25 +54,21 @@
5054
assert:
5155
quiet: yes
5256
that:
53-
- "{{ item }}"
57+
- "os_family is defined"
58+
- "os_family in os_family_support"
5459
fail_msg: >
5560
You must specify the os_family for host {{ inventory_hostname }},
5661
supported values are: {{ os_family_support }}
57-
with_items:
58-
- "os_family is defined"
59-
- "os_family in os_family_support"
6062
6163
# Ensure that the hypervisor variable has been set for current host.
6264
- name: Assert hypervisor variable
6365
assert:
6466
quiet: yes
6567
that:
66-
- "{{ item }}"
68+
- "hypervisor is defined"
69+
- "hostvars[hypervisor]['ansible_host'] is defined"
6770
fail_msg: >
6871
You must specify the hyperisor value for host {{ inventory_hostname }}
69-
with_items:
70-
- "hypervisor is defined"
71-
- "hostvars[hypervisor]['ansible_host'] is defined"
7272
7373
# Verifiy the correctness of the parameters.
7474
- name: Validate 'cpu' parameter
@@ -117,11 +117,11 @@
117117
assert:
118118
quiet: yes
119119
that:
120-
- "{{ item }}.name is defined"
121-
- "{{ item }}.type is defined"
122-
- "{{ item }}.ip is defined"
123-
- "{{ item }}.gw is defined"
124-
- "{{ item }}.dns is defined"
120+
- "item.name is defined"
121+
- "item.type is defined"
122+
- "item.ip is defined"
123+
- "item.gw is defined"
124+
- "item.dns is defined"
125125
fail_msg: >
126126
Incomplete interface declaration for {{ item }}, missing a mandatory var
127127
with_items:
@@ -132,9 +132,9 @@
132132
assert:
133133
quiet: yes
134134
that:
135-
- "{{ item }}.type == 'nat' or
136-
{{ item }}.type == 'macvtap' or
137-
{{ item }}.type == 'bridge'"
135+
- "item.type == 'nat' or
136+
item.type == 'macvtap' or
137+
item.type == 'bridge'"
138138
fail_msg: >
139139
Unsupported value for interface type.
140140
with_items:
@@ -145,7 +145,7 @@
145145
assert:
146146
quiet: yes
147147
that:
148-
- "{{ item }}.ip | ipaddr"
148+
- "item.ip | ipaddr"
149149
fail_msg: >
150150
Invalid interface ip address.
151151
when:
@@ -158,7 +158,7 @@
158158
assert:
159159
quiet: yes
160160
that:
161-
- "{{ item }}.gw | ipaddr"
161+
- "item.gw | ipaddr"
162162
fail_msg: >
163163
Invalid interface gateway address.
164164
when:
@@ -171,7 +171,7 @@
171171
assert:
172172
quiet: yes
173173
that:
174-
- "{{ item.dns }} | length > 0"
174+
- "item.dns | length > 0"
175175
fail_msg: >
176176
Incomplete interface dns specification.
177177
when:
@@ -190,11 +190,11 @@
190190
assert:
191191
quiet: yes
192192
that:
193-
- "{{ item }}.size is defined"
194-
- "{{ item }}.pool is defined"
195-
- "{{ item }}.format is defined"
196-
- "{{ item }}.mount_point is defined"
197-
- "{{ item }}.encryption is defined"
193+
- "item.size is defined"
194+
- "item.pool is defined"
195+
- "item.format is defined"
196+
- "item.mount_point is defined"
197+
- "item.encryption is defined"
198198
fail_msg: >
199199
You must specify {{ item }} when data_disks is defined
200200
with_items:
@@ -208,8 +208,8 @@
208208
assert:
209209
quiet: yes
210210
that:
211-
- "{{ item }}.size | int"
212-
- "{{ item }}.size > 0"
211+
- "item.size | int"
212+
- "item.size > 0"
213213
fail_msg: >
214214
You are trying to use an unsupported size value.
215215
with_items:
@@ -223,7 +223,7 @@
223223
assert:
224224
quiet: yes
225225
that:
226-
- "{{ item }}.pool | length > 0"
226+
- "item.pool | length > 0"
227227
fail_msg: >
228228
You are trying to use an unsupported pool value.
229229
with_items:
@@ -236,8 +236,8 @@
236236
assert:
237237
quiet: yes
238238
that:
239-
- "{{ item }}.format in {{ supported_fs }}"
240-
- "{{ item }}.format | length > 0"
239+
- "item.format in {{ supported_fs }}"
240+
- "item.format | length > 0"
241241
fail_msg: >
242242
You must specify a valid disk format for host {{ inventory_hostname }},
243243
supported values are: {{ supported_fs }}
@@ -252,8 +252,8 @@
252252
assert:
253253
quiet: yes
254254
that:
255-
- '{{ item }}.mount_point == "none" or {{ item }}.mount_point | regex_search("^/\\w+")'
256-
- "{{ item }}.mount_point | length > 0"
255+
- 'item.mount_point == "none" or item.mount_point | regex_search("^/\\w+")'
256+
- "item.mount_point | length > 0"
257257
fail_msg: >
258258
You are trying to use an unsupported mount_point value.
259259
with_items:
@@ -267,7 +267,7 @@
267267
assert:
268268
quiet: yes
269269
that:
270-
- not {{ item }}.encryption
270+
- not item.encryption
271271
fail_msg: >
272272
You are trying to use an unsupported encryption value. FreeBSD family and CloudInit images doesn't support encryption.
273273
with_items:
@@ -282,7 +282,7 @@
282282
assert:
283283
quiet: yes
284284
that:
285-
- "{{ item.encryption }} | type_debug == 'bool'"
285+
- "item.encryption | type_debug == 'bool'"
286286
fail_msg: >
287287
You are trying to use an unsupported encryption value. Possible values could be 'True' or 'False'.
288288
with_items:

roles/terrible/templates/terraform-vm.tf.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ resource "libvirt_domain" "domain_{{ inventory_hostname }}" {
4040

4141
autostart = {% if vm_autostart %}true{% else %}false{% endif %}
4242

43-
cpu = {
43+
cpu {
4444
mode = "host-passthrough"
4545
}
4646

0 commit comments

Comments
 (0)