iterate with_file contents ?

20 views
Skip to first unread message

Nicolas G

unread,
Dec 6, 2018, 2:12:30 AM12/6/18
to Ansible Project
I have some files under my role path dir :

ls roles/my-role/files/
file1 file2 file3

I can display the content of the file by running :
- name: display contents

  debug
:
    msg
: "{{ item }}"
  with_file
:
   
- "roles/my-role/files/file1"


if in the last line I try something like :
  with_file:    
    - "roles/my-role/files/*"

I get an error :
fatal: [localhost]: FAILED! => {"msg": "could not locate file in lookup: roles/my-role/files/*"}


Is there a way I can iterate inside the files directory and get the content ? 

PS: I need the files content and note a list of the file names.

Kai Stian Olstad

unread,
Dec 6, 2018, 4:00:04 AM12/6/18
to ansible...@googlegroups.com
On 06.12.2018 08:12, Nicolas G wrote:
> I have some files under my role path dir :
>
> ls roles/my-role/files/
> file1 file2 file3
>
> I can display the content of the file by running :
> - name: display contents
>
> debug:
> msg: "{{ item }}"
> with_file:
> - "roles/my-role/files/file1"
>
>
> if in the last line I try something like :
> with_file:
> - "roles/my-role/files/*"
>

You need to use the fileglob, so something like this

- debug: msg="{{ lookup('file', item) }}"
with_fileglob: roles/my-role/files/*

--
Kai Stian Olstad

Nicolas G.

unread,
Dec 6, 2018, 10:55:20 AM12/6/18
to ansible...@googlegroups.com
worked like a charm , thank you so much Kai Stian !

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/takAhjge3MA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0c42dbb2fbffd2b8e293c6e8051203cf%40olstad.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages