How to Copy File from a Vagrant MAChine to Localhost

Easiest way to copy a single file from host to Vagrant guest?

Instead of using a shell provisioner to copy the file, you can also use a Vagrant file provisioner.

Provisioner name: "file"

The file provisioner allows you to upload a file from the host machine to the guest machine.

Vagrant.configure("2") do |config|
# ... other configuration

config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"
end

How to copy file from a Vagrant machine to localhost

You should read the manual page for scp. The correct syntax is:

scp -P 2222 vagrant@127.0.0.1:/home/vagrant/devstack/local.conf .

The uppercase P is for "port". Lowercase is used to preserve modification times.

copy file from host to vagrant virtual machine

To copy a file from the host to vagrant:

scp -P 2222 /path/to/file vagrant@127.0.0.1:.
  • By default The vagrant instance uses port 2222 and ip 127.0.0.1. The password for vagrant is 'vagrant'.
    The file will be copied to the home of vagrant.

To copy a file from vagrant to host machine:

scp -P 2222 vagrant@127.0.0.1:/path/to/file

vagrant /etc/hosts: machine IP vs localhost

General

The localhost is normally always the same on different machines: 127.0.0.1 (local loopback) and the VM IP is the external IP on the 'network'. You can for example connect from your machine to your VM by accessing the VM IP, but if you connect to localhost from your machine to the VM, you end up on your own machine.

If you bind a service to 127.0.0.1 you will not be able to reach it from 'outside' of the 'machine'.

This offers probably a better explanation if you want to read more:
https://www.lifewire.com/network-computer-special-ip-address-818385

More specific to your situation

Not sure if I understood your question correctly, but I guess your question is about: what are the hostnames in the /etc/hosts of your virtual machine? That is because they don't exist in the DNS and if you are connecting to these hosts it needs to end up at the right place and that, in this case is the VM itself.

ansible vagrant windows and localhost

There are a few things to say about your inventory:

  1. You seem to be confusing the concept of group and host
  2. You are defining a group that has the same name as an implicit host (i.e. localhost) (maybe because of point 1)
  3. You are explicitly defining a host for your ansible controller when this is actually probably not what you want since ansible defines an implicit localhost for you. Note that an explicit definition makes your controller match the all magic group which is typically not wanted in most situations.

From the information you gave, here is how I would write my inventory. The examples below use the ansible capability to organize vars in seperate files/folders. Please have a look at the inventory documentation for more info. I also used the yaml format for the inventory as it is easier to understand IMO. Feel free to translate back to ini if you whish

in inventories/my_env/hosts.yml (any filename will do)

---
all:
hosts:
my.windows.vagrant:

in inventories/my_env/host_vars/my.windows.vagrant.yml

---
ansible_host: 127.0.0.1
ansible_port: 55985
ansible_winrm_transport: basic
ansible_winrm_scheme: http
ansible_user: vagrant
ansible_password: "{{ lookup('env', 'WIN_GUEST_PASSWORD') }}"
ansible_connection: winrm

in inventories/my_env/host_vars/localhost.yml

---
ansible_python_interpreter: "/Library/Frameworks/Python.framework/Versions/3.8/bin/python3"

Note that I am not (re)defining the implicit localhost in the inventory, only defining the (non standard) python interpreter you use on that host. Note as well that I dropped the other vars for localhost as:

  1. implicit localhost uses the local connection plugin by default
  2. the local connection plugin does not take ansible_user into account and use the already logged in user (i.e. the one lauching the playbook on the controller)

Once you have done this, you can use the my.windows.vagrant target to address your vagrant windows box and localhost to run things on the controller.

Adapt to your exact needs.

How to copy files into a virtual box oracle box

A nice solution could be to share a directory on your host machine with the virtual machine, so you can copy your files in and out.

Look at:
How to share your computer files with a virtual machine



Related Topics



Leave a reply



Submit