Reputation: 123
I have the following piece of manifest on Puppet Master :
exec { 'DatabaseCreation':
command => '/usr/bin/mysqladmin -u root --password="system" create gitHandson'
}
When I ran puppet agent --test
on Puppet Agent, it gave the following error :
Notice: /Stage[main]/Deployment/Exec[DatabaseCreation]/returns: /usr/bin/mysqladmin: CREATE DATABASE failed; error: 'Can't create database 'gitHandson'; database exists'
Error: /usr/bin/mysqladmin -u root --password="system" create gitHandson returned 1 instead of one of [0]
Instead of giving error, it should ignore the error. For this purpose, we have 'ignore_failure' in Chef and 'ignore_errors' in Ansible. What is its Puppet equivalent?
Upvotes: 4
Views: 5266
Reputation: 497
This is a classical example of an exec
resource that is not idempotent. The good news is: there are tools that can automatically detect these issues in your script. Check this out: https://github.com/citac/citac
Citac systematically executes your Puppet manifest in various configurations, different resource execution orders, and more. The generated test reports inform you about issues with non-idempotent resources, convergence-related issues, etc.
The tool uses Docker containers for execution, hence your system remains untouched while testing.
There is also an evaluation page with lots of similar bugs identified in public Puppet scripts: http://citac.github.io/eval
Happy Puppet testing!
Upvotes: 1
Reputation: 5190
Short answer: no. Puppet resources have to succeed, there's no way to ignore errors.
Easiest (and hackyiest) solution is to just add a && true
to the end of your command, so it will return 0 and not fail.
However, the problem with the exec is that it is not idempotent. Puppet is about describing state and making sure things only have to run once.
So for your example, it would probably be better to extend the exec to add an unless
or onlyif
parameter, so the command only runs if the database doesn't already exist.
exec { 'DatabaseCreation':
command => '/usr/bin/mysqladmin -u root --password="system" create gitHandson',
unless => 'Some command that exits 0 if the gitHandson database exists'
}
More details here.
Even better, there is a Puppetlabs MySQL module, which allows you to install mysql, set a root password and create MySQL databases.
Example:
mysql::db { 'mydb':
user => 'myuser',
password => 'mypass',
host => 'localhost',
grant => ['SELECT', 'UPDATE'],
}
Upvotes: 4