Thanks for your help Rob! 

Yes even, I have second doubts about if statement, I will find some other 
way to fix this conditional statement. 


On Wednesday, May 3, 2017 at 6:21:52 AM UTC-7, Rob Nelson wrote:
>
> I think some characters got gobbled during pasting or by Google Groups; it 
> looks like the parameter block to your type is missing the start/finish. 
> But that's syntax, I'm sure you've got it right or can fix any typos 
> yourself.
>
> The suggestion I have is about a common anti-pattern: Don't Repeat 
> Yourself (DRY). You have two exec resource declarations, only of which is 
> instantiated, with the same parameter list for both. The only difference is 
> interpolation of the password, which is set inside the control flow. Moving 
> the exec instantiation *after* the conditional blocks helps with 
> maintenance in the long term, but also makes it easier for someone to read 
> your code later (including future you!) and see that the end result is a 
> single resource. That would look something like this:
>
> define certificate-generator (
>   user => root,
>   group => root,
> ) {
>   if ( $env != 'test' or $env != 'qa') {
>     case $::target_cert_type {
>       'type1': {
>         $password = '123'
>       }
>       'type2': {
>         $password = '345'
>       }
>       default: {
>         fail("No password defined")
>       }
>     }
>   }
>   else {
>     case $::target_cert_type {
>       'type1': {
>         $password = '567'
>       }
>       'type2': {
>         $password = '789'
>       }
>       default: {
>         fail("No password defined ")
>       }    
>     }
>   }
>
>   # Single exec resource here with variable inputs determined above
>   exec { "certificate-generator":
>     command => 'sh certgen.sh',
>     path    => '/bin/bash',
>     logoutput => true,
>     onlyif =>  "test ! -f ${cert_basedir}/${host}.jks"
>     source => "puppet:///modules/mymodule/certgen/certgen.sh $password"
>   }    
> }
>
> I still have a concern about your if statements, though. If `env` is 
> `prod`, it obviously matches for if ( $env != 'test' or $env != 'qa'). 
> But if `env` is `qa`, it STILL matches because $env != 'test'
> is true and it never reaches the $env != 'qa' comparison. And if it's 
> `test`, it still matches because after failing the first test, it passes 
> the second test. The `else` condition is never reached that I can see. 
> Changing that to an AND match, or using equality and swapping the if/else 
> blocks, appears closer to the described desired state.
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/b207bbc1-8ca7-4fce-886e-96edc7230557%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to