copy files to server from relative path in ansible
Asked Answered
O

2

15

How can I pass a relative path so that Ansible can copy files from node/keys and copy them to a server?

The playbook is ansible/playbook.

My directory structure is:

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

Below is the playbook:

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

  tasks: 

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

That's how I run:

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

I am trying to pass a relative path.

Otter answered 11/9, 2019 at 11:37 Comment(4)
Do you recieve an error or are the files just not copied?Gladisgladney
I don't see anything wrong with your approach. Just made a quick test and it works for me. What is the exact problem you are facing ?Almira
It can't find the file. It looks here /ansible/playbook/files/../node/keys. I am not sure it looks for ansible/filesOtter
can anyone help me wtih the issue ?Otter
H
-6

You can use absolute paths with src that avoids the problems of not knowing where is the root folder.

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

Harberd answered 11/9, 2019 at 11:47 Comment(5)
I don't want to have strict absolute path because i can put this script in any machineOtter
You do not have to. You can construct the absolute path very easily everywhere.Harberd
But in that case i need to change ansible script everytime if i move my code. In assible how can i get i do that ?Otter
you can combine pwd + relative path without changing the codeHarberd
As Istvan says, like ansible-playbook -i inventory/inventory.yaml playbook/crypto-generate.yaml --extra-vars "source=$(pwd)/../node/keys".Georgetta
P
35

I am using {{ playbook_dir }} to construct full path,see special ansible variables

- name: Copy files
  copy: 
    src:  "{{ playbook_dir }}/../../node/keys"
    dest: "{{ destination }}"
Psychographer answered 22/11, 2020 at 11:39 Comment(0)
H
-6

You can use absolute paths with src that avoids the problems of not knowing where is the root folder.

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

Harberd answered 11/9, 2019 at 11:47 Comment(5)
I don't want to have strict absolute path because i can put this script in any machineOtter
You do not have to. You can construct the absolute path very easily everywhere.Harberd
But in that case i need to change ansible script everytime if i move my code. In assible how can i get i do that ?Otter
you can combine pwd + relative path without changing the codeHarberd
As Istvan says, like ansible-playbook -i inventory/inventory.yaml playbook/crypto-generate.yaml --extra-vars "source=$(pwd)/../node/keys".Georgetta

© 2022 - 2024 — McMap. All rights reserved.