cal*_*rae 18 bash shell ansible 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
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有以下内容:
- name: Link Prezto files
file: src={{ item }} dest=~ state=link
with_fileglob:
- ~/.zprezto/runcoms/z*
Run Code Online (Sandbox Code Playgroud)
我知道它不一样,但它会选择相同的文件:除了主机上的with_fileglob外观,我希望它在远程机器上查看.
有没有办法做到这一点,或者我应该只使用shell脚本?
小智 17
清除与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 }}"
Run Code Online (Sandbox Code Playgroud)
Bruce P的解决方案可行,但它需要一个附加文件并且有点乱.以下是一个纯粹的安理解决方案.
第一个任务抓取文件名列表并将其存储在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
Run Code Online (Sandbox Code Playgroud)
当使用 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
Run Code Online (Sandbox Code Playgroud)
另一种方法是创建一个完整的命令行来执行您想要的操作并使用 shell 模块调用它:
- name: Generate links
shell: find ~/.zprezto/runcoms/z* -exec ln -s {} ~ \;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7913 次 |
最近记录: |