# File selector based on variables

**URL:** <https://forum.ansible.com/t/file-selector-based-on-variables/14099>\
**Category:** Ansible Project\
**Created:** [December 8, 2013, 5:03pm UTC](https://forum.ansible.com/t/file-selector-based-on-variables/14099 "2013-12-08T17:03:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillem\_Liarte](https://avatars.discourse-cdn.com/v4/letter/g/e95f7d/32.png) [@Guillem\_Liarte](https://forum.ansible.com/u/Guillem_Liarte)\
**Post date:** [December 8, 2013, 5:03pm UTC](https://forum.ansible.com/t/file-selector-based-on-variables/14099/1 "2013-12-08T17:03:01Z")

</div>

Hello.

I try to use this:

[http://www.ansibleworks.com/docs/playbooks\_conditionals.html#selecting-files-and-templates-based-on-variables](http://www.ansibleworks.com/docs/playbooks_conditionals.html#selecting-files-and-templates-based-on-variables)

/etc/ansible/roles/common/templates# ls -lrt _some_  
-rw-------. 1 root root 750 Dec 6 11:49 hos1.some.conf.j2  
-rw-r–r–. 1 root root 700 Dec 6 12:06 some.conf.j2

- name: Make sure configuration is correct.  
template: src={{ item }} dest=/etc/some.conf  
with\_first\_found:  
files:
- ${ansible\_hostname}.some.conf.j2
- some.conf.j2  
paths:
- …/templates
- …/files  
tags:
- common
- auth

When I run:

TASK: [Make sure configuration is correct.] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [host1] =\> input file not found at /etc/ansible/roles/common/templates/None or /etc/ansible/None

FATAL: all hosts have already failed – aborting

So, the files exist, both the especific and the default. The pattern is as per given example.

I am succesfully distributing files from /etc/ansible/roles/common/files and templates /etc/ansible/roles/common/templates, that is not the issue. I tried specifying only templates is path, and also nothing.

If I change the actions to copy, it would work, but it would not be used as a template.

What am I missing here?

Many thanks in advance.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 8, 2013, 5:15pm UTC](https://forum.ansible.com/t/file-selector-based-on-variables/14099/2 "2013-12-08T17:15:41Z")

</div>

That’s definitely confusing, I agree, we should work to make this better.

Can you please file a ticket on GitHub so we can investigate?

Please include the output of “ansible --version” in the report.

Thanks!

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [December 8, 2013, 5:36pm UTC](https://forum.ansible.com/t/file-selector-based-on-variables/14099/3 "2013-12-08T17:36:54Z")

</div>

​Could this be a typo?​

/etc/ansible/roles/common/templates# ls -lrt \*some\*

> -rw-------. 1 root root 750 Dec 6 11:49 hos1.some.conf.j2

....​​

> fatal: [host1] =\> input file not found at  
> /etc/ansible/roles/common/templates/None or /etc/ansible/None

The filename seems to be missing a 't'?​

Serge

---

<div class="post-metadata">

**Author:** ![Guillem\_Liarte](https://avatars.discourse-cdn.com/v4/letter/g/e95f7d/32.png) [@Guillem\_Liarte](https://forum.ansible.com/u/Guillem_Liarte)\
**Post date:** [December 9, 2013, 8:05pm UTC](https://forum.ansible.com/t/file-selector-based-on-variables/14099/4 "2013-12-09T20:05:18Z")

</div>

Yes, it does indeed, but only because I edited this by hand to change the hostname.

Thanks for pointing it out.

---

<div class="post-metadata">

**Author:** ![Guillem\_Liarte](https://avatars.discourse-cdn.com/v4/letter/g/e95f7d/32.png) [@Guillem\_Liarte](https://forum.ansible.com/u/Guillem_Liarte)\
**Post date:** [December 9, 2013, 8:15pm UTC](https://forum.ansible.com/t/file-selector-based-on-variables/14099/5 "2013-12-09T20:15:48Z")

</div>

Michael,

Done: [https://github.com/ansible/ansible/issues/5227](https://github.com/ansible/ansible/issues/5227)

In the meantime I will stick to flat files, as it seems to happen only when using it with templates, although the way I understand it, it should work exactly the same way.

---

<div class="post-metadata">

**Author:** ![Guillem\_Liarte](https://avatars.discourse-cdn.com/v4/letter/g/e95f7d/32.png) [@Guillem\_Liarte](https://forum.ansible.com/u/Guillem_Liarte)\
**Post date:** [January 18, 2014, 12:13pm UTC](https://forum.ansible.com/t/file-selector-based-on-variables/14099/6 "2014-01-18T12:13:37Z")

</div>

Hello again,

I am still experiencing issues with this. I reply first here in case what I am doing is blatantly evident.

Since then, this issue was closed and new documentation produced:

[https://github.com/ansible/ansible/issues/5227](https://github.com/ansible/ansible/issues/5227)

[http://docs.ansible.com/playbooks\_loops.html?highlight=loops#finding-first-matched-files](http://docs.ansible.com/playbooks_loops.html?highlight=loops#finding-first-matched-files)

This is good, however, I keep experiencing something undesirable.

So this is Centos 6.5, Ansible 1.4.3-1

This is to distribute a configuration that may be different in function of the host type. The reason does not matter.

In the templates directory:

…/roles/common/templates$ ls -lrt _sssd_  
-rw-rw-r-- 1 guillem guillem 723 Jan 12 21:25 sssd.conf.j2  
-rw-rw-r-- 1 guillem guillem 789 Jan 18 12:53 ndoluxel001.sssd.conf.j2

In the playbook:

- name: Make sure SSSD configuration is correct.  
template: src={{ item }} dest=/etc/sssd/sssd.conf  
with\_first\_found:
- “{{ inventory\_hostname }}.sssd.conf.j2”
- sssd.conf.j2

This is exactly as shown in the documentation:

[http://docs.ansible.com/playbooks\_loops.html?highlight=loops#finding-first-matched-files](http://docs.ansible.com/playbooks_loops.html?highlight=loops#finding-first-matched-files)

> - name: INTERFACES | Create Ansible header for /etc/network/interfaces template: src={{ item }} dest=/etc/foo.conf with\_first\_found: - “{{ansible\_virtualization\_type}\_foo.conf” - “default\_foo.conf”

```auto

```

However when I run the play book:

ansible-playbook /etc/ansible/common.yml --tags=“auth” -k  
SSH password:

PLAY [all] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [ndoluxel001]  
ok: [ndoandel001]  
ok: [ndoluxel002]

TASK: [common | Make sure SSSD configuration is correct.] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [ndoluxel001] =\> input file not found at /etc/ansible/roles/common/templates/None or /etc/ansible/None  
fatal: [ndoluxel002] =\> input file not found at /etc/ansible/roles/common/templates/None or /etc/ansible/None  
fatal: [ndoandel001] =\> input file not found at /etc/ansible/roles/common/templates/None or /etc/ansible/None

FATAL: all hosts have already failed – aborting

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
to retry, use: --limit @/root/common.retry

ndoandel001 : ok=1 changed=0 unreachable=1 failed=0  
ndoluxel001 : ok=1 changed=0 unreachable=1 failed=0  
ndoluxel002 : ok=1 changed=0 unreachable=1 failed=0

What would I expect:

ndoluxel001 to obtain configuration from template ndoluxel001.sssd.conf.j2  
The others to obtain the default sssd.conf.j2 template.

Things I have tried already:

- The way [jctanner](https://github.com/jctanner) proposes me to fix it, which did not work, actually complains of a syntax error.
- Use underscore instead of dots.
- Qualifying the search paths, relative and absolute. Nothing.
- Switching verbosity, not very cool as it gives me things I do not need and still I do not see what is going on behind the curtain:

TASK: [common | Make sure SSSD configuration is correct.] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
 ESTABLISH CONNECTION FOR USER: root on PORT 22 TO ndoluxel001  
 ESTABLISH CONNECTION FOR USER: root on PORT 2022 TO ndoandel001  
 ESTABLISH CONNECTION FOR USER: root on PORT 22 TO ndoluxel002  
 EXEC /bin/sh -c ‘mkdir -p $HOME/.ansible/tmp/ansible-1390047005.86-11603493989537 && echo $HOME/.ansible/tmp/ansible-1390047005.86-11603493989537’  
fatal: [ndoluxel001] =\> input file not found at /etc/ansible/roles/common/templates/None or /etc/ansible/None  
 EXEC /bin/sh -c ‘mkdir -p $HOME/.ansible/tmp/ansible-1390047006.29-141184342231390 && echo $HOME/.ansible/tmp/ansible-1390047006.29-141184342231390’  
 EXEC /bin/sh -c ‘mkdir -p $HOME/.ansible/tmp/ansible-1390047006.45-261013738830940 && echo $HOME/.ansible/tmp/ansible-1390047006.45-261013738830940’  
fatal: [ndoluxel002] =\> input file not found at /etc/ansible/roles/common/templates/None or /etc/ansible/None  
fatal: [ndoandel001] =\> input file not found at /etc/ansible/roles/common/templates/None or /etc/ansible/None

I really hope that this is something simple that I am missing.

Many thanks in advance.
