I'm trying to setup a hierarchical set of variable files so that different
teams can setup value, permitting more specific instances to override
generic ones. The key is that the more specific instances may not be
defined in all cases. I thought I solved it with a combination of
"include_vars" and "with_items" and using "ignore_errors" to skip a missing
file.
But, when any of the include files are missing the entire set of included
values are lost. It appears that any ignored failure in the include_vars
deletes the entire data structure when it continues on.
Here's a sample playbook and variable files. (For what it's worth this is
Ansible 2.2.0.0 on RHEL 6)
*teststackvars.yml*
#!/usr/bin/env ansible-playbook
# Run like this:
# ANSIBLE_HASH_BEHAVIOUR=merge ./teststackvars.yml -i localhost, -l
localhost --check
# or set hash behavior in ansible.cfg and run without variable.
---
- hosts: all
gather_facts: False
tasks:
- name: Include the variables in precedence
include_vars:
file: "{{ item }}"
name: myvars
with_items:
- info_a.yml
- info_b.yml
- info_{{ inventory_hostname }}.yml
ignore_errors: True
- debug:
msg: "{{ myvars }}"
And these three vars files:
*vars/info_a.yml*
---
var_from_info_a: from info_a.yml
some_var: value from info_a
*vars/info_b.yml*
---
var_from_info_b: from info_b.yml
some_var: value from info_b
*vars/info_localhost.yml*
---
var_from_info_localhost: from info_localhost.yml
some_var: value from info_localhost
When I run it with all files defined it works just fine:
*$ ./teststackvars.yml -i localhost, -l localhost --check*
*PLAY [all]
**********************************************************************
*TASK [Include the variables in precedence]
**************************************
*ok: [localhost] => (item=info_a.yml)*
*ok: [localhost] => (item=info_b.yml)*
*ok: [localhost] => (item=info_localhost.yml)*
*TASK [debug]
********************************************************************
*ok: [localhost] => {*
* "msg": {*
* "some_var": "value from info_localhost",*
* "var_from_info_a": "from info_a.yml",*
* "var_from_info_b": "from info_b.yml",*
* "var_from_info_localhost": "from info_localhost.yml"*
* }*
*}*
*PLAY RECAP
**********************************************************************
*localhost : ok=2 changed=0 unreachable=0
failed=0*
That looks good - all the unique variables from info_a/b/localhost are
defined, and the common "some_var" is overwritten by the last file.
When I rename one of the YML files, the entire "myvars" variable structure
goes away:
*$ mv vars/info_localhost.yml vars/info_localhost.yml.disabled*
*$ ./teststackvars.yml -i localhost, -l localhost --check*
*PLAY [all]
**********************************************************************
*TASK [Include the variables in precedence]
**************************************
*ok: [localhost] => (item=info_a.yml)*
*ok: [localhost] => (item=info_b.yml)*
*fatal: [localhost]: FAILED! => {"failed": true, "msg": "Unable to find
'info_localhost.yml' in expected paths."}*
*...ignoring*
*TASK [debug]
********************************************************************
*fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args'
has an invalid value, which appears to include a variable that is
undefined. The error was: 'myvars' is undefined\n\nThe error appears to
have been in '/home/dan/teststackvars/teststackvars.yml': line 20, column
5, but may\nbe elsewhere in the file depending on the exact syntax
problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^
here\n"}*
* to retry, use: --limit
@/home/dan/teststackvars/teststackvars.retry*
*PLAY RECAP
**********************************************************************
*localhost : ok=1 changed=0 unreachable=0
failed=1*
*$*
I searched the group and Google in general for "with_items" and
"include_vars" but didn't see anything that seems to pertain to this.
Shouldn't this work - at least let the variables that were in the "myvars"
stay in-spite of the missing variable file?
Thanks,
Dan
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/5993f7b5-764c-4276-81e2-e11eb27d57e1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.