Puppet [SOLVED]: Puppet exec chaining

Puppet [SOLVED]: Puppet exec chaining

Home Forums Automation Tools Puppet Puppet [SOLVED]: Puppet exec chaining

Tagged: 

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #217743

    Cloudy Point
    Keymaster

    QuestionQuestion

    My need is this…

    1. look for changes to /tmp/file-1 file
        a. if there is a change, execute in the following order
            i.   run cmd-3
            ii.  run cmd-2
            iii. run cmd-1
        b. If there is NO change to /tmp/file-1 file, do not run anything
    

    My code is like this…

    exec { 'exec-3':
      command => "sudo cmd-3",
      path    => '/usr/local/bin',
      require => File["/tmp/file-1"],
    }
    
    exec { 'exec-2':
      command => "sudo cmd-2",
      path    => '/usr/local/bin',
      require => Exec['exec-3'],
    }
    
    exec { 'exec-1':
      command     => "sudo cmd-1",
      path        => '/usr/local/bin',
      require     => Exec['exec-2'],
      subscribe   => File["/tmp/file-1"],
      refreshonly => true,
    }
    

    But, whether there is a change to /tmp/file-1 or not, cmd-3 and cmd-2 always runs. How do I prevent it? I do not want “require” to be run in exec-1 when there is no change to /tmp/file-1.

    #217744

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You need: firstly, for all of the execs to subscribe to the file resource; secondly, for each of those to also require their preceding exec resources; and thirdly, for each exec to be set to refreshonly.

    Here is some code that does that:

    Exec {
      path => '/usr/local/bin',
      refreshonly => true,
    }
    
    file { '/tmp/file-1':
      ensure  => file,
      content => "file-1-contentn",
    }
    
    exec { 'exec-1':
      command   => 'sudo cmd-1', 
      subscribe => File['/tmp/file-1'],
    } 
    exec { 'exec-2':
      command   => 'sudo cmd-2',
      subscribe => File['/tmp/file-1'],
      require   => Exec['exec-1'],
    } 
    exec { 'exec-3':
      command   => 'sudo cmd-3',
      subscribe => File['/tmp/file-1'],
      require   => Exec['exec-2'],
    }
    

    Notice that I also refactored out a bit of unnecessary duplication using resource defaults (ref) and changed the quoting per the style guide (ref).

    Source: https://stackoverflow.com/questions/47105184/puppet-exec-chaining
    Author: Alex Harvey
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.