StandardNerd
StandardNerd

Reputation: 4183

Ansible How to iterate through filenames for making symlinks?

Here is my specifications in Ansible Play-Book, to make symlinks:

---
- hosts: DEVSRV
  become: yes
  tasks:
  - name: symlink deploy_config scripts
    file:
      src: "{{ item }}"
      dest: "/usr/local/bin/"
      state: link
    loop:
      - "/home/foo/bar/deploy/config/dev_deploy_config.sh"
      - "/home/foo/bar/deploy/config/int_deploy_config.sh"
      - "/home/foo/bar/deploy/config/prod_deploy_config.sh"

In src: it iterates over the path and filenames within loop: which is good. However, how can I use just filenames for dest: without the path?

Upvotes: 0

Views: 240

Answers (1)

ilias-sp
ilias-sp

Reputation: 6685

this task should do it, and its pretty self-explanatory:

  - name: symlink deploy_config scripts
    file:
      src: "{{ item }}"
      dest: "/usr/local/bin/{{ item.split('/') | last }}"
      state: link
    loop:
      - "/home/foo/bar/deploy/config/dev_deploy_config.sh"
      - "/home/foo/bar/deploy/config/int_deploy_config.sh"
      - "/home/foo/bar/deploy/config/prod_deploy_config.sh"

hope it helps!

Upvotes: 1

Related Questions