1

I'm fairly new to Augeas, but I've been told it is the tool for modifying config with puppet.

I'm trying to create a simple class that adds a line to /etc/hosts if it doesn't already exist.

    augeas { "test_config":
             context => "/files/etc/hosts/01/",
             changes => [
                         "set ipaddr 192.168.100.3",
                         "set canonical test.localdomain",
                         "set alias[1] test",
                        ],

This creates the line i'm after.

The hosts file looks like this

127.0.0.1       localhost       localhost.localdomain localhost4 localhost4.localdomain4
::1     localhost       localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.3     test.localhost test
192.168.0.4     badger.oam.eeint.co.uk badger

As I don't want duplicates, I created a match line in augtool to get match.

augtool> match /files/etc/hosts/3/alias /files/etc/hosts/3/alias = test

My current config seems to be unpredictable at best

    augeas { "test_config":
             context => "/files/etc/hosts/*/",
             changes => [
                         "set ipaddr 192.168.100.3",
                         "set canonical test.localdomain",
                         "set alias[1] test",
                        ],

            onlyif => "match alias 'test'",
           }

Can anyone help nudge me in the right direction on this one?

Giacomo1968
  • 3,542
  • 27
  • 38
Matt
  • 69
  • 2
  • 9

2 Answers2

2

To manage host entries, you should use the host resource type.

This type comes by default with Puppet, but you can also use it with Augeas (which I would recommend) by using the augeasproviders module (and in particular, the augeasproviders-base module).

raphink
  • 11,987
  • 6
  • 37
  • 48
  • I'm going to take a look at [augeasproviders](http://augeasproviders.com). However I think I had failed before that point, mainly with the way in which config is tested using onlyif. Thanks for your help though, it's much appreciated. – Matt Dec 08 '14 at 16:35
  • Having looked at augeasproviders, and given I won't be writing my own lenses anytime soon, it looks like the tidiest solution. Many thanks, I think this is the best solution. – Matt Dec 08 '14 at 16:48
0

Why not use the exec resource?

exec{'serverfault demo':
     command => 'echo 192.168.100.3       test.localdomain >> /etc/hosts',
     unless => 'grep test.localdomain /etc/hosts',
     path => ['/bin','/usr/bin'],
}

This will run the echo command and add that line only if the line 192.168.100.3 test.localdomain does not exist in /etc/hosts.

Sreeraj
  • 464
  • 1
  • 5
  • 15
  • I should have been clearer, this is about learning Augeas with a simple test rather than adding the hosts themselves. I do appreciate your answer though, as I hadn't considered that particular method of solving it. – Matt Dec 08 '14 at 16:33
  • `exec` resources should be avoided at all cost, because there is no garantee of idempotence. They should only be used if you have no other choice (e.g. `apt-get update`). If anything, use `file_line` before you get to this extreme. – raphink Dec 09 '14 at 12:49
  • @ℝaphink I beg to differ there. Under this particular instance the idempotent part is taken care of by the `unless` condition. Also, in this particular context, file_line is no different that using `exec` and guarantees no idempotence. – Sreeraj Dec 09 '14 at 15:51
  • OP wants to manage an alias in addition to the canonical name. This makes it harder to ensure idempotence, lest you end up grep for regexps in your unless statement. Generally speaking, it's better practice to rely on existing resource ruby types, that manage edge cases better (and are actually thoroughly tested for them). – raphink Dec 09 '14 at 22:40