从ansible中的相对路径将文件复制到服务器

copy files to server from relative path in ansible

如何传递相对路径以便 Ansible 可以从 node/keys 复制文件并将它们复制到服务器?

剧本是ansible/playbook

我的目录结构是:

├── ansible
│   ├── inventory
│   └── playbook
├── node
│   ├── keys
│   ├── index.js
│   ├── node_modules
│   ├── package-lock.json
│   └── utils
└── shell
    ├── data.json
    ├── create-data.sh
    ├── destory.sh
    └── firewall-rules.sh

下面是剧本:

- hosts: all
  vars:
    source: "{{ source }}"
    destination: /home/ubuntu

  tasks: 

    - name: Copy files
      copy: 
        src:  "{{ source }}"
        dest: "{{ destination }}"

我就是这样 运行:

ansible-playbook -i inventory/inventory.yaml playbook/crypto-generate.yaml
 --extra-vars "source=../node/keys"

我正在尝试传递相对路径。

src可以使用绝对路径,避免不知道根文件夹在哪里的问题。

Local path to a file to copy to the remote server. This can be absolute or relative. If path is a directory, it is copied recursively. In this case, if path ends with "/", only inside contents of that directory are copied to destination. Otherwise, if it does not end with "/", the directory itself with all contents is copied. This behavior is similar to the rsync command line tool.

https://docs.ansible.com/ansible/latest/modules/copy_module.html

我正在使用{{ playbook_dir }}构建完整路径,请参阅 special ansible variables

- name: Copy files
  copy: 
    src:  "{{ playbook_dir }}/../../node/keys"
    dest: "{{ destination }}"