dasarp
dasarp

Reputation: 1

Conditional execution of puppet defined resource type through exec

I have a requirement where one exec notifies another exec which notifies a defined resource type (which sets some variables and runs an internal exec). My understanding is that if the second exec fails, the defined resource type should NOT be refreshed. But it does... please let me know what is amiss here..

`

class test {
Exec  {
path => ['/usr/bin','/sbin','/bin'],
user => 'root',
}
  exec {
    "MAIN":
      command     => 'echo "MAIN EXEC FUNCTION OK"',
      onlyif      => 'test ! -f /var/log/no_file',
      logoutput   => true,
  }
  ~>
  exec {
    "SUB":
      command     => 'echo "SUB EXEC FUNCTION OK"',
      onlyif      => 'test -f /var/log/no_file',
      logoutput   => true,
      refreshonly => true,
  }
  ~>
  res_type {'TITLE':}
}
define  res_type () {
  exec {
    "$title":
      command     => 'echo "EXEC IN DEF RESOURCE TYPE"',
      refreshonly => true,
      logoutput   => true,
  }
}
include test

`

Here is the output of the puppet apply run `

puppet apply test.pp
Notice: Compiled catalog for test-server-0 in environment dev in 0.08 seconds
Notice: /Stage[main]/Test/Exec[MAIN]/returns: MAIN EXEC FUNCTION OK
Notice: /Stage[main]/Test/Exec[MAIN]/returns: executed successfully
Notice: /Stage[main]/Test/Exec[SUB]: Triggered 'refresh' from 1 events
Notice: /Stage[main]/Test/Res_type[TITLE]/Exec[TITLE]/returns: EXEC IN DEF RESOURCE TYPE
Notice: /Stage[main]/Test/Res_type[TITLE]/Exec[TITLE]: Triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.64 seconds

`

Upvotes: 0

Views: 1119

Answers (1)

John Bollinger
John Bollinger

Reputation: 180093

My understanding is that if the second exec fails, the defined resource type should NOT be refreshed. But it does... please let me know what is amiss here

But the second Exec does not fail. It is never synced at all, because it is refreshonly, therefore it cannot fail. Refreshing is a separate thing, though whether its command is executed upon refresh is governed by the same criteria.

Moreover, even if the second Exec were applied, it would be very surprising for it to fail. Avoiding running the main command on account of the result of the onlyif command does not constitute a failure -- rather, that's a form of the Exec already being in sync. A failure occurs only if the main command is run, and its exit code is not among those accepted as success (by default, only exit code 0 is accepted as successful).

I'd be inclined to say, however, that because the Exec is never applied, it should not notify the Res_type. With its command not being run even upon refresh, it even more should not notify the Res_type. With that said, the behavior of Execs with respect to events and refreshing has long been a troublesome matter, with a long list of tickets filed against various aspects. See the list of issues related to PUP-1106, for example.

I don't think this particular behavior is the subject of a previous ticket, so you could consider filing one, but you probably should also find a better way to go about what you're trying to do. Often Execs are expedient, and sometimes they are even appropriate, but chaining them has a mild code smell, and relying on events generated by them has a stronger code smell.

Upvotes: 0

Related Questions