I have a database table where I need to insert a row for each host has been assigned a particular puppet model. Lets call this puppet model foo
. The db table is called foo_hosts
has three columns: id
, hostname
, and date_added
. I would like to some how have my foo
module execute SQL ...
insert into foo_hosts (hostname) values ( $hostname );
... if there is not already a row in the table for the host.
You will need a way to access your database from the command line. You do not specify which database engine you're using, so I will presume PostgreSQL, as this is what I am most familiar with. I know SQLite and MySQL both have a command line client which allows execution of queries.
First, I would start with this in a module:
And the
update-database
script could be:Note that this SQL query is vulnerable: if an attacker gains control of Puppet, he can execute any arbitrary command against your database server. Of course, if the attacker gains control of Puppet, you have even more serious problems.