# strange error message from file lookup plugin

**URL:** <https://forum.ansible.com/t/strange-error-message-from-file-lookup-plugin/28740>\
**Category:** Ansible Project\
**Created:** [October 8, 2018, 8:48am UTC](https://forum.ansible.com/t/strange-error-message-from-file-lookup-plugin/28740 "2018-10-08T08:48:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![christian.tremel](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@christian.tremel](https://forum.ansible.com/u/christian.tremel)\
**Post date:** [October 8, 2018, 8:48am UTC](https://forum.ansible.com/t/strange-error-message-from-file-lookup-plugin/28740/1 "2018-10-08T08:48:30Z")

</div>

until now i believed that the file lookup only bites when the file is available (ssh key in this example), but instead it fails if it not exists. any hints on the reason of this strange behaviour?

error message:

`  
TASK [manage\_ldap\_users : authorized\_key] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
[WARNING]: Unable to find ‘files/keys/kwo.key.pub’ in expected paths (use -vvvvv to see paths)

fatal: [TESTHOST]: FAILED! =\> {}

MSG:

An unhandled exception occurred while running the lookup plugin ‘file’. Error was a \<class ‘ansible.errors.AnsibleError’\>, original message: could not locate file in lookup: files/keys/user2.key.pub  
Enter code here…  
`

play:

`

- block:

- name: enable aml users  
blockinfile:  
dest: /etc/security/user  
backup: no  
marker: “\*\*\* {mark} \*\*\*”  
marker\_begin: “BEGIN - ADDED WITH ANSIBLE - {{ item.name }}”  
marker\_end: “END - ADDED WITH ANSIBLE - {{ item.name }}”  
state: “{{ item.state }}”  
block: |  
{{ item.name }}:  
SYSTEM = “LDAP”  
registry = LDAP  
with\_items: “{{ aml\_users }}”

- name: prepare homes  
include\_tasks: “{{ role\_path }}/tasks/create\_homes.yml”  
with\_items: “{{ aml\_users }}”  
when: create\_aml\_users.stat.exists  
Enter code here…  
`

included task:

`

- shell: “lsuser -R LDAP -a pgrp {{ item.name }} | awk ‘{print $2}’ | cut -d= -f2”  
register: primary\_group  
failed\_when: false  
changed\_when: false

- shell: “lsuser -R LDAP -a home {{ item.name }} | awk -F= ‘{print $2}’”  
register: home\_dir  
failed\_when: false  
changed\_when: false

- file:  
path: ‘{{ home\_dir.stdout | default(“/home/”+item.name) }}’  
state: directory  
owner: “{{ item.name }}”  
group: ‘{{ primary\_group.stdout | default(item.name) }}’  
when: “item.state == ‘present’”

- file:  
path: ‘{{ home\_dir.stdout | default(“/home/”+item.name) }}’  
state: absent  
owner: “{{ item.name }}”  
group: ‘{{ primary\_group.stdout | default(item.name) }}’  
when: “item.state == ‘absent’”

- authorized\_key:  
user: “{{ item.name }}”  
state: “{{ item.state }}”  
key: “{{ lookup(‘file’, ‘files/keys/’+ item.name + ‘.key.pub’) }}”  
when: “item.state == ‘present’”  
Enter code here…  
`

vars file structure:

`  
aml\_users:

- name: “user1”  
comment: “some name”  
state: present
- name: “user2”  
comment: “some name”  
state: present
- name: “user3”  
comment: “some name”  
state: present  
…

`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 9, 2018, 6:30pm UTC](https://forum.ansible.com/t/strange-error-message-from-file-lookup-plugin/28740/2 "2018-10-09T18:30:44Z")

</div>

I'm not sure i understand exactly what you are expecting, but in newer  
version of ansible you can control lookup errors

[https://docs.ansible.com/ansible/latest/plugins/lookup.html#using-lookup-plugins](https://docs.ansible.com/ansible/latest/plugins/lookup.html#using-lookup-plugins)

---

<div class="post-metadata">

**Author:** ![christian.tremel](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@christian.tremel](https://forum.ansible.com/u/christian.tremel)\
**Post date:** [October 10, 2018, 7:35am UTC](https://forum.ansible.com/t/strange-error-message-from-file-lookup-plugin/28740/3 "2018-10-10T07:35:51Z")

</div>

well, dumbass me was absolutly shure that the file lookup only occurs if the named file exists…so the error came from the authorized\_key module not from the lookup plugin.

the following fixed it…

`

- stat:  
path: “{{ role\_path+‘/files/keys/’+item.name+‘.key.pub’ }}”  
register: has\_key  
delegate\_to: 127.0.0.1

- authorized\_key:  
user: “{{ item.name }}”  
state: present  
key: “{{ lookup(‘file’, ‘files/keys/’+item.name+‘.key.pub’) }}”  
when: (item.state == ‘present’) and  
(has\_key.stat.exists == True)

`
