Re: [ansible-project] using wildcards to fetching src files

8,708 views
Skip to first unread message

Michael DeHaan

unread,
Oct 29, 2012, 4:03:32 PM10/29/12
to ansible...@googlegroups.com
Ansible 0.8 contains "with_fileglob", which does what you want

action: modulename src=$item <other arguments>
with_fileglob: /data_dump_*.sql

It would send over all files matching the glob from the local server
in that case.

Looks like that will do exactly what you want.

--Michael

On Mon, Oct 29, 2012 at 3:56 PM, Jarrett Chisholm
<jarrett...@gmail.com> wrote:
> Hi all,
>
> I'm trying to copy a files down from one of our hosts, and it contains a
> timestamp in the name. This, unfortunately, is going to be hard to change
> for us (if at all).
>
> Is there a way in ansible to have a source file with a wildcard?
>
> i.e.:
>>
>> action: fetch src=/data_dump_*.sql dest=/var/lib/ansible/files/fetched
>
>
> We are using Ansible 0.7.
>
> I appreciate any help you guys can provide!
>
> Thanks
>
> Jarrett
>
> --
>
>
Message has been deleted

Daniel Hokka Zakrisson

unread,
Oct 29, 2012, 4:37:36 PM10/29/12
to ansible...@googlegroups.com
Michael DeHaan wrote:
> Ansible 0.8 contains "with_fileglob", which does what you want
>
> action: modulename src=$item <other arguments>
> with_fileglob: /data_dump_*.sql
>
> It would send over all files matching the glob from the local server
> in that case.

However, when you want the opposite and you're using fetch, that is not
going to work.

> Looks like that will do exactly what you want.
>
> --Michael
>
> On Mon, Oct 29, 2012 at 3:56 PM, Jarrett Chisholm
> <jarrett...@gmail.com> wrote:
>> Hi all,
>>
>> I'm trying to copy a files down from one of our hosts, and it contains a
>> timestamp in the name. This, unfortunately, is going to be hard to
>> change
>> for us (if at all).
>>
>> Is there a way in ansible to have a source file with a wildcard?
>>
>> i.e.:
>>>
>>> action: fetch src=/data_dump_*.sql dest=/var/lib/ansible/files/fetched

Yes, the way to do it is to list the files in one action, and then use
with_items on the result. For example
- name: list files
action: command ls -1 /data_dump_*.sql
register: dumpfiles
- name: fetch files
action: fetch src=$item dest=/var/lib/ansible/files/fetched
with_items: ${dumpfiles.stdout_lines}

Daniel

Dag Wieers

unread,
Oct 29, 2012, 7:51:09 PM10/29/12
to ansible...@googlegroups.com
On Mon, 29 Oct 2012, Daniel Hokka Zakrisson wrote:

> Michael DeHaan wrote:
>> On Mon, Oct 29, 2012 at 3:56 PM, Jarrett Chisholm
>> <jarrett...@gmail.com> wrote:
>>> Hi all,
>>>
>>> I'm trying to copy a files down from one of our hosts, and it contains a
>>> timestamp in the name. This, unfortunately, is going to be hard to
>>> change
>>> for us (if at all).
>>>
>>> Is there a way in ansible to have a source file with a wildcard?
>>>
>>> i.e.:
>>>>
>>>> action: fetch src=/data_dump_*.sql dest=/var/lib/ansible/files/fetched
>
> Yes, the way to do it is to list the files in one action, and then use
> with_items on the result. For example
> - name: list files
> action: command ls -1 /data_dump_*.sql
> register: dumpfiles
> - name: fetch files
> action: fetch src=$item dest=/var/lib/ansible/files/fetched
> with_items: ${dumpfiles.stdout_lines}

What I usually do in this case, especially if you need to recurse over
directories is:

----
- action: command mktemp -d
register: tempdir

- local_action: rsync -aH /local/path ${inventory_hostname}:${tempdir.stdout}
----

In the above case it's in a temporary directory because we start the
installation from that directory and afterwards remove it. In case you
simply want to make sure what is on the destination is exactly like on the
source, you might want to add some other options, eg. --delete
--delay-updates, etc...

Or the reverse if firewalls allow it and DNS is happy with it:
----
- local_action: command hostname -s
register: localname

- action: rsync -aH /remote/path ${localname.stdout}:/local/path
----

However I was wondering if an rsync-module using rsync over the existing
transport would be a possibility to avoid firewall/dns issues.

Kind regards,
--
-- dag wieers, d...@wieers.com, http://dag.wieers.com/
-- dagit linux solutions, in...@dagit.net, http://dagit.net/

[Any errors in spelling, tact or fact are transmission errors]

Michael DeHaan

unread,
Oct 29, 2012, 8:26:50 PM10/29/12
to ansible...@googlegroups.com
I prefer Daniel's approach in this case, but I think we can also make syntax easier if using fetch by building a "with_remote_fileglob" if we wanted to.

-- Michael
> --
>
>

Dag Wieers

unread,
Oct 29, 2012, 8:34:52 PM10/29/12
to ansible...@googlegroups.com
Which should have read:

----
- local_action: rsync -aH ${inventory_hostname}:/remote/path /local/path
----

No need to make a remote action out of this.

Michael DeHaan

unread,
Oct 29, 2012, 8:37:02 PM10/29/12
to ansible...@googlegroups.com
The only disadvantage of shelling out to rsync is it assumes you have
keys and such configured.

So if you're using paramiko with askpass, it is not a clean option.

However, if you are moving a large amount of data, it's definitely a
great option, and most people will be using SSH-agent anyway.

Also: fetch is a minority of the use cases anyway :)
> --
>
>

Joanna Delaporte

unread,
May 18, 2016, 3:50:31 PM5/18/16
to Ansible Project
I am wondering if a remote fileglob mechanism has been added for fetch. If not, I'll use Daniel's solution from above.

Thanks!
Joanna

Brian Coca

unread,
May 18, 2016, 3:55:04 PM5/18/16
to ansible...@googlegroups.com
instead of shell/ls, you can use the 'find' module to create the list and feed that to the 'fetch' module.

as for rsync, the syncronize module can handle many of the same scenarios.


--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages