__hosts: modification results in duplicate lines #40

Open
opened 2021-11-20 11:25:05 +00:00 by ungleich-gitea · 5 comments

Changing

__hosts example.com --ip 1.2.3.4

to

__hosts example.com --ip 1.2.3.4 --alias example

results two lines in /etc/hosts:

1.2.3.4	example.com # _hosts/example.com
1.2.3.4	example.com	example  # __hosts/example.com

I couldn't find a way to work around this.
It seems like a limitation of __line to me.

Any ideas how this could be addressed?

Changing __hosts example.com --ip 1.2.3.4 to __hosts example.com --ip 1.2.3.4 --alias example results two lines in `/etc/hosts`: 1.2.3.4 example.com # _hosts/example.com 1.2.3.4 example.com example # __hosts/example.com I couldn't find a way to work around this. It seems like a limitation of `__line` to me. Any ideas how this could be addressed?
Author
Owner

mentioned in issue #842

mentioned in issue #842
Author
Owner

@poljakowski: /etc/hosts is not that simple…

The hostname cannot be considered unique. The default /etc/hosts in most distros will likely include something like this:

127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback

Hostname + IP could maybe be considered unique.

There is also a difference between aliases and multiple entries.
This can be seen when checking out the gethostby*(3) functions, which return a struct hostent (which contains the hostname and aliases.)

Given these lines in /etc/hosts:

1.2.3.4	test1.example.com test1
2.3.4.5	test2.example.com
2.3.4.5	test2

the difference can be seen with getent(1):

$ getent hosts test1
1.2.3.4	test1.example.com test1
$ getent hosts test2
2.3.4.5	test2
$ getent hosts 1.2.3.4
1.2.3.4	test1.example.com test1
$ getent hosts 2.3.4.5
2.3.4.5	test2.example.com

Edit: Another interesting thing about /etc/hosts can be observed if say example.com resolves to fc00::1234 using DNS:

$ grep ^hosts /etc/nsswitch.conf
hosts:          files dns
$ grep example.com /etc/hosts
1.2.3.4	example.com
$ getent hosts example.com
fc00::1234	example.com
@poljakowski: `/etc/hosts` is not that simple… The hostname cannot be considered unique. The default `/etc/hosts` in most distros will likely include something like this: ``` 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback ``` Hostname + IP could maybe be considered unique. There is also a difference between aliases and multiple entries. This can be seen when checking out the `gethostby*(3)` functions, which return a `struct hostent` (which contains the hostname and aliases.) Given these lines in `/etc/hosts`: ``` 1.2.3.4 test1.example.com test1 2.3.4.5 test2.example.com 2.3.4.5 test2 ``` the difference can be seen with `getent(1)`: ```console $ getent hosts test1 1.2.3.4 test1.example.com test1 $ getent hosts test2 2.3.4.5 test2 $ getent hosts 1.2.3.4 1.2.3.4 test1.example.com test1 $ getent hosts 2.3.4.5 2.3.4.5 test2.example.com ``` Edit: Another interesting thing about `/etc/hosts` can be observed if say `example.com` resolves to `fc00::1234` using DNS: ```console $ grep ^hosts /etc/nsswitch.conf hosts: files dns $ grep example.com /etc/hosts 1.2.3.4 example.com $ getent hosts example.com fc00::1234 example.com ```
Author
Owner

changed title from __hosts: {---alias -}modification results in duplicate lines to __hosts: modification results in duplicate lines

changed title from **__hosts: {---alias -}modification results in duplicate lines** to **__hosts: modification results in duplicate lines**
Author
Owner

@ssrq Also take a look at the following discussion: https://code.ungleich.ch/ungleich-public/cdist/-/merge_requests/465.
I think that Dmitry purposely didn't support alias, multiple names are managed by multiple type executions resulting in consistent hosts file.

@ssrq Also take a look at the following discussion: https://code.ungleich.ch/ungleich-public/cdist/-/merge_requests/465. I think that Dmitry purposely didn't support alias, multiple names are managed by multiple type executions resulting in consistent hosts file.
Author
Owner

@ssrq Is the above change: first I used hosts type without alias, run cdist config; then I changed usage of hosts type so that I added alias and run cdist config?
If yes, then this cannot be done with this type which uses line type.

You would have to use first hosts usage with absent then the second usage to add new line.

Or reimplement type/replace usage of line type with logic that detects such cases and appends new alias to the same hosts line.

@ssrq Is the above change: first I used hosts type without alias, run cdist config; then I changed usage of hosts type so that I added alias and run cdist config? If yes, then this cannot be done with this type which uses line type. You would have to use first hosts usage with absent then the second usage to add new line. Or reimplement type/replace usage of line type with logic that detects such cases and appends new alias to the same hosts line.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: ungleich-public/cdist#40
No description provided.