首页 > 解决方案 > Ansible 如何遍历文件名以制作符号链接?

问题描述

这是我在 Ansible Play-Book 中的规范,用于制作符号链接:

---
- 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"

src:其中迭代loop:其中好的路径和文件名。但是,如何在dest:没有路径的情况下仅使用文件名?

标签: ansible

解决方案


这个任务应该做到这一点,而且它非常不言自明:

  - 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"

希望能帮助到你!


推荐阅读