xaviborja
xaviborja

Reputation: 41

Puppet: Exec from class when Exec from another class is successful

I want to call an Exec only when another Exec from a different class is executed successfully.

class mysql {
    exec { 'load-sql':
        command => 'mysql -uadmi -pxxx general < /vagrant/sites/ddbb/general.sql',
        path    => ['/bin', '/usr/bin'],
        timeout => 0,
        onlyif   => "test -f /vagrant/sites/ddbb/general.sql",
    }
    exec { 'delete-general-sql':
        command => 'sudo rm /vagrant/sites/ddbb/general.sql',
        path    => ['/bin', '/usr/bin'],
        onlyif   => "test -f /vagrant/sites/ddbb/general.sql",
        require => Exec['load-sql'],
    }
}

class sphinx {
    exec { 'sphinx-create-all-index':
        command => 'sudo indexer -c /etc/sphinxsearch/sphinx.conf --all --rotate',
        require => Exec['load-sql'],
        path => '/usr/bin/';
    }
}

The command 'delete-general-sql' is executed only if 'load-sql' is executed successfully but 'sphinx-create-all-index'ignores the result of 'load-sql'...

Thanks in advance!

Upvotes: 1

Views: 2075

Answers (2)

Felix Frank
Felix Frank

Reputation: 8223

To make the dependent exec run only once the previous one did run, you can use subscribe and refreshonly.

exec { 'sphinx-create-all-index':
    command => 'sudo indexer -c /etc/sphinxsearch/sphinx.conf --all --rotate',
    subscribe => Exec['load-sql'],
    refreshonly => true,
    path => '/usr/bin/';
}

This has some caveats - you may have a hard time to get Puppet to execute this task again if something goes wrong the first time around.

Upvotes: 0

kkamil
kkamil

Reputation: 2595

You mess up with require and onlyif. Read about puppet ordering.

require

Causes a resource to be applied after the target resource.

so

require => Exec['load-sql'],

means, execute resource after execution of exec{'load-sql':} resource.

On the other hand onlyif in exec means:

If this parameter is set, then this exec will only run if the command has an exit code of 0.

So you must add onlyif with proper test (probably onlyif => "test -f /vagrant/sites/ddbb/general.sql) to 'sphinx-create-all-index'.

Upvotes: 1

Related Questions