Copying a text file from my Ubuntu Windows Sub System to a remote instance through SSH

Following questions involves use of YAML, BASH, SSH and Software called Ansible

I am trying to learn how to use a Linux environment (in my case a Ubuntu Windows Sub System) to copy a text file from my files to a remote instance (in this case Amazon Web Services) by connecting via SSH.

I have set up directories and files in Ubuntu like so:

hosts > my_hosts
playbook.yml
roles>my-role>files>test.txt
roles>my-role>tasks>main.yml

(hosts, playbook.yml, roles are all directories in home; hope the way I have shown it makes it clear)

The test.txt file contains a simple statement saying "Hello, World"; and this is what I want to send of to my remote instance.

The contents of the YAML files are as follows:

main.yml :
---

  • name: Copy a file to the remote host
    copy:
    src: test.txt
    dest: /tmp/test.txt

playbook.yml :
---

  • hosts: my_hosts
    roles:
    • my-role

I am able to connect to the Amazon Web Services Remote Instances using a command in the following format:

sudo ssh -i ansible_instance_d**_*.pem "ubuntu@ec2-**-***-**-.compute-1.amazonaws.com"
(ignore quotation marks)

I assume I now need to somehow add something in the text file my_hosts that will allow me to connect to the remote instance; and subsequently copy the text file.

In order to then run the process is use:

ansible-playbook -i hosts playbook.yml

But i don't know what to put in the my_hosts file or whether everything else i have done is correct.

I've tried my best to explain what I am trying to do, please ask any more questions to help clarify.

Thanks

Hi,

I would think that the standard scp functionality would be what you need as in;

sudo scp -i ansible_instance_d**_****.pem "local_path_to_file" "ubuntu@ec2-**-***-**-***.compute-1.amazonaws.com:/remote_path_to_file"

Minus all the quotes of course.

Regards

Gull04

And yes, if you have ssh access, you have scp access. It's the same protocol.

You will put the hostname you wish to use run ansible against.
In your case the hostname of amazon host written in that file.

Check out the /etc/ansible/hosts , it has comments about usage.
This is the default file used if you issue playbook without the -i parameter.

This presumes you have generated a ssh key for user you are issuing commands with and exchanged it with your amazon host.
See ssh-keygen and, since you are on Linux, ssh-copy-id if you haven't.
There is no need to use root accounts for any of the operations, only editing of default configuration files, which you are not doing.

It is also good to mention, using ansible to copy a file to one host is an overhead, since you can use scp to achieve the same.
If you require to execute several actions, copy files, start service(s), install packages on large number of servers ansible utility has a use case.

Otherwise it's like you are using a pneumatic hammer to kill a fly.

Regards
Peasant.

Thanks for your reply,

When running that command:
sudo scp -i ansible_instance_d**_*.pem "local_path_to_file" "ubuntu@ec2-**-***-**-.compute-1.amazonaws.com:/remote_path_to_file"

(obviously with all the right input)

i get
scp: /./test.txt: Permission denied

The access permission for the file is -rwxrwxrwx

The 'permission denied' is probably on the remote system, not yours. You are attempting to write to the root folder, which isn't going to work. Be sure to tell scp exactly where you want the file to end up.

scp -i ansible_instance_d**_****.pem "local_path_to_file" "ubuntu@ec2-**-***-**-***.compute-1.amazonaws.com:/path/to/remote/folder/filename"

In that light, the 'sudo' is probably superfluous. You don't seem to need special permissions on your local system, and don't get elevated permissions on the remote one anyway.

Also, chmod 777 is not the magic sledgehammer to fix all problems. Try chmod 660, unless you really want to keep a world-writable script executable hanging around.

Do you need sudo at all for ssh / scp ? There might be reasons against it, but isn't it usually installed open for every user?

User was getting 'permission denied' so did chmod 777 and slapped on a sudo. Neither of which helped because the permission problem wasn't local.

Thanks for your reply. It did work in the end;

It was because i was specifying the file path in the remote instance as:
....amazonaws.com:/.

1 Like