是否有with_fileglob在ansible中可远程工作? [英] Is there with_fileglob that works remotely in ansible?

查看:211
本文介绍了是否有with_fileglob在ansible中可远程工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有with_fileglob可以在ansible中远程工作?

Is there with_fileglob that works remotely in ansible?

主要我确实想使用与with_fileglob类似的东西,但是这将使远程/目标计算机上的文件遍历,而不是正在运行ansible的计算机上的文件.

Mainly I do want to use something similar with the with_fileglob but that will glob the files on the remote/target machine, not on the one that is running ansible.

推荐答案

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

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.

您可以做的是通过向主机添加Shell,然后注册输出并在输出的stdout_lines部分上循环来生成您的fileglob.

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 }}"

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

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/.

这篇关于是否有with_fileglob在ansible中可远程工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆