file ... with_first_found uses unexpected value

55 views
Skip to first unread message

Werner Flamme

unread,
Mar 27, 2018, 5:45:09 AM3/27/18
to Ansible Project
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]
***************************************************************************************************************************************************
task path:
/srv/nfs4/ersatz/sapdirs/etc/ansible/roles/my.certs/tasks/main.yml:59
failed: [sapvhrq] (item=/etc/mycert/von_2016) => {"changed": false,
"item": "/etc/mycert/von_2016", "msg": "src file does not exist, use
\"force=yes\" if you really want to create the link:
/etc/mycert/von_2016", "path": "/etc/ufzcert/current", "src":
"/etc/mycert/von_2016", "state": "absent"}


--- pins ---

So, the file module operate with the non-existing "von_2016"
subdirectory instead of using the correct "von_2018".

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?

Regards,
Werner

--


Kai Stian Olstad

unread,
Mar 27, 2018, 6:17:16 AM3/27/18
to ansible...@googlegroups.com
On 27.03.2018 11:44, Werner Flamme wrote:
> 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 ---
>
> So, the file module operate with the non-existing "von_2016"
> subdirectory instead of using the correct "von_2018".
>
> 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.

--
Kai Stian Olstad

Werner Flamme

unread,
Mar 27, 2018, 7:09:35 AM3/27/18
to ansible...@googlegroups.com
Kai Stian Olstad [27.03.2018 12:17]:
> On 27.03.2018 11:44, Werner Flamme wrote:
>> 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>
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

unread,
Mar 27, 2018, 8:28:39 AM3/27/18
to ansible...@googlegroups.com
On 27.03.2018 13:09, Werner Flamme wrote:
> 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>
> 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.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages