Passing special characters to exec's command - syntax help!!

464 views
Skip to first unread message

Julia Smith

unread,
Aug 14, 2012, 8:05:44 AM8/14/12
to puppet...@googlegroups.com
Hi

I want to use exec to create a recursive directory (I know I can use file but for another reason I don't want to)

So as a variable I get passed in $home and it may be for example /first/second/userdirectory where first and second may not exist.

Puppet's user wraps useradd so it won't recursively create my directory so I want to use exec to create the directory /first/second before the user is created.
If I allow puppet to recursively create /first/second/userdirectory in it's entirety with file.....when the user is created it doesn't contain the .bashrc and .profile files which I want.

So I wanted to do a simple exec command which does the following if I were to do it at the command line:

mkdir -p ${home%/*}

...this would make /first/second and I can allow useradd create the userdirectory.

BUT....puppet doesn't like me passing that to command.

exec { "$home.create":
      path      => '/bin:/usr/bin',
      command   => "mkdir -p '${home%/*}'",
    }

I get Could not match %/*}\"

Is this possible and if so what is the correct syntax? I've tried lots of options.
Thanks in advance
Julia



This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail.

earthgecko

unread,
Aug 14, 2012, 8:44:15 AM8/14/12
to puppet...@googlegroups.com
There are a number of ways to achieve that.

One would be to.

file { [ '/first', '/first/second' ]:
  ensure => directory,
  owner  => 'root',
  group  => 'root',
  mode  => '0755',
  before => User['julia'],
}

user { 'juila':
  ensure     => present,
  gid          => 'julia',
  groups    => ['users'],
#  uid        => '7000',
  comment => 'This user was created by puppet',
  require    => File['/first/second'],
#  managehome => true,
    }

Now you can declare a require or before, you do not have to specify both, but if you do it will not break.

As for the passing special characters.  I will post something related now.

earthgecko

unread,
Aug 14, 2012, 8:54:15 AM8/14/12
to puppet...@googlegroups.com
So as a variable I get passed in $home and it may be for example /first/second/userdirectory where first and second may not exist.

So I wanted to do a simple exec command which does the following if I were to do it at the command line:

mkdir -p ${home%/*}

...this would make /first/second and I can allow useradd create the userdirectory.

BUT....puppet doesn't like me passing that to command.

exec { "$home.create":
      path      => '/bin:/usr/bin',
      command   => "mkdir -p '${home%/*}'",
    }

I get Could not match %/*}\"



So firstly in the context of

command   => "mkdir -p '${home%/*}'",

Puppet will try and look up variable:

home%/*

Because it is ${ and double quoted, so puppet is trying to compile the command as it is ${ in a double quoted string which puppet understands as a variable.

I do not understand the context of the % percent and * wildcard references.  But is $home was defined as '/first/second', then:

command   => "mkdir -p ${home}",

As for passing special characters, different blends of variables and special characters should always be tested.  Special characters should be passed via the exec command context if single quoted and no variables, not 100% certain, it always pays to test.
 

Julia Smith

unread,
Aug 14, 2012, 9:18:57 AM8/14/12
to puppet...@googlegroups.com
Hi
Thanks for your response.
The first answer won't work for me as I want to be dynamic about it and take in a variable of the full path so it could just as easily be 10 directories deep. I'm getting the value for home from a CLI rather than hand crafting something static.

The significance of the %/* means take the variable's value and right strip back to the last '/' character. So where I have /first/second/userdir passed in in the variable $home,
I want to create only /first/second with the mkdir -p command.

Julia

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/4QCHahZc-MYJ.

To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.

earthgecko

unread,
Aug 14, 2012, 10:21:36 AM8/14/12
to puppet...@googlegroups.com
OK then.

    command => "mkdir -p $(dirname ${home})",

That does it.


Reply all
Reply to author
Forward
0 new messages