bash ansible:远程机器上的文件是否有类似 with_fileglobs 的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24112066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ansible: Is there something like with_fileglobs for files on remote machine?
提问by callumacrae
I'm trying to turn theselines into something I can put in an ansible playbook:
我正在尝试将这些行变成我可以放入 ansible playbook 的内容:
# Install Prezto files
shopt -s extglob
shopt -s nullglob
files=( "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/!(README.md) )
for rcfile in "${files[@]}"; do
[[ -f $rcfile ]] && ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile##*/}"
done
So far I've got the following:
到目前为止,我有以下几点:
- name: Link Prezto files
file: src={{ item }} dest=~ state=link
with_fileglob:
- ~/.zprezto/runcoms/z*
I know it isn't the same, but it would select the same files: except with_fileglob looks on the host machine, and I want it to look on the remote machine.
我知道它不一样,但它会选择相同的文件:除了 with_fileglob 在主机上查找,我希望它在远程机器上查找。
Is there any way to do this, or should I just use a shell script?
有什么办法可以做到这一点,还是应该只使用 shell 脚本?
回答by Doru C.
A clean Ansible way of purging unwanted files matching a glob is:
清除与 glob 匹配的不需要的文件的一种干净的 Ansible 方法是:
- name: List all tmp files
find:
paths: /tmp/foo
patterns: "*.tmp"
register: tmp_glob
- name: Cleanup tmp files
file:
path: "{{ item.path }}"
state: absent
with_items:
- "{{ tmp_glob.files }}"
回答by AdamY
Bruce P's solution works, but it requires an addition file and gets a little messy. Below is a pure ansible solution.
Bruce P 的解决方案有效,但它需要一个附加文件并且有点混乱。下面是一个纯粹的 ansible 解决方案。
The first task grabs a list of filenames and stores it in files_to_copy. The second task appends each filename to the path you provide and creates symlinks.
第一个任务获取文件名列表并将其存储在 files_to_copy 中。第二个任务将每个文件名附加到您提供的路径并创建符号链接。
- name: grab file list
shell: ls /path/to/src
register: files_to_copy
- name: create symbolic links
file:
src: "/path/to/src/{{ item }}"
dest: "path/to/dest/{{ item }}"
state: link
with_items: files_to_copy.stdout_lines
回答by Bruce P
The file module does indeed look on the server where ansible is running for files when using with_fileglob, etc. Since you want to work with files that exist solely on the remote machine then you could do a couple things. One approach would be to copy over a shell script in one task then invoke it in the next task. You could even use the fact that the file was copied as a way to only run the script if it didn't already exist:
当使用 with_fileglob 等时,文件模块确实会在 ansible 运行文件的服务器上查看。由于您想处理仅存在于远程机器上的文件,因此您可以做一些事情。一种方法是在一个任务中复制一个 shell 脚本,然后在下一个任务中调用它。您甚至可以使用文件被复制这一事实作为仅在脚本不存在时运行脚本的方式:
- name: Copy link script
copy: src=/path/to/foo.sh
dest=/target/path/to/foo.sh
mode=0755
register: copied_script
- name: Invoke link script
command: /target/path/to/foo.sh
when: copied_script.changed
Another approach would be to create an entire command line that does what you want and invoke it using the shell module:
另一种方法是创建一个完整的命令行来执行您想要的操作并使用 shell 模块调用它:
- name: Generate links
shell: find ~/.zprezto/runcoms/z* -exec ln -s {} ~ \;
回答by kejadlen
You can use with_lines
to accomplish this:
您可以使用以下方法with_lines
来完成此操作:
- name: Link Prezto files
file: src={{ item }} dest=~ state=link
with_lines: ls ~/.zprezto/runcoms/z*