File selector based on variables

Hello.

I try to use this:

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.

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!

​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

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

Thanks for pointing it out.

Michael,

Done: 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.

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

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

  • 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”

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 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.