Ask Your Question
1

exec for loop

asked 2016-02-17 06:31:45 -0500

asktbt gravatar image

Hi,

I try to create a number of folders in one go using a for loop in bash and the exec resource. This might not be the best/only way, but it is the path I am currently on. I only need to create the directories once. Funny enough: the for-loop does not seem to be able to use the bash variable.
This code does not work:

$ puppet apply -e 'exec{"test":
  command => "/bin/bash -c \"for i in {000..099}; do /bin/mkdir -p /tmp/data/$i; done\"",
  provider => shell
}'

Running other commands like /bin/touch testfile will be executed correctly and the file created.

Does anybody see where I made my typo or why $i is not referenced in the for-loop? Do I have to deploy a script and execute that (just to create directories?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2016-02-17 07:18:59 -0500

Correct, it's not the best or right way to solve this sort of problem in Puppet (in Puppet 4, you can do all of this cleanly in the DSL using an iterator).

Your error is that $i is being expanded into an empty string by Puppet. To help you debug these sorts of issues with execs, try passing --debug to puppet apply.

This works:

[root@centos-66-x64 ~]# puppet apply -e 'exec{"test":
  command => "for i in {000..099}; do /bin/mkdir -p /tmp/data/\$i; done",
  provider => shell,
}'

Note that Puppet forks a Bourne Shell for you, so you don't need the bash -c either.

edit flag offensive delete link more
0

answered 2016-02-18 01:19:59 -0500

asktbt gravatar image

Thanks for the solution and the information regariding the additional shell. It basically works, but I get a warning from the style-guide regarding using a variable within double quotes. When I replace the quotes with single quotes and adjust the command, I get a different result of the folders being created (funny):

$puppet apply -e 'exec{"test":
  command => 'for i in {00..99}; do /bin/mkdir -p /tmp/data/$i; done',
  provider => shell,
}'

This will create folders like

1
2
3
99

instead of

01
02
03
...
99

I tried working around it by using a modified command:

for i in {00..99}; do c=000\$i; /bin/mkdir -p /tmp/data/\${c: -2}; done

This worked as well as your version, but did not resolve the styleguide issue. I either trigger a warning of using a variable in a single quoted string or the opposite warning of using a double quoted string without a variable - depending on the quotation used. I did not find any other solution than pushing the code to the puppet server while disabling the styleguid checks. This IMHO clearly shows what kind of an ugly solution this is. I consider this problem done, but wanted to share the outcome.

edit flag offensive delete link more

Comments

That sounds like a bug in puppet-lint. I'd either go discuss the issue at the Lint project, or disable that Lint check.

Alex Harvey gravatar imageAlex Harvey ( 2016-02-18 23:13:23 -0500 )edit

https://github.com/rodjek/puppet-lint/issues/463

asktbt gravatar imageasktbt ( 2016-02-19 06:38:56 -0500 )edit
0

answered 2016-02-18 03:38:52 -0500

c4f4t0r gravatar image

To create one range of folders, I think you don't need a for loop, because the shell can expand the range without loop.

puppet apply -e 'exec { "test": command => "mkdir -p /tmp/one/{1..200}", provider => shell }'
edit flag offensive delete link more

Comments

That is correct and more elegant this way as well. However: Adjusting the " to ' (according to the styleguide) creates folder without leading zero: $ puppet apply -e "exec { "test": command => 'mkdir -p /tmp/one/{1..200}', provider => shell }" Using double quotes instead, it works as exspected

asktbt gravatar imageasktbt ( 2016-02-18 06:35:23 -0500 )edit

Not really. You shouldn't use an exec to create directories at all. It leads to all sorts of other issues. In Puppet 3, I would probably stick all 100 of these resources in Hiera. In Puppet 4, do the loop in the DSL.

Alex Harvey gravatar imageAlex Harvey ( 2016-02-19 07:31:59 -0500 )edit

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

Stats

Asked: 2016-02-17 06:31:45 -0500

Seen: 1,023 times

Last updated: Feb 18 '16