Reputation: 3065
My ansible playbook files is test.yml.
In the same location i have three files having variables i.e app_base_vars.yaml, web_base_vars.yaml & web_bran_vars.yml
Sample contents of one of the variable files:
more app_base_vars.yaml
10.8.45.192: /opt/logs/cell17
10.8.87.165: /opt/logs/cell23
....
....
When i run the playbook as:
ansible-playbook /app/Ansible/playbook/test.yml -e "USER=wasadm Dest_IP=10.0.21.111,10.9.17.113,10.8.19.114 Layer=web_base"
I get the following error output:
[WARNING]: provided hosts list is empty, only localhost is available.
Note that the implicit localhost does not match 'all'
PLAY [Find the details here 10.0.21.111,10.9.17.113,10.8.19.114 and
wasadm]
*************************************************************************************************
TASK [add_host]
************************************************************************************************************************************************************* changed: [localhost] => (item=10.0.21.111) changed: [localhost] =>
(item=10.9.17.113) changed: [localhost] => (item=10.8.19.114)
TASK [Load a variable file based on the options the user has selected
i.e. "web_base_*.yaml."]
**************************************************************************** fatal: [localhost]: FAILED! => {"ansible_facts": {},
"ansible_included_var_files": [], "changed": false, "message": "Could
not find or access 'web_base_*.yaml'\nSearched
in:\n\t/app/Ansible/playbook/vars/web_base_*.yaml\n\t/app/Ansible/playbook/web_base_*.yaml\n\t/app/Ansible/playbook/vars/web_base_*.yaml\n\t/app/Ansible/playbook/web_base_*.yaml
on the Ansible Controller.\nIf you are using a module and expect the
file to exist on the remote, see the remote_src option"}
to retry, use: --limit @/app/Ansible/playbook/test.retry
PLAY RECAP
******************************************************************************************************************************************************************** localhost : ok=1 changed=1 unreachable=0
failed=1
My playbook test.yml is as below:
---
- name: "Find the details here {{ Dest_IP }} and {{ USER }}"
hosts: localhost
gather_facts: no
tasks:
- add_host: name={{ item }}
groups=dest_nodes
ansible_user={{ USER }}
with_items: "{{ Dest_IP.split(',') }}"
- name: Load a variable file based on the options the user has selected i.e. "{{ Layer }}_*.yaml."
include_vars: "{{ Layer }}_*.yaml"
register: include_vars_result
- name: Display included Files
debug:
var: include_vars_result
I have two questions:
I know that there are other solutions to including files based on variables like importing files; however I wish to debug and resolve this error and get this to work.
In the output you can see
/app/Ansible/playbook/vars/web_base_*.yaml
Why is ansible also looking for the files under "vars" directory when i have not asked it to.
Upvotes: 0
Views: 4494
Reputation: 140
Use of wildcard I think is not actually supported here, it actually looks for a filename with a star in it. At least I have never seen any example like that. Would rather look into having several files under the same directory and then include all those. See the options you got in the doc:
https://docs.ansible.com/ansible/latest/modules/include_vars_module.html
Why ansible is looking in more directories than you expected is written in the doc above for the different parameters.
Cheers!
UPDATED 2019-07-30
Working example:
test.yml:
---
- hosts: localhost
vars:
layer: web_base
tasks:
- include_vars:
dir: .
files_matching: "{{ layer }}_.+[.]yml"
- debug:
var: myvar
web_base_var.yml
myvar: testing 1 2 3...
Output during run:
$ ansible-playbook test.yml
PLAY [localhost] *******************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [localhost]
TASK [include_vars] ****************************************************************************************************
ok: [localhost]
TASK [debug] ***********************************************************************************************************
ok: [localhost] => {
"myvar": "testing 1 2 3..."
}
PLAY RECAP *************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Upvotes: 1
Reputation: 3065
This is how I got it to work :)
include_vars:
file: "{{ item }}"
with_fileglob:
- "{{ Layer }}_*.yaml"
My wildcard format was fine. Thank you for the help !!
Upvotes: 1