Ask Your Question
0

Conditional Statement

asked 2014-11-20 00:24:00 -0500

jefferyb gravatar image

updated 2014-12-02 10:29:44 -0500

EDIT VERSION:

So, I have this code but it doesn't seem to work... Any ideas???

in my init.pp file:

class example (

  $database_type = 'MYSQL', # Options: MYSQL, ORACLE

  # Settings:
  $connector_filename = $::example::settings::connector_filename,
  $connector_url = $::example::settings::connector_url,

  )  inherits ::example::settings {

  include example::connector

}

in settings.pp

class example::settings {

  # MySQL/ORACLE Connector
  case $example::database_type {
    'MYSQL' : {
      $connector_filename = 'mysql-connector-java-5.1.9.jar'
      $connector_url = 'http://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar'
    }
    'ORACLE'  : {
      $connector_filename = 'ojdbc6.jar'
      $connector_url = '/var/lib/docker/aufs/mnt/*/u01/app/oracle/product/11.2.0/xe/jdbc/lib/ojdbc6.jar'
    }
#    default  : {
#      $connector_filename = 'mysql-connector-java-5.1.9.jar'
#      $connector_url = 'http://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar'
#    }
  }
}

in connector.pp

class example::connector {
  if $example::database_type == 'MYSQL' {
        notify {"Downloading MySQL Connector": }
        notify {"connector_filename value is: $::example::connector_filename": }
        notify {"connector_url value is: $::example::connector_url": }
      }

  if $example::database_type == 'ORACLE' {
        notify {"Downloading Oracle Connector": }
        notify {"connector_filename value is: $::example::connector_filename": }
        notify {"connector_url value is: $::example::connector_url": }
  }
}

Whenever I run puppet agent -t on the node with database_type = 'MYSQL'

Warning: Scope(Class[Example::Settings]): Could not look up qualified variable 'example::database_type'; class example has not been evaluated
Notice: Compiled catalog for jefferyb-puppet in environment production in 0.12 seconds
Notice: Downloading MySQL Connector
Notice: /Stage[main]/Example::Connector/Notify[Downloading MySQL Connector]/message: defined 'message' as 'Downloading MySQL Connector'
Notice: connector_url value is: 
Notice: /Stage[main]/Example::Connector/Notify[connector_url value is: ]/message: defined 'message' as 'connector_url value is: '
Notice: connector_filename value is: 
Notice: /Stage[main]/Example::Connector/Notify[connector_filename value is: ]/message: defined 'message' as 'connector_filename value is: '
Notice: Finished catalog run in 0.87 seconds

and if I switch and run puppet agent -t on the node with database_type = 'ORACLE' I get

Warning: Scope(Class[Example::Settings]): Could not look up qualified variable 'example::database_type'; class example has not been evaluated
Notice: Compiled catalog for jefferyb-puppet in environment production in 0.11 seconds
Notice: Downloading Oracle Connector
Notice: /Stage[main]/Example::Connector/Notify[Downloading Oracle Connector]/message: defined 'message' as 'Downloading Oracle Connector'
Notice: connector_url value is: 
Notice: /Stage[main]/Example::Connector/Notify[connector_url value is: ]/message: defined 'message' as 'connector_url value is: '
Notice: connector_filename value is: 
Notice: /Stage[main]/Example::Connector/Notify[connector_filename value is: ]/message: defined 'message' as 'connector_filename value is: '
Notice: Finished catalog run in 0.66 seconds

So the switch, $example::database_type, in connector.pp is working, but not finding the values, $::example::connector_filename or $::example::connector_url, because Scope(Class[Example::Settings]): Could not look up qualified variable 'example::database_type' for some reason...

Warning: Scope(Class[Example::Settings]): Could not look up qualified variable 'example::database_type'; class example has not been evaluated

Thanks for the help.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-11-25 00:35:42 -0500

GregLarkin gravatar image

Please add a notify resource to settings.pp that shows the value of the databasetype variable and report back here with the results. Also, for consistency with the rest of your code, change the case statement to check the value of $::example::databasetype and let us know if the behavior changes at all.

edit flag offensive delete link more
0

answered 2014-11-25 15:44:25 -0500

jefferyb gravatar image

I added a notify:

  # MySQL/ORACLE Connector
  notify {"database_type is: $::example::database_type":}
  case $example::database_type {
    'MYSQL' : {
      $connector_filename = 'mysql-connector-java-5.1.9.jar'
      $connector_url = 'http://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar'
      notify {"database_type is: $::example::database_type":}
    }
    'ORACLE'  : {
      $connector_filename = 'ojdbc6.jar'
      $connector_url = '/var/lib/docker/aufs/mnt/*/u01/app/oracle/product/11.2.0/xe/jdbc/lib/ojdbc6.jar'
      notify {"database_type is: $::example::database_type":}
    }
    default  : {
      $connector_filename = 'mysql-connector-java-5.1.9.jar'
      $connector_url = 'http://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar'
    }
  }

and it looks like it's not getting the value:

Notice: database_type is: 
Notice: /Stage[main]/Example::Settings/Notify[database_type is: ]/message: defined 'message' as 'database_type is: '
Notice: Finished catalog run in 4.33 seconds
edit flag offensive delete link more

Comments

Please post the code that includes all of your classes and any other supporting code/hiera data. That will help in troubleshooting, too.

GregLarkin gravatar imageGregLarkin ( 2014-11-25 16:30:11 -0500 )edit

I just created a module, example, with just those 3 files to see if maybe there was something else going on, but it's still not working... I updated the original post up there with all the classes, everything that I have in that example module that I just created for testing... Thanks for the help

jefferyb gravatar imagejefferyb ( 2014-12-02 10:37:01 -0500 )edit

Can you switch your code in settings.pp back to "case $database_type"? This post (https://groups.google.com/forum/#!topic/puppet-users/xr1QQM1o6Ac) describes the same problem you have with variable scoping and using the inherits keyword.

GregLarkin gravatar imageGregLarkin ( 2014-12-02 14:30:24 -0500 )edit

I switched back to `case $database_type` and it's still not working for some reason, but I'm not getting Scope(Class[Example::Settings]) warning. I added `notify {"Database type is: $database_type": }` in settings to see what it was getting, but it was coming back empty `Notice: Database type is: `

jefferyb gravatar imagejefferyb ( 2014-12-03 21:23:31 -0500 )edit

I don't understand why connector.pp would get the $example::database_type value, but not settings.pp... Any ideas?

jefferyb gravatar imagejefferyb ( 2014-12-03 21:38:15 -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: 2014-11-20 00:24:00 -0500

Seen: 631 times

Last updated: Dec 02 '14