Ask Your Question

Add the loopback entry to the first line in the /etc/hosts file

asked 2017-04-10 01:09:16 -0600

monalsharma gravatar image


I need to create a module which checks if the /etc/hosts file has the first line with the loopback entry.

If it does, do nothing; else, insert localhost localhost.localdomain

in the first line of the file right after the comments.

I tried using augeas but even if I write something using node(0), it doesn't let me save that file. I tried with augtool using the Hosts lens. Can someone please help me if this is possible or not.

Thanks, Monal

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2017-04-10 09:21:01 -0600

DarylW gravatar image

I would suggest using the native host resource.

host { 'localhost':
  ensure       => 'present',
  ip           => '',
  host_aliases => ['localhost.localdomain'],

( Similar answer to )

edit flag offensive delete link more


But it doesn't make the entry at the first line, if, • When entry is not there - it is inserted in the last • When entry is in some other position other than the first - it makes changes to the entry content but doesn't move to first line

SundarKid gravatar imageSundarKid ( 2017-04-13 04:11:48 -0600 )edit

Thanks for your help DarylW. Unfortunately, the ask is to add the loopback entry as the "first" line in the file. With your solution, here's what happens: When loopback entry does not exist in file: It gets added as the last line (incorrect solution) If it exists (1st or any line), it does nothing.

monalsharma gravatar imagemonalsharma ( 2017-04-17 05:51:04 -0600 )edit

I have been using the host resource, but I also am purging any existing entries, so I haven't run into an issue. One thing that you could possibly do is use the spaceship operator to make the loopback entry happen before all other host resource invocations

DarylW gravatar imageDarylW ( 2017-07-14 08:12:03 -0600 )edit

answered 2017-07-14 05:06:57 -0600

monalsharma gravatar image

The best solution to this question is here:

I found the answer to this question after some research and hit & trial with augeas insert function.

I was able to insert a line using augeas which was the correct option in my case. So something like this:

augeas{'update loopback in hosts file':
context => "/files${hostsfile}",
onlyif => "match /files/etc/hosts/1/ipaddr[.=''] size == 0",
changes => [
"ins 01 before /files/etc/hosts/1",
"set /files/etc/hosts/01/ipaddr ${::ipaddress_lo}",
'set /files/etc/hosts/01/canonical localhost',
'set /files/etc/hosts/01/alias localhost.localdomain',
Hope this helps someone else as well.

edit flag offensive delete link more


Or just distribute a file resource (generated by template). Usually all /etc/hosts look alike.

Kai Burghardt gravatar imageKai Burghardt ( 2017-07-14 08:07:48 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-04-10 01:09:16 -0600

Seen: 1,254 times

Last updated: Jul 14 '17