Skip to content

Commit 2437f9e

Browse files
committed
Add various_vars cifmw_helpers tasks
In some places, we used to have variables like: cifmw_extras which can have files or dictionaries. Let's parse these variables and set as fact correctly. Signed-off-by: Daniel Pawlik <[email protected]>
1 parent 530ca37 commit 2437f9e

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

roles/cifmw_helpers/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,25 @@ That code, can be replaced by:
132132
- logs
133133
```
134134

135+
#### Read var file and set as fact
136+
137+
Example task execution:
138+
139+
```yaml
140+
- name: Read base centos-9 scenarios
141+
vars:
142+
provided_file: >
143+
{{ ansible_user_dir }}/src/github.com/openstack-k8s-operators/
144+
ci-framework/scenarios/centos-9/base.yml
145+
ansible.builtin.include_role:
146+
name: cifmw_helpers
147+
tasks_from: var_file.yml
148+
```
149+
135150
Of course, before Zuul execute the playbook, it is mandatory to call `playbooks/cifmw_collection_zuul_executor.yml`.
136151

152+
#### Read directory and parse all files and then set as fact
153+
137154
For setting all files in the directory as fact, use `var_dir.yml` tasks.
138155
Example:
139156

@@ -147,3 +164,29 @@ Example:
147164
name: cifmw_helpers
148165
tasks_from: var_dir.yml
149166
```
167+
168+
#### Set as fact various variables
169+
170+
In some places in our workflow, we can have a list that contains
171+
various variables like files: "@some_file.yml" or dictionaries like "some: var".
172+
To parse them and set as a fact, use `various_vars.yml` task file.
173+
174+
```yaml
175+
- name: Example
176+
hosts: localhost
177+
tasks:
178+
- name: Test various vars
179+
vars:
180+
various_vars:
181+
- "@scenarios/centos-9/base.yml"
182+
- test: ok
183+
ansible.builtin.include_role:
184+
name: cifmw_helpers
185+
tasks_from: various_vars.yml
186+
187+
- name: Print parsed variables
188+
ansible.builtin.debug:
189+
msg: |
190+
"Value for file is: {{ cifmw_repo_setup_os_release }}"
191+
"Value for dict is: {{ test }}"
192+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
# various_vars
3+
- name: Filter Ansible variable files and set as fact
4+
vars:
5+
provided_file: "{{ item | replace('@','') }}"
6+
ansible.builtin.include_tasks: var_file.yml
7+
loop: "{{ various_vars | select('match', '^@.*\\.(yml|yaml)$') | list }}"
8+
9+
- name: Filter just dict and set as fact
10+
ansible.builtin.set_fact:
11+
"{{ item.key }}": "{{ item.value }}"
12+
cacheable: true
13+
loop: "{{ (various_vars | select('mapping') | list) | map('dict2items') | flatten }}"

0 commit comments

Comments
 (0)