Skip to content

Commit 52784b6

Browse files
danpawlikevallesp
authored andcommitted
Add loop_var in cifmw_helpers task files; trim variables
To avoid unecessary conflicts issue where "item" was already used in higher task, let's add loop_var to have better control on the vars what are parsed. Also trim variables in var_dir and var_file and improve finding files to parse in var_dir helper. Signed-off-by: Daniel Pawlik <[email protected]>
1 parent 94fa1e5 commit 52784b6

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
---
22
- name: Set files as fact
3-
when: "'content' in item"
3+
when: "'content' in dir_item"
44
ansible.builtin.set_fact:
5-
"{{ item.key }}": "{{ item.value }}"
5+
"{{ _file_content.key }}": "{{ _file_content.value }}"
66
cacheable: true
7-
loop: "{{ item['content'] | b64decode | from_yaml | dict2items }}"
7+
loop: "{{ dir_item['content'] | b64decode | from_yaml | dict2items }}"
8+
loop_control:
9+
loop_var: _file_content
810
no_log: "{{ cifmw_helpers_no_log }}"

roles/cifmw_helpers/tasks/var_dir.yml

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,38 @@
44
# In that case, include_vars would not work.
55
- name: Check directory is available
66
ansible.builtin.stat:
7-
path: "{{ provided_dir }}"
7+
path: "{{ provided_dir | trim }}"
88
register: param_dir
99

1010
- name: List files available in dir and parse
1111
when: param_dir.stat.exists
1212
block:
13-
- name: List available files
14-
ansible.builtin.command: |
15-
ls {{ provided_dir }}
16-
register: _param_dir
13+
- name: Find yaml files
14+
ansible.builtin.find:
15+
paths: "{{ provided_dir | trim }}"
16+
patterns: "*.yml,*.yaml"
17+
file_type: file
18+
recurse: false
19+
register: _yaml_files
20+
21+
- name: Print available yaml files
22+
ansible.builtin.debug:
23+
msg: |
24+
Found yaml files to parse: {{ _yaml_files.files | map(attribute='path') | list }}
1725
1826
- name: Read vars
1927
ansible.builtin.slurp:
20-
src: "{{ provided_dir }}/{{ item }}"
21-
register: _parsed_vars
22-
loop: "{{ _param_dir.stdout_lines }}"
28+
src: "{{ _file_to_parse.path }}"
29+
loop: "{{ _yaml_files.files }}"
30+
loop_control:
31+
loop_var: _file_to_parse
2332
no_log: "{{ cifmw_helpers_no_log }}"
33+
register: _parsed_vars
2434

2535
- name: Call task to parse all files as fact
2636
ansible.builtin.include_tasks:
2737
file: set_dir_facts.yml
28-
loop: '{{ _parsed_vars["results"] }}'
38+
loop: "{{ _parsed_vars['results'] }}"
39+
loop_control:
40+
loop_var: dir_item
2941
no_log: "{{ cifmw_helpers_no_log }}"
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
---
2+
- name: Fail if file is not yaml or yml extension
3+
ansible.builtin.fail:
4+
msg: "File needs to be yaml/yml extension"
5+
when:
6+
- provided_file | trim | regex_search('\.(yml|yaml)$') == None
7+
28
- name: Check if file is available
39
ansible.builtin.stat:
4-
path: "{{ provided_file }}"
10+
path: "{{ provided_file | trim }}"
511
register: _param_file
612

713
- name: Read vars
814
when: _param_file.stat.exists
915
ansible.builtin.slurp:
10-
src: "{{ provided_file }}"
16+
src: "{{ provided_file | trim }}"
1117
register: _parsed_vars
1218
no_log: "{{ cifmw_helpers_no_log }}"
1319

1420
- name: Set vars as fact
1521
when: "'content' in _parsed_vars"
1622
ansible.builtin.set_fact:
17-
"{{ item.key }}": "{{ item.value }}"
23+
"{{ file_item.key }}": "{{ file_item.value }}"
1824
cacheable: true
1925
loop: "{{ _parsed_vars['content'] | b64decode | from_yaml | dict2items }}"
2026
no_log: "{{ cifmw_helpers_no_log }}"
27+
loop_control:
28+
loop_var: file_item

roles/cifmw_helpers/tasks/various_vars.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
# various_vars
33
- name: Filter Ansible variable files and set as fact
44
vars:
5-
provided_file: "{{ item | replace('@','') }}"
5+
provided_file: "{{ various_file_item | replace('@','') }}"
66
ansible.builtin.include_tasks: var_file.yml
77
loop: "{{ various_vars | select('match', '^@.*\\.(yml|yaml)$') | list }}"
8+
loop_control:
9+
loop_var: various_file_item
810

911
- name: Filter just dict and set as fact
1012
ansible.builtin.set_fact:
11-
"{{ item.key }}": "{{ item.value }}"
13+
"{{ various_item.key }}": "{{ various_item.value }}"
1214
cacheable: true
1315
loop: "{{ (various_vars | select('mapping') | list) | map('dict2items') | flatten }}"
16+
loop_control:
17+
loop_var: various_item

0 commit comments

Comments
 (0)