include_vars in ansible 2.0

607 views
Skip to first unread message

Marcus Furlong

unread,
Feb 18, 2016, 12:20:20 PM2/18/16
to Ansible Project
Hi,

With ansible 2.0, should the following use of include_vars give the error below?

# tree
├── roles
│   └── common
│       ├── tasks
│       │   └── main.yml
│       └── vars
│           ├── Debian.yml
│           └── default.yml

# cat roles/common/tasks/main.yml 
- include_vars: "{{ item }}"
 with_first_found:
   - "{{ ansible_os_family }}.yml"
   - "default.yml"

- package: name={{ bindutils_package }} state=present

# cat roles/common/vars/default.yml
bindutils_package: bind-utils

# cat roles/common/vars/Debian.yml
bindutils_package: dnsutils

ansible-playbook -i hosts site.yml 

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [127.0.0.1]

TASK [include] *****************************************************************
included: /tmp/ansible-test/roles/common/tasks/main.yml for 127.0.0.1

TASK [include_vars] ************************************************************
fatal: [127.0.0.1]: FAILED! => {"failed": true, "msg": "ERROR! No file was found when using with_first_found. Use the 'skip: true' option to allow this task to be skipped if no files are found"}

PLAY RECAP *********************************************************************
127.0.0.1                  : ok=2    changed=0    unreachable=0    failed=1

Using strace, I can see it tries to access /tmp/vars/default.yml. This seems to be because roledir = None in lib/ansible/plugins/lookup/first_found.py

        roledir = variables.get('roledir')

Is this the expected behaviour or am I using the module incorrectly?

Regards,
Marcus.




Brian Coca

unread,
Feb 24, 2016, 10:48:21 PM2/24/16
to ansible...@googlegroups.com
it is not an issue with the module, but with first found, which always looks in files/ not in vars/

​this should work:​

 with_first_found: 
   -
​"vars/
{{ ansible_os_family }}.yml" 
   - "
​vars/​
default.yml" 


--
----------
Brian Coca

Marcus Furlong

unread,
Feb 25, 2016, 2:44:23 AM2/25/16
to Ansible Project


On Thursday, 25 February 2016 14:48:21 UTC+11, Brian Coca wrote:
it is not an issue with the module, but with first found, which always looks in files/ not in vars/

​this should work:​

 with_first_found: 
   -
​"vars/
{{ ansible_os_family }}.yml" 
   - "
​vars/​
default.yml" 

Unfortunately, that fails with the same error.

It looks like with_first_found now tries all directories:

                if roledir is not None:
                   # check the templates and vars directories too,if they exist
                   for subdir in ('templates', 'vars', 'files'):
                       path = self._loader.path_dwim_relative(roledir, subdir, fn)
                       if os.path.exists(path):
                           return [path]

               # if none of the above were found, just check the
               # current filename against the current dir
               path = self._loader.path_dwim(fn)
               if os.path.exists(path):
                   return [path]

However, 'roledir' = None for some reason, so we only check for the file in the current working directory.

Cheers,
Marcus.

Rania Talbi

unread,
Mar 19, 2017, 6:29:53 PM3/19/17
to Ansible Project
Hello, i was wondaring if u guys have found a solution for this issue
Reply all
Reply to author
Forward
0 new messages