How to assign a variable value after executing a command

asked 2017-11-22 14:31:27 -0600

ranjan gravatar image

updated 2017-11-23 10:11:43 -0600

stivesso gravatar image

I am trying to retrieve the Build Number from the version.txt before and after transferring the file. Below is my exercise, please let me know how can I achieve this. Thanks in advance

Sequence expected is retrieve build number -> tranfer version.txt to /tmp/ -> retrive build number

example.pp

class example1
 {
  notice('example1')
  $version = generate ("/bin/bash", "-c", "/bin/grep 'Build Number' /root/example/version.txt | awk '{print \$3}'")
  notice("The install example1 version is: ${version}")
  exec{'Copy version.txt':
  command => "/bin/cp -r /root/example/version.txt /tmp/",
  onlyif => ['/usr/bin/test -e /root/example/version.txt'],
  }
 }

class example2
 {
  notice('example2')
  $version2 = generate ("/bin/bash", "-c", "/bin/grep 'Build Number' /tmp/version.txt | awk '{print \$3}'")
  notice("The install example2 version is: ${version2}")
  if !('none' in $version2)
  {
    notice('would like to do something here')
  }
 }
class{'example1': } ->
class{'example2': }

version.txt
Example1
Build Number: 5.1.65.1-294578

Puppet apply output

root@ranjan3:~/example# puppet apply example.pp
Warning: Could not retrieve fact fqdn
Notice: Scope(Class[Example1]): example1
Notice: Scope(Class[Example1]): The install example1 version is: 5.1.65.1-294578

Notice: Scope(Class[Example2]): example2
Notice: Scope(Class[Example2]): The install example2 version is: /bin/grep: /tmp/version.txt: No such file or directory

Notice: Scope(Class[Example2]): would like to do something
Notice: Compiled catalog for admin3 in environment production in 0.10 seconds
Notice: /Stage[main]/Example1/Exec[Copy version.txt]/returns: executed successfully
Notice: Finished catalog run in 0.16 seconds
root@admin3:~/example#

Thanks in advance

edit retag flag offensive close merge delete

Comments

I am not able to format my exercise code.

ranjan gravatar imageranjan ( 2017-11-22 14:32:45 -0600 )edit

a) Your `/root/example/` directory is mounted via network? Or, b) do you want to configure your Puppet server only? I guess not, because then the whole set-up reduces to nonsense, so: c) apparently you're not aware that generate() is executed on the Puppet Server, _not_ the client.

Kai Burghardt gravatar imageKai Burghardt ( 2017-11-23 11:25:27 -0600 )edit

Hi Ranjan, If you check the debug output (run puppet apply -d example.pp), you will note that the generate function calls are executed before the exec is applied. And that is expected as Variables are evaluated before resources are applied.

stivesso gravatar imagestivesso ( 2017-11-23 11:28:38 -0600 )edit

When you run ‘apply’, your local machine is acting as the server, in a standard puppet configuration (not masterless), this wont behave as you expect

DarylW gravatar imageDarylW ( 2017-11-26 22:46:20 -0600 )edit

I believe all generate runs happen in the order the code is executed (which happens before the catalog is generated, and before the resources from that catalog are applied. This is not imperative. Top down programming like a bash script.

DarylW gravatar imageDarylW ( 2017-11-26 22:51:02 -0600 )edit