File_line only if the file exists

2,143 views
Skip to first unread message

mike r

unread,
Jul 8, 2016, 1:01:28 PM7/8/16
to Puppet Users
Quick question, Im writing a module that makes sure file_line exists but cant figure out how to only apply this if the target file exists, heres the module so far


$file = '/etc/modprobe.d/CIS.conf'

file { $file :
  ensure => file,
  mode   => '0600',
  owner  => 'root',
  group  => 'root',
}


file_line { "(1.1.18) ${file} - cramfs":
  ensure  => present,
  path    => $file,
  line    => 'install cramfs /bin/true',
  require => File[$file],
}

file_line { "(1.1.19) ${file} - freevxfs":
  ensure  => present,
  path    => $file,
  line    => 'install freevxfs /bin/true',
  require => File[$file],
}


I added the Require for each file_line but when I test on a node I get this,

Notice: /Stage[main]/Cis_rhel7::Rule::Rule_1_1_18/File[/etc/modprobe.d/CIS.conf]/ensure: current_value absent, should be file (noop)
Error: /Stage[main]/Cis_rhel7::Rule::Rule_1_1_18/File_line[(1.1.18) /etc/modprobe.d/CIS.conf - cramfs]: Could not evaluate: No such file or directory @ rb_sysopen - /etc/modprobe.d/CIS.conf
Error: /Stage[main]/Cis_rhel7::Rule::Rule_1_1_18/File_line[(1.1.19) /etc/modprobe.d/CIS.conf - freevxfs]: Could not evaluate: No such file or directory @ rb_sysopen - /etc/modprobe.d/CIS.conf
Error: /Stage[main]/Cis_rhel7::Rule::Rule_1_1_18/File_line[(1.1.20) /etc/modprobe.d/CIS.conf - jffs2]: Could not evaluate: No such file or directory @ rb_sysopen - /etc/modprobe.d/CIS.conf

Error: /Stage[main]/Cis_rhel7::Rule::Rule_1_1_18/File_line[(1.1.21) /etc/modprobe.d/CIS.conf - hfs]: Could not evaluate: No such file or directory @ rb_sysopen - /etc/modprobe.d/CIS.conf
Error: /Stage[main]/Cis_rhel7::Rule::Rule_1_1_18/File_line[(1.1.22) /etc/modprobe.d/CIS.conf - hfsplus]: Could not evaluate: No such file or directory @ rb_sysopen - /etc/modprobe.d/CIS.conf
Error: /Stage[main]/Cis_rhel7::Rule::Rule_1_1_18/File_line[(1.1.23) /etc/modprobe.d/CIS.conf - squashfs]: Could not evaluate: No such file or directory @ rb_sysopen - /etc/modprobe.d/CIS.conf
Error: /Stage[main]/Cis_rhel7::Rule::Rule_1_1_18/File_line[(1.1.24) /etc/modprobe.d/CIS.conf - udf]: Could not evaluate: No such file or directory @ rb_sysopen - /etc/modprobe.d/CIS.conf
Info: Class[Cis_rhel7::Rule::Rule_1_1_18]: Unscheduling all events on Class[Cis_rhel7::Rule::Rule_1_1_18]

All my resources are have a resource default of NOOP, since Im doing a compliance check. Cant figure out how to make File_line only get applied if the file exists.

Brent Gerig

unread,
Aug 2, 2016, 11:44:56 AM8/2/16
to Puppet Users
Did you ever find a solution for this? I'd like to do the same thing.

Bret Wortman

unread,
Aug 2, 2016, 12:04:22 PM8/2/16
to Puppet Users
I know that file_line should autorequire the file since it's being managed, so the "require"s aren't strictly necessary.

To test for the file's existence, you'll need to write a custom fact. See this for suggestions: http://stackoverflow.com/questions/18784329/how-to-test-for-existence-of-a-file-on-the-puppet-master


Bret


On Friday, July 8, 2016 at 1:01:28 PM UTC-4, mike r wrote:
Reply all
Reply to author
Forward
0 new messages