file ... with_first_found uses unexpected value

Hi all,

I have a piece of code that gives an unexpected error:

--- snip ---
- name: link the proper certificate issue directory
  file:
    src: '{{ item }}'
    dest: '/etc/mycert/current'
    state: link
    follow: yes
  with_first_found:
    - "/etc/mycert/von_2020"
    - "/etc/mycert/von_2019"
    - "/etc/mycert/von_2018"
    - "/etc/mycert/von_2017"
    - "/etc/mycert/von_2016"
    - "/etc/mycert/von_2015"
    - "/etc/mycert/von_2014_09"
    - "/etc/mycert/von_2014"
  when: ( facter_mycertdir != '/etc/mycert/current' )
--- pins ---

When I look at /etc/mycert/, I see a subdirectory "von_2018", and I
expect the file module to create a symlink from "/etc/mycert/von_2018"
to '/etc/mycert/current'. Instead, I get an error:

--- snip ---
TASK [my.certs : link the proper certificate issue directory]

with_first_found always do lookup on localhost not the remote host.
To get files on remote you need to use the find module.

Kai Stian Olstad [27.03.2018 12:17]:

Hi all,

I have a piece of code that gives an unexpected error:

--- snip ---
- name: link the proper certificate issue directory
  file:
    src: '{{ item }}'
    dest: '/etc/mycert/current'
    state: link
    follow: yes
  with_first_found:
    - "/etc/mycert/von_2020"
    - "/etc/mycert/von_2019"

[...]

The "von_2016" would be correct on the host I use as ansible master,
but not on the host (sapvhrq) where the play is executed.

What do I do wrong here?

with_first_found always do lookup on localhost not the remote host.
To get files on remote you need to use the find module.

Oh, thanks, I didn't see this mentioned on the
<http://docs.ansible.com/ansible/latest/plugins/lookup/first_found.html&gt;
page.

I see how I can use the find module. But how can I get the "latest"
directory from the find module?

Do I have to split into several "stat" statements, one per year,
beginning with 2020? Plus one "file" statement for each stat, to create
the desired symlink?

Since the "/etc/mycert" directory is a symlink too, pointing to a shared
filesystem, I try to use other variable, it works here, but not all of
my hosts can share this filesystem.

As dest, I use "/clusterdisk/sapdirs/{{ facter_myhostname }}/current",
and the with_first_found list now reads like

    - "/clusterdisk/sapdirs/{{ facter_myhostname }}/von_2020"
    - "/clusterdisk/sapdirs/{{ facter_myhostname }}/von_2019"

Regards,
Werner

Kai Stian Olstad [27.03.2018 12:17]:

with_first_found always do lookup on localhost not the remote host.
To get files on remote you need to use the find module.

Oh, thanks, I didn't see this mentioned on the
<http://docs.ansible.com/ansible/latest/plugins/lookup/first_found.html&gt;
page.

This apply to all lookups and template so it documented on main page
http://docs.ansible.com/ansible/latest/plugins/lookup.html
"Like all templating, these plugins are evaluated on the Ansible control machine, not on the target/remote."

I see how I can use the find module. But how can I get the "latest"
directory from the find module?

Do I have to split into several "stat" statements, one per year,
beginning with 2020? Plus one "file" statement for each stat, to create
the desired symlink?

Since you can sort on name this will find newest entry

   - find:
       paths: /tmp
     register: r

   - debug: msg="{{ r.files | map(attribute='path') | list | sort | last }}"

Since the "/etc/mycert" directory is a symlink too, pointing to a shared
filesystem, I try to use other variable, it works here, but not all of
my hosts can share this filesystem.

I think you can use find module on a symlink.