是否with_fileglob在ansible中远程工作?

sor*_*rin 24 ansible ansible-playbook

是否with_fileglob在ansible远程工作?

主要是我想要使用类似的东西with_fileglob但是会使远程/目标机器上的文件全局化,而不是运行ansible的文件.

yda*_*coR 20

with_*不幸的是,所有循环机制都是本地查找,因此在Ansible中没有真正干净的方法.设计中的远程操作必须包含在任务中,因为它需要处理连接和库存等.

你可以做的是通过shelling out到主机然后注册输出并循环输出stdout_lines部分来生成你的fileglob .

所以一个简单的例子可能是这样的:

- 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 }}"
Run Code Online (Sandbox Code Playgroud)

这将连接到远程主机(例如host.example.com),获取所有文件名/path/,然后将它们复制回Ansible主机到路径:/path/host.example.com/.


tec*_*raf 11

使用find模块过滤文件,然后处理结果列表:

- name: Get files on remote machine
  find:
    paths: /path/on/remote
  register: my_find

- debug:
    var: item.path
  with_items: "{{ my_find.files }}"
Run Code Online (Sandbox Code Playgroud)

  • 对于“win_find”也效果很好。感谢`with_items: "{{ my_find.files }}"`部分,这并不明显,我首先尝试使用`with_items: my_find.files`。 (2认同)