Search code examples
puppet

Reading from file /etc/resolv.conf and populating in named.conf.options


I'm using puppet to generate my named.conf.options file, in order to do this I'd like it to use the forwarders defined in /etc/resolv.conf. What's the best way of doing this, I've been doing it like this (where named.conf.options.erb contains ) - but this runs constantly.

file { '/etc/bind/named.conf.options':
    ensure => present,
    content => template('my_template_location/named.conf.options.erb'),
    replace => true,
}
->
exec { "add_nameserver":
    command => '/bin/sed -i "s/<name_server>/$(grep nameserver/etc/resolv.conf | tr -d [a-z])/g" /etc/bind/named.conf.options',
}

Solution

  • An exec will always run unless it has something to limit it. There are a number of parameters you can set.

    In your case, it sounds like you want the exec to run only when your file changes. You might want to use the refreshonly parameter on your exec.

    First, change the require arrow to a notify arrow, from -> to ~>. This will cause puppet to refresh the exec whenever the file changes.

    Second, add refreshonly => true to your exec. This will cause the exec to only run when it is refreshed by some other resource.

    You'll end up with the following:

    file { '/etc/bind/named.conf.options':
        ensure  => present,
        content => template('my_template_location/named.conf.options.erb'),
        replace => true,
    }
    ~>
    exec { "add_nameserver":
        command     => '/bin/sed -i "s/<name_server>/$(grep nameserver/etc/resolv.conf | tr -d [a-z])/g" /etc/bind/named.conf.options',
        refreshonly => true,
    }
    

    You can check out some of the other ways to limit an exec on the Puppet Type Reference Page.