task action over multiple files using fileglob query

686 views
Skip to first unread message

Walid

unread,
Nov 22, 2013, 3:47:41 PM11/22/13
to ansible...@googlegroups.com
Hi,
I would like  to run the module lineinfile against several files that exist on the remote managed host.  I saw in the changelog for 1.4  that i can use the following lookup plugin feature:
" added a fileglob filter that will return files matching a glob pattern. with_items: "/foo/pattern/*.txt | fileglob"
however i did not seem to get it right, see below, could you please let me know what other possible ways to do this, or what i might be doing wrong below 
I tried it with a simple debug task as follows:

- action: shell echo "{{ item }}"
   with_items:
    - " /etc/* | fileglob"
   register: task
   tags:
      - globtest
 - debug: msg="{{item.item}}"
   with_items: task.results
   when: item.changed == True
   tags:
      - globtest


however it did not work for me as i expected it, it returned the following :
TASK: [common | shell echo ""] ************************************************ 
changed: [tlcav117] => (item= /etc/* | fileglob)

TASK: [common | debug msg="{{item.item}}"] ************************************ 
ok: [tlcav117] => (item={'item': ' /etc/* | fileglob', u'delta': u'0:00:00.002148', u'cmd': u'echo " /etc/* | fileglob" ', u'end': u'2013-11-22 23:44:54.216654', u'stderr': u'', u'stdout': u' /etc/* | fileglob', 'invocation': {'module_name': 'shell', 'module_args': u'echo " /etc/* | fileglob"'}, u'changed': True, u'rc': 0, u'start': u'2013-11-22 23:44:54.214506'}) => {
    "item": {
        "changed": true, 
        "cmd": "echo \" /etc/* | fileglob\" ", 
        "delta": "0:00:00.002148", 
        "end": "2013-11-22 23:44:54.216654", 
        "invocation": {
            "module_args": "echo \" /etc/* | fileglob\"", 
            "module_name": "shell"
        }, 
        "item": " /etc/* | fileglob", 
        "rc": 0, 
        "start": "2013-11-22 23:44:54.214506", 
        "stderr": "", 
        "stdout": " /etc/* | fileglob"
    }, 
    "msg": " /etc/* | fileglob"
}

regards

Walid

Michael DeHaan

unread,
Nov 22, 2013, 3:56:51 PM11/22/13
to ansible...@googlegroups.com


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Walid

unread,
Nov 22, 2013, 4:05:38 PM11/22/13
to ansible...@googlegroups.com
Hi Michael,

i tried with_fileglob first, however it references or resolves files from the local machine running ansible-playbook, not the remote managed machine. what i am trying to do is to change remote configuration files using lineinfile module. 

kind regards

Walid


Michael DeHaan

unread,
Nov 22, 2013, 4:13:40 PM11/22/13
to ansible...@googlegroups.com
Fileglob always runs locally, this is true.

You should probably run a command like "ls"  remotely and register the result, and then iterate over the result.stdout_lines variable.


Walid

unread,
Nov 22, 2013, 4:15:54 PM11/22/13
to ansible...@googlegroups.com
Thanks Michael, will do that. what was the new lookup plugin :
with_items: "/foo/pattern/*.txt | fileglob"?

Michael DeHaan

unread,
Nov 22, 2013, 4:34:01 PM11/22/13
to ansible...@googlegroups.com
Nope, simpler, as in docs:

with_fileglob:
  - /splat/*.txt

-- Michael
Reply all
Reply to author
Forward
0 new messages