从文件/etc/resolv.conf读取并填充named.conf.options

时间:2015-04-24 15:18:18

标签: puppet

我正在使用puppet生成我的named.conf.options文件,为了做到这一点,我希望它使用/etc/resolv.conf中定义的转发器。这样做的最好方法是什么,我一直在这样做(其中named.conf.options.erb包含) - 但这种情况经常发生。

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',
}

2 个答案:

答案 0 :(得分:2)

除非有限制,否则exec将始终运行。您可以设置许多参数。

在您的情况下,听起来您希望exec仅在您的文件更改时运行。您可能希望在exec上使用refreshonly参数。

首先,将需求箭头更改为通知箭头,从->更改为~>。这将导致puppet在文件更改时刷新exec。

其次,将refreshonly => true添加到您的执行官。这将导致exec仅在其他资源刷新时才会运行。

您最终会得到以下信息:

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,
}

您可以查看一些限制执行Puppet Type Reference Page的其他方法。

答案 1 :(得分:1)

您无法以这种方式获得所需的状态,因为您正在使用两个不同的声明修改相同的资源(文件/etc/bind/named.conf.options)。

通常你必须避免在Puppet中使用exec资源,因为在执行&#34; old-school&#34;时很难保持状态和幂等性。命令。

因此,获得所需行为的最佳方法是创建一个自定义事实[1],将您的名称服务器公开给任何资源,然后将其包含在您的ERB模板中。

Facter.add(:nameservers_array) do
  setcode do
    nameservers = Facter::Core::Execution.exec('grep nameserver/etc/resolv.conf | tr -d [a-z]')
    nameservers_array = nameservers.split(" ")
    nameservers_array
  end
end

您还有另一个例子:https://www.jethrocarr.com/2013/11/05/exposing-name-servers-with-puppet-facts/

[1] https://docs.puppetlabs.com/facter/latest/fact_overview.html

相关问题