Reputation: 9505
Here is a structure of variables I'd like to work with from packages.yml
file:
---
- name: Some description 1,
package: package1
- name: Some description 1,
package: package2
My expectation was, that I import the file and store the list in the packages variable:
- name: Import packages vars
include_vars:
file: packages.yml
name: packages
Then I want to print names only:
- name: iteration
debug:
msg: "name: {{ item.name }}"
with_items: "{{ packages }}"
But get error:
TASK [Import packages vars] **************************************************** fatal: [default]: FAILED! => {"ansible_facts": {"packages": {}}, "ansible_included_var_files": [], "changed": false, "message": "/vagrant/packages.yml must be stored as a dictionary/hash"}
The original purposes I try to get:
1st. Simplify properties definition, by declaring a list of items without a name of this list. I mean, I do not want to define them in external file as:
---
- packages:
- name: Some description 1,
package: package1
- name: Some description 1,
package: package1
Although this can be solved easily without include_vars, but via vars_files directive:
vars_files:
- packages.yml
2nd. Having an unnamed list of items in yml file, I want in the playbook itself give it a name. That's what I try to do in the "Import packages vars" task via "packages
" in the playbook
- name: Import packages vars
include_vars:
file: packages.yml
name: packages
By this way, I move a responsibility of naming a list, to its consumer. List itself should not worry about how it is named by its clients.
If it is possible, how can I get it done?
Upvotes: 0
Views: 1073
Reputation: 2105
As the error message says, the contents of packages.yml
must be a dictionary or hash, not a list at the top level.
You could assign the list to a placeholder variable within the file and then assign that value to another variable within your playbook using the set_fact
module.
packages.yml
:
list:
- name: Some description 1,
package: package1
- name: Some description 1,
package: package2
Playbook:
- include_vars:
file: packages.yml
name: included_vars
- set_fact:
packages: "{{ include_vars.list }}"
Although, given that the file is named packages.yml
, packages
would make sense as a top level variable name.
Upvotes: 0