TechChain
TechChain

Reputation: 8942

Copy files between servers using SCP in ansible?

I have executed a ansible script on my machine. What is does is

1.Copy a file from my machine to a docker container running on a remote machine

file.pb

2.Execute a command on docker container. That command uses the copied file & sign it.

This is the command

peer channel signconfigtx -f file.pb

Now i want to again copy this signed file to another machine after it is signed in the same ansible script from the docker container where it is now.

I want try SCP but not sure how it will work because it will ask for password.

Can anyone suggest me how can i do this ?

EDIT:

I tried on my local machine to fetch files from remote server but it i get below error

fatal: [user1]: FAILED! => {"msg": "Unable to create local directories(/home/dhiraj/ansible_practise/playlist/fetched/user1/home/user1/Documents/Blockchain/network/scripts): [Errno 20] Not a directory: '/home/dhiraj/ansible_practise/playlist/fetched/user1'"}

below is my ansible script

 - name:  Fetching a file from remote server
      fetch:
        src: "/home/user1/Documents/Blockchain/network/scripts/file.pb"
        dest: fetched

Upvotes: 1

Views: 9124

Answers (2)

TechChain
TechChain

Reputation: 8942

Instead of copying files directly bertween servers what i have done is i used fetch module of ansible. In fetch i just had to define source path and destination path.

Here is sample

   - name:  Fetching a file from remote server
      fetch:
        src: "{{ DEST }}/Documents/Blockchain/network/scripts/file.pb"
        dest: /home/fetched/
        flat: yes // it will not create dir strcuture as same as source
        validate_checksum: false

Upvotes: 1

Vladimir Botka
Vladimir Botka

Reputation: 68144

There are more options.

1) scp from docker to another machine

  • Put public key of the user running scp at Docker to authorized_key of the user you connect at another machine.
  • At docker run command: scp file.pb user@another_machine:file.pdb

2) fetch and copy

  • fetch from docker to your machine

  • copy from your machine to another machine

3) fetch and pull

  • fetch from docker to your machine
  • At another machine run ansible-pull and run the playbook that would fetch the file from your machine.

4) Run scp at another machine ...

Upvotes: 1

Related Questions