using ansible within Vagrantfile -- how to configure ~/.ssh/config

75 views
Skip to first unread message

jason.wi...@gmail.com

unread,
Aug 12, 2020, 9:34:34 AM8/12/20
to Vagrant
Hey folks.
Looking for some help on how to get my setup working correctly.
I can use ansible AFTER vagrant up is working, by running:

vagrant ssh-config >> ~/.ssh/config
 Now can read the file and SSH into the host and run the playbooks.

How does that work when i am running ansilbe within vagrant up? Do you need to add the hosts into ~/.ssh/config ahead of time?
Just looking for help on how to get this working properly

Thanks

D Morgan

unread,
Aug 12, 2020, 10:50:48 AM8/12/20
to Vagrant
This is what a very basic Vagrantfile might look like

# vi: set ft=ruby :
num_machines = 1
itr = [
  # "bento/centos-6.7",
  # "bento/centos-7.2",
  # "bento/debian-8.6",
  # "bento/fedora-29",
  # "bento/ubuntu-16.10",
  "bento/ubuntu-18.04",
  # "bento/debian-9"
]

Vagrant.configure(2) do |config|

  itr.each_with_index do |os, index|

    config.vm.provision "file", source: "package.json", destination: "package.json"

    puts 'xxxxxxxxxxxxxxxxxxxxxxxxx', os

    # config.ssh.forward_agent = true

    config.vm.define "plu#{index+1}" do |plu|
      plu.vm.box = os
      plu.vm.box_check_update = true
      plu.vm.hostname = "plu#{index+1}"
      plu.vm.network "private_network", ip: "192.168.1.9#{index+1}"

      plu.vm.provider "virtualbox" do |v|
        v.name = "plu#{index+1}"
        v.memory = "512"
      end



      if index == (itr.length-1) then
        config.vm.provision "ansible" do |ansible|
          ansible.limit = "plu1"
          ansible.playbook = "./playbook.yml"
          ansible.inventory_path = "inventory"
          # ansible.ask_become_pass = true
          ansible.raw_arguments = ["--flush-cache"]
          ansible.verbose = "-vvvv"
        end
      end


    end

  end
end


jason.wi...@gmail.com

unread,
Aug 12, 2020, 11:05:53 AM8/12/20
to Vagrant
Thank you sir. I was messing around with a very basic setup. Here is a snippet of what i was trying to do:

```
Vagrant.configure("2") do |config|
  #config.vm.synced_folder "../data", "/vagrant_data"
  (1..NODE_COUNT).each do |i|
    config.vm.define "proxy0#{i}" do |subconfig|
      subconfig.ssh.private_key_path = ["/Users/jason/.ssh/vagrant", "~/.vagrant.d/insecure_private_key"]
      subconfig.vm.provision "file", source: "/Users/jason/.ssh/vagrant.pub", destination: "~/.ssh/authorized_keys"
      subconfig.ssh.insert_key = false
      subconfig.vm.provider "virtualbox" do |vb|
        vb.name = "proxy0#{i}"
        vb.memory = 1024
        vb.cpus   = 1
      end
    subconfig.vm.box = PLUS_IMAGE
    subconfig.vm.hostname = "node0#{i}"
    subconfig.vm.network :private_network, ip: "172.16.10.#{i + 20}"
    subconfig.vm.network "forwarded_port", guest: 80, host: "#{HA_PORT_START + i}"
    subconfig.vm.network "forwarded_port", guest: 8080, host: "#{DASHBOARD + i}"
    subconfig.vm.provision "os-setup", :type => "shell", :path => "provisioners/scripts/os-setup.sh"
    subconfig.vm.provision "setup-hosts", :type => "shell", :path => "provisioners/scripts/setup-hosts.sh" do |s|
      s.args = ["enp0s8"]
    end
    subconfig.vm.provision "ansible" do |ansible|
      ansible.limit = "all"
      ansible.playbook = "./provisioners/ansible/mainplus.yml"
      ansible.inventory_path = "./provisioners/ansible/inventory"
    end
  end
end
```

Now, the trouble I am running into is that when ansible goes to run the playbook, it cant connect to the vagrant box (I am assuming, because that host is not in ~/.ssh/config file. I may be wrong though).
I am lining up the name of the hosts in my vagrantfile with that in the inventory file.
That seems to be my road block.
If I remove the ansible section, it works fine. I then do a vagrant ssh-config >> ~/.ssh/config, do a vagrant destroy -f, do a fresh 'vagrant up' and then it works

Hopefully that makes sense.
But that is where I a stuck.
I can run ansible manually, which is fine, but would like to be able to get this to work 'integrated' with the vagrantfile.

Much obliged

Jason

D Morgan

unread,
Aug 12, 2020, 11:08:59 AM8/12/20
to Vagrant
If you comment out these three lines does it work as expected?

subconfig.ssh.private_key_path = ["/Users/jason/.ssh/vagrant", "~/.vagrant.d/insecure_private_key"]
subconfig.vm.provision "file", source: "/Users/jason/.ssh/vagrant.pub", destination: "~/.ssh/authorized_keys"
subconfig.ssh.insert_key = false



Jason Williams

unread,
Aug 12, 2020, 11:13:30 AM8/12/20
to vagra...@googlegroups.com
Yea, the box comes up fine and the private key works, as it should, but ansible fails when executing the playbook:
Here is the error output:

==> proxy01: Running provisioner: ansible...
    proxy01: Running ansible-playbook...

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
fatal: [web02]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ", "unreachable": true}
fatal: [proxy01]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname proxy01: nodename nor servname provided, or not known", "unreachable": true}
fatal: [proxy02]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname proxy02: nodename nor servname provided, or not known", "unreachable": true}

Am I correct in assuming ansible reads ~/.ssh/config to SSH into the vagrant box?

Much obliged!




--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/vagrant/issues
Discuss: https://discuss.hashicorp.com/c/vagrant/24
---
You received this message because you are subscribed to a topic in the Google Groups "Vagrant" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vagrant-up/LWuwDW97f04/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/a09130c8-e73b-4f07-b945-3a7057269402o%40googlegroups.com.

D Morgan

unread,
Aug 12, 2020, 11:31:22 AM8/12/20
to Vagrant
I think the issue you are running into is that it's running the playbook before the VM has actually finished spinning up. If you look at my Vagrantfile, I actually wait to start running the playbooks after the last VM is created.

if index == (itr.length-1) then
    config.vm.provision "ansible" do |ansible|
      ansible.limit = "plu1"
      ansible.playbook = "./playbook.yml"
      ansible.inventory_path = "inventory"
      ansible.raw_arguments = ["--flush-cache"]
      ansible.verbose = "-vvvv"
    end
end
To unsubscribe from this group and all its topics, send an email to vagra...@googlegroups.com.

jason.wi...@gmail.com

unread,
Aug 12, 2020, 2:28:28 PM8/12/20
to Vagrant
Hmm, that would make sense.
Let me modify the vagrantfile and a little and respond with my latest results
THanks for all the help!

jason.wi...@gmail.com

unread,
Aug 12, 2020, 4:26:13 PM8/12/20
to Vagrant
Yea, definitely running into a issue where the box was not up yet and ansible was trying to execute. That seems to be I fixed that. However it appears that my ansible section is not executing for some reason.
Here is my Vagrantfile:
Vagrant.configure("2") do |config|
  #config.vm.synced_folder "../data", "/vagrant_data"
  (1..NODE_COUNT).each do |index|
    config.vm.define "proxy0#{index}" do |subconfig|

      subconfig.ssh.private_key_path = ["/Users/jason/.ssh/vagrant", "~/.vagrant.d/insecure_private_key"]
      subconfig.vm.provision "file", source: "/Users/jason/.ssh/vagrant.pub", destination: "~/.ssh/authorized_keys"
      subconfig.ssh.insert_key = false
      #subconfig.ssh.username = "vagrant"
      #subconfig.ssh.password = "vagrant"
      #subconfig.ssh.insert_key = false
      subconfig.vm.provider "virtualbox" do |vb|
        vb.name = "proxy0#{index}"

        vb.memory = 1024
        vb.cpus   = 1
      end
    subconfig.vm.box = PLUS_IMAGE
    subconfig.vm.hostname = "proxy0#{index}"
    subconfig.vm.network :private_network, ip: "172.16.10.#{index + 20}"
    subconfig.vm.network "forwarded_port", guest: 80, host: "#{HA_PORT_START + index}"
    subconfig.vm.network "forwarded_port", guest: 8080, host: "#{DASHBOARD + index}"

    subconfig.vm.provision "os-setup", :type => "shell", :path => "provisioners/scripts/os-setup.sh"
    subconfig.vm.provision "setup-hosts", :type => "shell", :path => "provisioners/scripts/setup-hosts.sh" do |s|
      s.args = ["enp0s8"]
    end

    if config == NODE_COUNT
      config.vm.provision "ansible" do |ansible|
        #ansible.limit = "all"

        ansible.playbook = "./provisioners/ansible/mainplus.yml"
        ansible.inventory_path = "./provisioners/ansible/inventory"
    end
  end
 end
end

You can see I put the if statement below, but it does not seem to be triggering (I tried with and without the # in front of ansible.limit).
Is that in the right place?
I feel like its something small and i am very close.
Thanks for the help.

Jason

D Morgan

unread,
Aug 12, 2020, 4:53:34 PM8/12/20
to Vagrant
if index == NODE_COUNT then

      config.vm.provision "ansible" do |ansible|
        #ansible.limit = "all"
        ansible.playbook = "./provisioners/ansible/mainplus.yml"
        ansible.inventory_path = "./provisioners/ansible/inventory"
      end
      
 end

You're probably looking for above.

Incidentally, the language you're writing this in is Ruby. if you want to see output of any kind you can use 

puts 'some message = ', index, NODE_COUNT

which would output something like:

some message = 0, 0

This will help in your debugging.

Lastly, as a FYI, in programming, variable names with ALL_CAPS are typically reserved for constants (variables that are immutable i.e. don't change) or used for Environment variables (e.g. $HOME). While what you wrote works, it might be confusing to someone to see some index count in ALL CAPS changing on every iteration. 



On Wednesday, August 12, 2020 at 9:34:34 AM UTC-4, jason.wi...@gmail.com wrote:

jason.wi...@gmail.com

unread,
Aug 12, 2020, 5:08:19 PM8/12/20
to Vagrant
Got it. Yea, I am still learning Ruby and programming for the most part (lots of shell scripts, javascript, yaml files) but its fun and I enjoy it.

I am a bit baffled because I keep testing and changing things, but it does not seem to be executing my ansible.provision block. Here is my full Vagrantfile in case i missed something (going to change the uppercase letters when I gett his figured out)
```
Vagrant.configure("2") do |config|
  #config.vm.synced_folder "../data", "/vagrant_data"
  (1..NODE_COUNT).each do |index|
    config.vm.define "proxy0#{index}" do |subconfig|
      subconfig.ssh.private_key_path = ["/Users/jason/.ssh/vagrant", "~/.vagrant.d/insecure_private_key"]
      subconfig.vm.provision "file", source: "/Users/jason/.ssh/vagrant.pub", destination: "~/.ssh/authorized_keys"
      subconfig.ssh.insert_key = false
      #subconfig.ssh.username = "vagrant"
      #subconfig.ssh.password = "vagrant"
      #subconfig.ssh.insert_key = false
      subconfig.vm.provider "virtualbox" do |vb|
        vb.name = "proxy0#{index}"
        vb.memory = 1024
        vb.cpus   = 1
      end
    subconfig.vm.box = PLUS_IMAGE
    subconfig.vm.hostname = "proxy0#{index}"
    subconfig.vm.network :private_network, ip: "172.16.10.#{index + 20}"
    subconfig.vm.network "forwarded_port", guest: 80, host: "#{HA_PORT_START + index}"
    subconfig.vm.network "forwarded_port", guest: 8080, host: "#{DASHBOARD + index}"
    subconfig.vm.provision "os-setup", :type => "shell", :path => "provisioners/scripts/os-setup.sh"
    subconfig.vm.provision "setup-hosts", :type => "shell", :path => "provisioners/scripts/setup-hosts.sh" do |s|
      s.args = ["enp0s8"]
    if config == NODE_COUNT then
      config.vm.provision "ansible" do |ansible|

        ansible.limit = "all"
        ansible.playbook = "./provisioners/ansible/mainplus.yml"
        ansible.inventory_path = "./provisioners/ansible/inventory"
    end

    end
  end
 end
end


  (1..NODE_COUNT).each do |i|
    config.vm.define "web0#{i}" do |subconfig|

      subconfig.ssh.private_key_path = ["/Users/jason/.ssh/vagrant", "~/.vagrant.d/insecure_private_key"]
      subconfig.vm.provision "file", source: "/Users/jason/.ssh/vagrant.pub", destination: "~/.ssh/authorized_keys"
      subconfig.ssh.insert_key = false
      #subconfig.ssh.username = "vagrant"
      #subconfig.ssh.password = "vagrant"
      #subconfig.ssh.insert_key = false
      subconfig.vm.provider "virtualbox" do |vb|
          vb.name = "web0#{i}"

        vb.memory = 1024
          vb.cpus   = 1
      end
      subconfig.vm.box = PLUS_IMAGE
      subconfig.vm.hostname = "web0#{i}"
      subconfig.vm.network :private_network, ip: "192.168.10.#{i + 10}"
      subconfig.vm.network "forwarded_port", guest: 80, host: "#{HOST_PORT_START + i}"

      subconfig.vm.provision "os-setup", :type => "shell", :path => "provisioners/scripts/os-setup.sh"
      subconfig.vm.provision "setup-hosts", :type => "shell", :path => "provisioners/scripts/setup-hosts.sh" do |s|
        s.args = ["enp0s8"]
      if config == NODE_COUNT then
        config.vm.provision "ansible" do |ansible|
          ansible.limit = "all"
          ansible.playbook = "./provisioners/ansible/mainfoss.yml"

          ansible.inventory_path = "./provisioners/ansible/inventory"
      end

        end
      end
    end
  end
end
```

Is there a way to 'walkthrough' a Vagrantfile, watch/read the execute parts of the file.

Thanks

D Morgan

unread,
Aug 12, 2020, 5:32:44 PM8/12/20
to Vagrant
config will never == NODE_COUNT

index will == NODE_COUNT


On Wednesday, August 12, 2020 at 9:34:34 AM UTC-4, jason.wi...@gmail.com wrote:

jason.wi...@gmail.com

unread,
Aug 12, 2020, 6:40:03 PM8/12/20
to Vagrant
Ahhh...got it. When I saw that, it clicked.
That did the trick. The playbooks are executing correctly now (when the box is up), now i just need to clean that up a bit. The playbook is getting executed twice, like in a loop for some reason.
Off to that
Thank you for all your help! I really appreciate it!

Cheers!
Reply all
Reply to author
Forward
0 new messages