goodhosts / vagrant

Vagrant plugin that use goodhosts to manipulate hosts files
https://rubygems.org/gems/vagrant-goodhosts/
MIT License
17 stars 8 forks source link

vagrant up doesn't create hosts entries unless there are at least 2 aliases defined #36

Closed ajxb closed 3 years ago

ajxb commented 3 years ago

Software Versions

Host OS : Ubuntu 21.04 goodhosts : 1.0.7 vagrant : 2.2.16 vagrant-goodhosts : 1.0.15 virtualbox: 6.1.22

Problem

When I run vagrant up with the vagrant-goodhosts plugin installed I was expecting a hosts entry to be added for the vm.hostname similar to how the vagrant-hostsupdater plugin worked. On further investigation I've found that in order to get the hosts file to update I need to define at least 2 aliases. I've put together a very basic Vagrantfile using the centos/7 box found at Vagrant Cloud.

Example 1 - No Aliases Defined

The following results in no host entries being added when running vagrant up:

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "goodhosts.local"
  config.vm.network :private_network, ip: "192.28.128.4"
end

Output:

ajxb-work@ajxb-bud01:~/Work/goodhosts$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: [vagrant-goodhosts] Checking for host entries
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> default: Setting the name of the VM: goodhosts_default_1622643452164_23247
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /home/ajxb-work/Work/goodhosts/ => /vagrant

Expected result

The hosts file contains an entry for goodhosts.local.

Actual result

The hosts file contains no entry for goodhosts.local.

It should be noted that there was an assumption on my part that this plugin would work in a similar way to the now defunct vagrant-hostsupdater plugin - can you clarify what the behaviour should be in this situation?

Example 2 - One Alias Defined

The following results in no host entries being added when running vagrant up:

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "goodhosts.local"
  config.vm.network :private_network, ip: "192.28.128.4"
  config.goodhosts.aliases = ["goodhosts1.local"]
end

Output:

ajxb-work@ajxb-bud01:~/Work/goodhosts$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: [vagrant-goodhosts] Checking for host entries
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> default: Setting the name of the VM: goodhosts_default_1622643911338_81746
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /home/ajxb-work/Work/goodhosts/ => /vagrant

Expected result

The hosts file contains entries for:

Actual result

The hosts file contains no entry for goodhosts.local or goodhosts1.local.

Example 3 - Two Aliases Defined

The following results in two host entries being added for the aliases only when running vagrant up:

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "goodhosts.local"
  config.vm.network :private_network, ip: "192.28.128.4"
  config.goodhosts.aliases = ["goodhosts1.local", "goodhosts2.local"]
end

Output:

ajxb-work@ajxb-bud01:~/Work/goodhosts$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: [vagrant-goodhosts] Checking for host entries
==> default: [vagrant-goodhosts] - found entry for: 192.28.128.4 goodhosts1.local
==> default: [vagrant-goodhosts] - found entry for: 192.28.128.4 goodhosts2.local
[sudo] password for ajxb-work: 
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> default: Setting the name of the VM: goodhosts_default_1622644034804_60024
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /home/ajxb-work/Work/goodhosts/ => /vagrant

Expected result

The hosts file contains entries for:

Actual result

The hosts file contains entries for:

Mte90 commented 3 years ago

Yes we removed that behaviour some release ago and the readme specify to use the alias parameters (that can include just one domain). That behaviour was creating issues when there was aliases configured with duplicates and so on, so we chosen to remove it.

ajxb commented 3 years ago

Thanks for the quick response, and for clarifying that the plugin does not work the same as vagrant-hostsupdater.

You suggest that I should be able to use only one alias for the hosts file to be updated, but in my testing outlined In example 2 above, if you define only 1 alias the hosts file is not updated.

Presumably this has something to do with the condition found in this piece of code: https://github.com/goodhosts/vagrant/blob/9994ba1f52eb9b21459db4f96fe78fa02813ee9d/lib/vagrant-goodhosts/GoodHosts.rb#L196-L206

if hostnames[ip_address].count() > 1 implies that you'd need at least two entries for it to start parsing the list to populate the return data in hostnames_by_ips.

I suspect that this might also apply to condition at https://github.com/goodhosts/vagrant/blob/9994ba1f52eb9b21459db4f96fe78fa02813ee9d/lib/vagrant-goodhosts/GoodHosts.rb#L187

Mte90 commented 3 years ago

You are right code never lies. Should support at least 1 alias, I will update the code.