Ask Your Question

How to add a line to file only if directory exists, else do nothing

asked 2016-12-02 10:46:10 -0500

jrbast gravatar image

What would I use to check if a directory exists for true/false answer without generating an error. I can't use file, because ensure => present or absent with create or remove the directory.

Then use only a true answer to add line to a file with file_line like this:

if ( $answer == true ) { fileline { 'addparameter' : path => '/dir1/config_file', line => "line to append", }

edit retag flag offensive close merge delete



is this a directory that you are creating somewhere with puppet, or do you expect it to be pre-existing? If so, create a custom fact. (The real answer is you should manage state with puppet and make it declarative, trying to do other things like that are made hard by the language for a reason)

DarylW gravatar imageDarylW ( 2016-12-02 13:01:08 -0500 )edit

you can then use the custom fact (for a specific directory) to either apply or not apply the resource. I strongly encourage you to manage it in a different way

DarylW gravatar imageDarylW ( 2016-12-02 13:02:06 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-12-03 12:11:38 -0500

updated 2016-12-03 12:14:34 -0500

you write it as a (shell) script

$_l = 'line to append'
$_f = '/dir1/config_file'
$_c = @("EOT"/L$)
  /usr/bin/test -d /dir0/ && \
    /bin/grep -q '^${_l}\$' ${_f} || \
    /bin/echo '${_l}' >> ${_f}

exec { 'config_file':
  command  => $_c,
  provider => 'shell',
edit flag offensive delete link more

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: 2016-12-02 10:46:10 -0500

Seen: 50 times

Last updated: Dec 03 '16