Is there with_fileglob that works remotely in ansible?

前端 未结 4 996
说谎
说谎 2020-12-10 00:49

Is there with_fileglob that works remotely in ansible?

Mainly I do want to use something similar with the with_fileglob but that will glob

4条回答
  •  孤城傲影
    2020-12-10 01:26

    All of the with_* looping mechanisms are local lookups unfortunately so there's no really clean way to do this in Ansible. Remote operations by design must be enclosed in tasks as it would need to deal with connections and inventory etc.

    What you can do is generate your fileglob by shelling out to the host and then registering the output and looping over the stdout_lines part of the output.

    So a trivial example may be something like this:

    - name    : get files in /path/
      shell   : ls /path/*
      register: path_files
    
    - name: fetch these back to the local Ansible host for backup purposes
      fetch:
        src : /path/"{{item}}"
        dest: /path/to/backups/
      with_items: "{{ path_files.stdout_lines }}"
    

    This would connect to the remote host (e.g., host.example.com), get all the file names under /path/ and then copy them back to the Ansible host to the path: /path/host.example.com/.

提交回复
热议问题