Answers: <p>There is no general method and it might depend on how <code>boxcutter/ol67</code> was packed.</p>
  
  <ol>
  <li><p>The easiest method would be to define the password in the Ansible inventory file:</p>
  
  <pre><code>[oracle-vm:vars]
  ansible_ssh_user=vagrant
  ansible_ssh_pass=vagrant
  </code></pre></li>
  <li><p>The second method would be to leave the insecure private key configured on the <code>oracle-vm</code> machine and inject the private key to the <code>ansible</code> VM:</p>
  
  <pre><code>config.vm.provision "shell" do |s|
    ssh_insecure_key = File.readlines("#{Dir.home}/.vagrant.d/insecure_private_key").first.strip
    s.inline = &lt;&lt;-SHELL
      echo #{ssh_insecure_key} &gt;&gt; /home/vagrant/.ssh/id_rsa
      chown vagrant /home/vagrant/.ssh/id_rsa
      chmod 400 /home/vagrant/.ssh/id_rsa
    SHELL
  end
  </code></pre></li>
  <li><p>Generate the key pair beforehand on the host machine, inject private key to Ansible VM, public key to Oracle's <code>authorized_keys</code>.</p></li>
  <li><p>Generate the key pair on Ansible VM, copy the public key to Oracle VM using shell provisioner and inject <code>vagrant</code> as password for <code>ssh-copy-id</code>.</p></li>
  </ol>
  
  <p>And the list does not end here, it depends on required security.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-05-25T08:19:20.420Z
Answers: 2017-05-25T08:28:32.633Z
Answers: 2017-05-25T08:28:32.633Z
Answers: techraf
Answers: <p>If you want to have a preformatted block within a list, indent by eight spaces:</p>
  
  <ol>
  <li><p>generate public/private key</p>
  
  <pre><code>cd vagrant-home
  ssh-keygen // just pressed enter
  copy ~/.ssh/id_rsa .
  copy ~/.ssh/id_rsa.pub .
  </code></pre></li>
  <li><p>edit Vagrantfile,add follow lines:
          config.vm.provision "file", source: "id_rsa", destination: "/home/vagrant/.ssh/id_rsa"</p>
  
  <pre><code>    public_key = File.read("id_rsa.pub")
      config.vm.provision "shell", inline: &lt;&lt;-SCRIPT
          chmod 600 /home/vagrant/.ssh/is_rsa
          echo 'Copying ansible-vm public SSH Keys to the VM'
          #mkdir -p /home/vagrant/.ssh
          chmod 700 /home/vagrant/.ssh
          echo '#{public_key}' &gt;&gt; /home/vagrant/.ssh/authorized_keys
          chmod -R 600 /home/vagrant/.ssh/authorized_keys
          echo 'Host 192.168.*.*' &gt;&gt; /home/vagrant/.ssh/config
          echo 'StrictHostKeyChecking no' &gt;&gt; /home/vagrant/.ssh/config
          echo 'UserKnownHostsFile /dev/null' &gt;&gt; /home/vagrant/.ssh/config
          chmod -R 600 /home/vagrant/.ssh/config
          SCRIPT
  </code></pre></li>
  <li><pre><code>        vagrant up // or vagrant reload --provision
  </code></pre></li>
  </ol>
  
Answers: CC BY-SA 4.0
Answers: 2018-08-21T17:14:52.100Z
Answers: Is this just a reformatted version of my answer? If so you should probably just edit my answer rather than adding a new one.
Answers: opticyclic
Answers: CC BY-SA 4.0
Answers: 2018-08-20T01:15:42.833Z
Answers: 2018-08-20T01:32:04.173Z
Answers: 2018-08-20T01:32:04.173Z
Answers: tar tu
Answers: <p>You could generate the keys using shell provisioner ssh-keygen quiet mode, copy the pub key to the project folder and then copy it to the other(s) machine(s) using file provisioner. Assuming /vagrant/ is the project synced folder, it should be something like this:</p>
  <pre><code>machine1.vm.provision &quot;shell&quot;, inline: &quot;[ ! -f '/home/vagrant/.ssh/id_rsa' ] &amp;&amp; ssh-keygen -q -f /home/vagrant/.ssh/id_rsa -N ''&quot;
  
  machine1.vm.provision &quot;shell&quot;, inline: &quot;cp /home/vagrant/.ssh/id_rsa.pub /vagrant/&quot;
  
  machine2.vm.provision &quot;file&quot;, source: &quot;id_rsa.pub&quot;, destination: &quot;/home/vagrant/.ssh/&quot;
  
  machine2.vm.provision &quot;shell&quot;, inline: &quot;[ ! -f '/home/vagrant/.ssh/id_rsa_machine1.pub' ] &amp;&amp; cat /vagrant/id_rsa.pub &gt;&gt; /home/vagrant/.ssh/authorized_keys&quot;
  
  machine2.vm.provision &quot;file&quot;, source: &quot;id_rsa.pub&quot;, destination: &quot;/home/vagrant/.ssh/id_rsa_machine1.pub&quot;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-01-30T23:55:02.823Z
Answers: 2022-01-31T20:19:12.653Z
Answers: 2022-01-31T20:19:12.653Z
Answers: samueldc
Answers: <p>Based on <a href="https://devops.stackexchange.com/a/1241/375">techraf's 3rd suggestion</a> I did the following:</p>
  
  <ul>
  <li><code>vagrant up ansible</code></li>
  <li><code>ssh-keygen</code> (no password just pressed <kbd>Enter</kbd>)</li>
  <li>copied <code>.ssh/id_rsa</code> and <code>.ssh/id_rsa.pub</code> to the project directory</li>
  <li><code>vagrant destroy ansible</code></li>
  <li>modified the <code>Vagrantfile</code> to copy the <code>id_rsa</code> to all hosts</li>
  <li>modified the <code>Vagrantfile</code> to copy the <code>id_rsa.pub</code> into <code>authorized_keys</code> on all hosts</li>
  <li>modified the Vagrantfile to disable host checking</li>
  </ul>
  
  <p>Vagrantfile snippet:</p>
  
  <pre><code> config.vm.provision "file", source: "id_rsa", destination: "/home/vagrant/.ssh/id_rsa"
   public_key = File.read("id_rsa.pub")
   config.vm.provision :shell, :inline =&gt;"
       echo 'Copying ansible-vm public SSH Keys to the VM'
       mkdir -p /home/vagrant/.ssh
       chmod 700 /home/vagrant/.ssh
       echo '#{public_key}' &gt;&gt; /home/vagrant/.ssh/authorized_keys
       chmod -R 600 /home/vagrant/.ssh/authorized_keys
       echo 'Host 192.168.*.*' &gt;&gt; /home/vagrant/.ssh/config
       echo 'StrictHostKeyChecking no' &gt;&gt; /home/vagrant/.ssh/config
       echo 'UserKnownHostsFile /dev/null' &gt;&gt; /home/vagrant/.ssh/config
       chmod -R 600 /home/vagrant/.ssh/config
       ", privileged: false
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2018-10-02T19:04:40.783Z
Answers: This solution worked well for me but I had to change Host 192.168.*.* to just *. Not sure why. Thanks!
Answers: Zacho
Answers: CC BY-SA 4.0
Answers: 2017-05-25T22:44:51.087Z
Answers: 2018-08-30T17:48:40.007Z
Answers: 2018-08-30T17:48:40.007Z
Answers: opticyclic
Body: <p>I have 4 VMs in my Vagrantfile - 3 application servers and an Ansible control host.</p>
  
  <p>I only use Vagrant to create the VMs as I provision them manually from the ansible control host because I am still creating/editing the ansible scripts.</p>
  
  <p>I can do <code>vagrant ssh ansible</code> and <code>vagrant ssh app1/2/3</code> etc. but when I try to do <code>ansible-playbook oracle.yml</code> from the Ansible control host, SSH fails with</p>
  
  <pre><code>fatal: [192.168.60.10]: UNREACHABLE! =&gt; {"changed": false, "msg": "SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue", "unreachable": true}
  </code></pre>
  
  <p>I can successfully ssh from the Ansible VM to the Oracle VM using user vagrant and password vagrant.</p>
  
  <p>The key parts of my Vagrantfile are:</p>
  
  <pre><code>config.ssh.insert_key = false
  
  config.vm.define "db" do |db|
      db.vm.box = "boxcutter/ol67"
      db.vm.hostname = "oracle-vm"
      db.vm.network "forwarded_port", guest: 22, host: 2201, id: "ssh", auto_correct: false
      db.vm.network "forwarded_port", guest: 1521, host: 1521
      db.vm.network "private_network", ip: "192.168.60.10"
      db.vm.provider "virtualbox" do |v|
          v.name = "oracle-vm"
          v.linked_clone = true
          v.memory = 2048
          v.cpus = 2
      end
  end
  
  #Optional ansible control machine for Windows users
  config.vm.define "ansible", autostart: false do |ansible|
      ansible.vm.box = "williamyeh/ansible"
      ansible.vm.hostname = "ansible-vm"
      ansible.vm.network "forwarded_port", guest: 22, host: 2204, id: "ssh", auto_correct: false
      ansible.vm.network "private_network", ip: "192.168.60.50"
      ansible.vm.provider "virtualbox" do |v|
          v.linked_clone = true
      end
      #Mount the project directory on the guest so we can run the playbooks from there
      ansible.vm.synced_folder ".", "/data/ansible", create: true
  end
  </code></pre>
  
  <p>What do I need to put in the Vagrantfile to allow the Ansible VM to connect to the other VMs without requiring a password or extra manual steps after <code>vagrant up</code>? </p>
  
  <p>This is just for development testing on a private network on developers PCs so security is not really an issue and comes second to ease of implementation and smooth user experience.</p>
  
Comments: CC BY-SA 3.0
Comments: 2017-05-25T00:47:39.603Z
Comments: Duplicate of https://devops.stackexchange.com/questions/1017/unable-to-ssh-in-a-mutli-machine-vagrant-environment
Comments: James Shewey
Comments: CC BY-SA 3.0
Comments: 2017-05-25T02:34:32.963Z
Comments: I stated quite clearly that I can ssh between VMs it is just that ansible cannot because it requires keys set up. He cannot ssh. The questions are clearly different.
Comments: opticyclic
Comments: CC BY-SA 3.0
Comments: 2018-03-15T00:36:55.113Z
Comments: @JamesShewey: I don't think that any answer for that question is relevant to that. The answer given refers to communications from the host to the guests; the needed answer needs to apply between guests.
Comments: Torenware Networks
Tags: <ansible><vagrant>
Title: How do I configure SSH keys in a Vagrant multi-machine setup?
UserDisplayName: opticyclic
ThreadText: # How do I configure SSH keys in a Vagrant multi-machine setup?
  
  (Asked by: username_0 on 2017-05-25)
  
  I have 4 VMs in my Vagrantfile - 3 application servers and an Ansible control host.
  
  I only use Vagrant to create the VMs as I provision them manually from the ansible control host because I am still creating/editing the ansible scripts.
  
  I can do `vagrant ssh ansible` and `vagrant ssh app1/2/3` etc. but when I try to do `ansible-playbook oracle.yml` from the Ansible control host, SSH fails with
  
  ```
  fatal: [192.168.60.10]: UNREACHABLE! => {"changed": false, "msg": "SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue", "unreachable": true}
  
  ```
  
  I can successfully ssh from the Ansible VM to the Oracle VM using user vagrant and password vagrant.
  
  The key parts of my Vagrantfile are:
  
  ```
  config.ssh.insert_key = false
  
  config.vm.define "db" do |db|
      db.vm.box = "boxcutter/ol67"
      db.vm.hostname = "oracle-vm"
      db.vm.network "forwarded_port", guest: 22, host: 2201, id: "ssh", auto_correct: false
      db.vm.network "forwarded_port", guest: 1521, host: 1521
      db.vm.network "private_network", ip: "192.168.60.10"
      db.vm.provider "virtualbox" do |v|
          v.name = "oracle-vm"
          v.linked_clone = true
          v.memory = 2048
          v.cpus = 2
      end
  end
  
  #Optional ansible control machine for Windows users
  config.vm.define "ansible", autostart: false do |ansible|
      ansible.vm.box = "williamyeh/ansible"
      ansible.vm.hostname = "ansible-vm"
      ansible.vm.network "forwarded_port", guest: 22, host: 2204, id: "ssh", auto_correct: false
      ansible.vm.network "private_network", ip: "192.168.60.50"
      ansible.vm.provider "virtualbox" do |v|
          v.linked_clone = true
      end
      #Mount the project directory on the guest so we can run the playbooks from there
      ansible.vm.synced_folder ".", "/data/ansible", create: true
  end
  
  ```
  
  What do I need to put in the Vagrantfile to allow the Ansible VM to connect to the other VMs without requiring a password or extra manual steps after `vagrant up`?
  
  This is just for development testing on a private network on developers PCs so security is not really an issue and comes second to ease of implementation and smooth user experience.
  
  **Tags:** ansible, vagrant
  
  > **Comments:**
  > **username_1**: Duplicate of https://devops.stackexchange.com/questions/1017/unable-to-ssh-in-a-mutli-machine-vagrant-environment  
  > **username_0**: I stated quite clearly that I can ssh between VMs it is just that ansible cannot because it requires keys set up. He cannot ssh. The questions are clearly different.  
  > **username_2**: @username_1: I don't think that any answer for that question is relevant to that. The answer given refers to communications from the host to the guests; the needed answer needs to apply between guests.  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_0 on 2017-05-25. Score: 8 | Accepted answer
  
  Based on [username_3's 3rd suggestion](https://devops.stackexchange.com/a/1241/375) I did the following:
  
  * `vagrant up ansible`
  * `ssh-keygen` (no password just pressed `Enter`)
  * copied `.ssh/id_rsa` and `.ssh/id_rsa.pub` to the project directory
  * `vagrant destroy ansible`
  * modified the `Vagrantfile` to copy the `id_rsa` to all hosts
  * modified the `Vagrantfile` to copy the `id_rsa.pub` into `authorized_keys` on all hosts
  * modified the Vagrantfile to disable host checking
  
  Vagrantfile snippet:
  
  ```
   config.vm.provision "file", source: "id_rsa", destination: "/home/vagrant/.ssh/id_rsa"
   public_key = File.read("id_rsa.pub")
   config.vm.provision :shell, :inline =>"
       echo 'Copying ansible-vm public SSH Keys to the VM'
       mkdir -p /home/vagrant/.ssh
       chmod 700 /home/vagrant/.ssh
       echo '#{public_key}' >> /home/vagrant/.ssh/authorized_keys
       chmod -R 600 /home/vagrant/.ssh/authorized_keys
       echo 'Host 192.168.*.*' >> /home/vagrant/.ssh/config
       echo 'StrictHostKeyChecking no' >> /home/vagrant/.ssh/config
       echo 'UserKnownHostsFile /dev/null' >> /home/vagrant/.ssh/config
       chmod -R 600 /home/vagrant/.ssh/config
       ", privileged: false
  
  ```
  
  > **Comments:**
  > **username_6**: This solution worked well for me but I had to change Host 192.168.*.* to just *. Not sure why. Thanks!  
  
  ---
  
  ### Answer by username_3 on 2017-05-25. Score: 9
  
  There is no general method and it might depend on how `boxcutter/ol67` was packed.
  
  1. The easiest method would be to define the password in the Ansible inventory file:
  
     ```
     [oracle-vm:vars]
     ansible_ssh_user=vagrant
     ansible_ssh_pass=vagrant
  
     ```
  2. The second method would be to leave the insecure private key configured on the `oracle-vm` machine and inject the private key to the `ansible` VM:
  
     ```
     config.vm.provision "shell" do |s|
       ssh_insecure_key = File.readlines("#{Dir.home}/.vagrant.d/insecure_private_key").first.strip
       s.inline = <<-SHELL
         echo #{ssh_insecure_key} >> /home/vagrant/.ssh/id_rsa
         chown vagrant /home/vagrant/.ssh/id_rsa
         chmod 400 /home/vagrant/.ssh/id_rsa
       SHELL
     end
  
     ```
  3. Generate the key pair beforehand on the host machine, inject private key to Ansible VM, public key to Oracle's `authorized_keys`.
  4. Generate the key pair on Ansible VM, copy the public key to Oracle VM using shell provisioner and inject `vagrant` as password for `ssh-copy-id`.
  
  And the list does not end here, it depends on required security.
  
  
  ---
  
  ### Answer by username_4 on 2018-08-20. Score: 0
  
  If you want to have a preformatted block within a list, indent by eight spaces:
  
  1. generate public/private key
  
     ```
     cd vagrant-home
     ssh-keygen // just pressed enter
     copy ~/.ssh/id_rsa .
     copy ~/.ssh/id_rsa.pub .
  
     ```
  2. edit Vagrantfile,add follow lines:
     config.vm.provision "file", source: "id\_rsa", destination: "/home/vagrant/.ssh/id\_rsa"
  
     ```
         public_key = File.read("id_rsa.pub")
         config.vm.provision "shell", inline: <<-SCRIPT
             chmod 600 /home/vagrant/.ssh/is_rsa
             echo 'Copying ansible-vm public SSH Keys to the VM'
             #mkdir -p /home/vagrant/.ssh
             chmod 700 /home/vagrant/.ssh
             echo '#{public_key}' >> /home/vagrant/.ssh/authorized_keys
             chmod -R 600 /home/vagrant/.ssh/authorized_keys
             echo 'Host 192.168.*.*' >> /home/vagrant/.ssh/config
             echo 'StrictHostKeyChecking no' >> /home/vagrant/.ssh/config
             echo 'UserKnownHostsFile /dev/null' >> /home/vagrant/.ssh/config
             chmod -R 600 /home/vagrant/.ssh/config
             SCRIPT
  
     ```
  3. ```
             vagrant up // or vagrant reload --provision
  
     ```
  
  > **Comments:**
  > **username_0**: Is this just a reformatted version of my answer? If so you should probably just edit my answer rather than adding a new one.  
  
  ---
  
  ### Answer by username_5 on 2022-01-30. Score: 0
  
  You could generate the keys using shell provisioner ssh-keygen quiet mode, copy the pub key to the project folder and then copy it to the other(s) machine(s) using file provisioner. Assuming /vagrant/ is the project synced folder, it should be something like this:
  
  ```
  machine1.vm.provision "shell", inline: "[ ! -f '/home/vagrant/.ssh/id_rsa' ] && ssh-keygen -q -f /home/vagrant/.ssh/id_rsa -N ''"
  
  machine1.vm.provision "shell", inline: "cp /home/vagrant/.ssh/id_rsa.pub /vagrant/"
  
  machine2.vm.provision "file", source: "id_rsa.pub", destination: "/home/vagrant/.ssh/"
  
  machine2.vm.provision "shell", inline: "[ ! -f '/home/vagrant/.ssh/id_rsa_machine1.pub' ] && cat /vagrant/id_rsa.pub >> /home/vagrant/.ssh/authorized_keys"
  
  machine2.vm.provision "file", source: "id_rsa.pub", destination: "/home/vagrant/.ssh/id_rsa_machine1.pub"
  
  ```
  
  
  ---
  
  