chrekh / puppet-hosts

A template-based module to manage /etc/hosts
5 stars 8 forks source link

Do not add entry if it's primary_name #23

Open PandemiK911 opened 3 years ago

PandemiK911 commented 3 years ago

Hello, Shouldn't pass an entry if the hostname is the same as primary_name ?

As an example, we're using this module on a cluster. Each machine use a public network and a private one. We want all traffic to go through the private network so all nodes has been added as entries. We end up with a hosts file like this on node01 :

1.2.3.1 node01.domain.com node01 dead::beef node01.domain.com node01

10.10.10.1 node01.domain.com node01 10.10.10.2 node02.domain.com node02 and so on ...

This create a duplicate entry for the local node. AFAIK the first one is used by this could potentially create strange behavior right ?

A good practice would be to filter entries corresponding to primary_name.

Sorry, I tried to create the code in the template file but it far above my skills.

Thanks !

chrekh commented 3 years ago

Hi, thanks for your report, I will investigate thorough later and se if I should change something. For my own use I do want to have the my primary hostname on all of it's IP addresses.

Perhaps you could try using hosts::entries? Or using one_primary_ipv4 or one_primary_ipv6 (which at the moment is kind of buggy non-deterministic, see

20) that will be fixed in the next release.

Regarding the template. Yes it's horrible, I have planned to rewrite it using epp instead of erb (see #17), and alro rip out the horrible ruby-code to collect all local addresses, that was needed when I first wrote this, but not needed with newer structured fact. (see #18)

chrekh commented 3 years ago

I'm trying to understand what you need, my best guess is that you have added the servers hostname (as in $hostname and $fqdn) as a separate entry using hosts::entries, and you don't want it to occur both under the heading "# Primary address" and further down under "# Additional entries"?

I have some suggestions you could try

1) Set enable_ipv4=false and enable_ipv6=false

This will prevent all content under the first heading, but add all hosts::entries. (you'll need to add 127.0.0.1 and ::1 to hosts::entries also in that case)

2) Set primary_names to something other than default ($hostname,$fqdn)

3) Add regexps to exclude_ipv4 and exclude_ipv6 that matches the primary IP you don't want duplicate of

It will only be excluded under the heading "# Primary address" and not "# Additional entries"

I have now mostly finished the rewriting (merged to master branch which will be released ass 4.0.0) I just need some more testing, and update Changelog before release.

I have also added lots of testcases, including one for suggestion 1, with

      let(:params) do
        {
          enable_ipv4: false,
          enable_ipv6: false,
          entries: {
            '10.10.10.1' => [
              'foo.example.org'
            ],
            '127.0.0.1' => [
              'localhost'
            ],
            '::1' => [
              'localhost'
            ]
          }
        }
      end

Which results in

# Managed by puppet module hosts.

# Loopback

# Primary address

# Additional entries (use hosts::entries to add)
10.10.10.1      foo.example.org
127.0.0.1       localhost
::1         localhost