How to create the multiple symlinks for the folders/files under the same source
Asked Answered
L

2

6

I want to create the folder: temp2 which is able to store all the symlinks of the subfolders/files of other foder: temp1. with_items can help complete this task, but it needs to list down all the folder/file name as below script shown:

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

It is not flexible as the subfolders/files under temp1 would be added or removed, and I need to update above script frequently to keep the symlinks as updated

Is there any way to detect all the files/folder under temp1 automatically instead of maintaining the with_items list?

Loadstar answered 8/11, 2017 at 3:25 Comment(0)
F
2
  • You can create a list of files using find module:

    Return a list of files based on specific criteria. Multiple criteria are AND’d together.

    You'll likely need to leave recurse set to false (default) since you assume subfolders might exist.

    You need to register the results of the module with register declaration:

    register: find
    
  • In the next step you need to iterate over the files list from the results:

    with_items: "{{ find.results.files }}"
    

    and refer to the value of the path key. You already know how to do it.

    You will also need to extract the filename from the path, so that you can append it to the destination path. Use basename filter for that.

Fortenberry answered 8/11, 2017 at 4:17 Comment(2)
Thanks TR. I have followed your answer and it works! Meanwhile, it is also my first time to raise question in Stack. Very impressive by your answer & this platform. Thanks again.Loadstar
Attached updated play book for archive or help somebody out: - name: "create folder: temp2 to store symlinks" file: path: "/etc/temp2" state: directory - name: "Recursively find file" find: paths: "/etc/temp1" file_type: "any" register: find_result - name: "create symlinks & store in temp2" file: src: "/etc/temp1/{{ item.path | basename}}" dest: "/etc/temp2/{{ item.path | basename }}" state: link force: yes with_items: "{{ find_result.files }}"Loadstar
L
9

The following code works under 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 }}"
Louvenialouver answered 9/9, 2019 at 17:1 Comment(1)
This works, thank you! Add file_type: any to the find task to include directories and symlinks, too, not only plain files.Lessor
F
2
  • You can create a list of files using find module:

    Return a list of files based on specific criteria. Multiple criteria are AND’d together.

    You'll likely need to leave recurse set to false (default) since you assume subfolders might exist.

    You need to register the results of the module with register declaration:

    register: find
    
  • In the next step you need to iterate over the files list from the results:

    with_items: "{{ find.results.files }}"
    

    and refer to the value of the path key. You already know how to do it.

    You will also need to extract the filename from the path, so that you can append it to the destination path. Use basename filter for that.

Fortenberry answered 8/11, 2017 at 4:17 Comment(2)
Thanks TR. I have followed your answer and it works! Meanwhile, it is also my first time to raise question in Stack. Very impressive by your answer & this platform. Thanks again.Loadstar
Attached updated play book for archive or help somebody out: - name: "create folder: temp2 to store symlinks" file: path: "/etc/temp2" state: directory - name: "Recursively find file" find: paths: "/etc/temp1" file_type: "any" register: find_result - name: "create symlinks & store in temp2" file: src: "/etc/temp1/{{ item.path | basename}}" dest: "/etc/temp2/{{ item.path | basename }}" state: link force: yes with_items: "{{ find_result.files }}"Loadstar

© 2022 - 2024 — McMap. All rights reserved.