On 03/05/17 18:28, Samir Gahirwal wrote:
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.


You probably want:

  if !($env == 'test' or $env == 'qa) {
     #
  }

i.e. "neither of", which you can also write as

  unless $env == 'test' or $env == 'qa' {
  }

Or do the reverse and move the logic you have in the "else" part to the if part and drop the "not".

i.e.

  if $env == 'test' or $env == 'qa {
    # what you have in else body
  } else {
    # what you now have in if body
  }

That is much easier to read.


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 (

You cannot use hyphens ('-') in names. Change to use underscore for example "certificate_generator".

Best,
- henrik

      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.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

--
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/oedi86%24i02%241%40blaine.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to