exec check for directory

34 views
Skip to first unread message

linux....@gmail.com

unread,
Jan 5, 2016, 6:51:04 AM1/5/16
to Puppet Users
Hi,

I am writing a module to place a file into a directory only if the directory exist. If the directory or source does not exist it should simply ignore without throwing an error message. However I get an error saying that the file could not be created. Can anyone please help me to find out what is wrong with the below code?

err: /File[/home/user/.bashrc]/ensure: change from absent to file failed: Could not set 'file on ensure: cannot generate tempfile
 
define test::user (username = $user){

file { "/home/${name}/.bashrc":
       ensure  => file,
       source  => ["puppet:///modules/test/${fqdn}_${$user}_bashrc" , 'puppet:///modules/test/bashrc-default'],
       before  =>  Exec ["${user}_check"],
}

exec {"${user}_check":
      command => '/bin/false',
      provider => shell,
      unless => "/usr/bin/test -d /home/$name",
    }

Thank You

Lowe Schmidt

unread,
Jan 5, 2016, 7:30:03 AM1/5/16
to puppet...@googlegroups.com
You will always create the file regardless of the before=>[] block, before just makes sure to realise what is referenced in the before block and then do what is in the file block.

What are you trying to accomplish?

Putting a .bashrc in place if a user exist? 



--
Lowe Schmidt | +46 723 867 157

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/e5de03ec-c7ae-4b4d-86dd-62270ef2856a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

linux....@gmail.com

unread,
Jan 5, 2016, 7:47:38 AM1/5/16
to Puppet Users
Exactly, that is what I am trying to do. .bashrc should be created only if the user exist and it should not throw errors that the file could not be created for the users that does not exist.

Dirk Heinrichs

unread,
Jan 5, 2016, 8:04:47 AM1/5/16
to puppet...@googlegroups.com
Am 05.01.2016 um 13:47 schrieb linux....@gmail.com:

Exactly, that is what I am trying to do. .bashrc should be created only if the user exist and it should not throw errors that the file could not be created for the users that does not exist.

You could setup a fact array (named "users") by providing a fact script in your class (lib/facter/collect_users.rb) and then refering to it in the code, like for example (Puppet 4):

$facts['users'].each |String $user| {
  file { "/home/${user}/.bashrc":
  ...
  }
}

In Puppet 3 you'd use a "define" instead of the loop.

HTH...

    Dirk
--

Dirk Heinrichs, Senior Systems Engineer, Engineering Solutions
Recommind GmbH, Von-Liebig-Straße 1, 53359 Rheinbach
Tel: +49 2226 1596666 (Ansage) 1149
Email: d...@recommind.com
Skype: dirk.heinrichs.recommind
www.recommind.com

linux script

unread,
Jan 5, 2016, 8:59:44 AM1/5/16
to puppet...@googlegroups.com

Thank you for the response.I am using puppet 2.7 and for me puppet is able to place the file which I am asking to. But it gives a lot of errors for the users which do not exist as it will not be able to place the file for that user and the puppet error log is filled up due to this. Hence, I want some way to suppress that error for the users that does not exist on the server. Also, I am not familer with writing ruby code for this requirement as I am passing the defined variable for user name.

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.

jcbollinger

unread,
Jan 6, 2016, 9:35:00 AM1/6/16
to Puppet Users
You are specifying that the .bashrc file should be managed before the directory check.  This is exactly opposite to what you want.  Switching to the 'require' metaparameter will give you the evaluation order you want.

With that said, this is probably a poor approach overall.  If you are managing user accounts and the contents of their home directories then almost certainly you should manage their home directories as well.  If the home directory were under management then you could either ensure it is present, in which case you wouldn't need a test, or ensure it absent or leave it unmanaged, in which case you could and should avoid attempting to manage any contents.

If you insist on avoiding managing home directories themselves, and yet managing their contents then Puppet's primary mechanism for conditional action based on on machine state revolves around facts.  You can use facts for this even with only string facts.  For example, it would be pretty easy to write a custom fact that provided, say, a colon-delimited list of all the subdirectories of /home, which your manifest code could then examine to determine whether a particular home directory was present.


John

Reply all
Reply to author
Forward
0 new messages