我想创建文件夹:temp2,它能够存储其他饲料的子文件夹/文件的所有符号链接:temp1。with_items可以帮助完成此任务,但它需要列出所有文件夹/文件名,如下所示:
- name: "create folder: temp2 to store symlinks"
file:
path: "/etc/temp2"
state: directory
- name: "create symlinks & store in temp2"
file:
src: "/etc/temp1/{{ item.src }}"
dest: "/etc/temp2/{{ item.dest }}"
state: link
force: yes
with_items:
- { src: 'BEAM', dest: 'BEAM' }
- { src: 'cfg', dest: 'cfg' }
- { src: 'Core', dest: 'Core' }
- { src: 'Data', dest: 'Data' }
Run Code Online (Sandbox Code Playgroud)
它不灵活,因为将添加或删除 temp1 下的子文件夹/文件,我需要经常更新上述脚本以保持符号链接的更新
有没有办法自动检测 temp1 下的所有文件/文件夹而不是维护with_items列表?
以下代码适用于 Ansible-2.8:
- name: Find all files in ~/commands
find:
paths: ~/commands
register: find
- name: Create symlinks to /usr/local/bin
become: True
file:
src: "{{ item.path }}"
path: "/usr/local/bin/{{ item.path | basename }}"
state: link
with_items: "{{ find.files }}"
Run Code Online (Sandbox Code Playgroud)
find您可以使用module创建文件列表:
\n\n\n根据特定条件返回文件列表。多个条件 AND\xe2\x80\x99d 在一起。
\n
您可能需要将设置保留recurse为false(默认),因为您假设可能存在子文件夹。
您需要使用声明来注册模块的结果register:
register: find\nRun Code Online (Sandbox Code Playgroud)在下一步中,您需要迭代结果files中的列表:
with_items: "{{ find.results.files }}"\nRun Code Online (Sandbox Code Playgroud)\n\n并引用该path键的值。你已经知道怎么做了。
您还需要从路径中提取文件名,以便可以将其附加到目标路径。为此使用basename过滤器。
| 归档时间: |
|
| 查看次数: |
3634 次 |
| 最近记录: |