Resolve duplicate declaration on puppet - module

I'm trying to call several times a defined instance of a puppet module to deploy multiple files from a given repository but I'm getting this error:
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Duplicate declaration: File[/bin/deploy_artifacts.rb] is already declared in file /etc/puppet/modules/deploy_artifacts/manifests/init.pp:11; cannot redeclare at /etc/puppet/modules/deploy_artifacts/manifests/init.pp:11 on node node.example.com
This is the init.pp manifest of the module:
define deploy_artifacts (
$repository)
{
notify{"La UUAA esta en el repositorio: $repository": }
file { "/bin/deploy_artifacts.rb":
ensure => present,
owner => root,
group => root,
mode => 700,
source => "puppet:///modules/deploy_artifacts/deploy_artifacts.rb";
}
exec {"Deployment":
require => File["/bin/deploy_artifacts.rb"],
command => "/usr/bin/time /bin/deploy_artifacts.rb $repository",
logoutput => true;
}
}
Now the node manifest:
node "node.example.com" {
deploy_artifacts {'test-ASO':
repository => 'test-ASO',
}
deploy_artifacts {'PRUEBA_ASO':
repository => 'PRUEBA_ASO',
}
}
I tried to rewrite the whole module to put into init.pp the common piece of code(file statement) and in another manifest the exec statement but when I call more than once the module deploy_artifacts it throws me the same duplicated error.
How can I rewrite the code to ensure that the file is in the client node before executing all the instances of the defined deploy_artifacts without duplications?
Is there another solution rather than declare a dedicated class only for the file? Thank you!

Try this:
The file:
class deploy_artifacts {
file { "/bin/deploy_artifacts.rb":
ensure => present,
owner => root,
group => root,
mode => 700,
source => "puppet:///modules/deploy_artifacts/deploy_artifacts.rb";
}
}
The type:
define deploy_artifacts::repository ($repository) {
include deploy_artifacts
exec {"Deployment":
command => "/usr/bin/time /bin/deploy_artifacts.rb $repository",
logoutput => true,
require => File["/bin/deploy_artifacts.rb"
}
}
The node definition:
node "node.example.com" {
deploy_artifacts::repository {'test-ASO':
repository => 'test-ASO',
}
deploy_artifacts::repository {'PRUEBA_ASO':
repository => 'PRUEBA_ASO',
}
}

Related

Puppet conditional only if file exists in a particular directory

I have the following file resource in my puppet manifest:
file{
'example.dat'
path => "/path/to/example.dat",
owner => devops,
mode => "0644",
source => "/path/to/example.txt"
}
I want to run the above snippet only when the .txt file is present in a particular directory. Otherwise, I do not want the snippet to run.
How do I go about doing that in puppet?
In general, you would create a custom fact that returns true when the directory in question satisfies your condition. For example:
Facter.add('contains_txt') do
setcode do
! Dir.glob("/path/to/dir/*.txt").empty?
end
end
Then you would write:
if $facts['contains_txt'] {
file { 'example.dat':
path => "/path/to/example.dat",
owner => devops,
mode => "0644",
source => "/path/to/example.txt",
}
}

Install a package from tarball using puppet

I'm trying to install ActiveMQ using puppet. this package comes in tar ball. how can I make sure each and every file is being pushed (recursively) from puppet and it makes sure the service is running. As it has its own executable in 'bin' dir.
I would ask is it essential to install activemq from a Tarball? It'd probably be easier to manage as a package, such as a yum or apt install.
Managing tarballs is always going to be more difficult, especially when updating versions, or dealing with issues like downloads failing.
I would recommend using an existing activemq module from the forge:
https://forge.puppet.com/modules?utf-8=%E2%9C%93&sort=latest_release&q=activemq
To give you a general idea of how it might look, here's some basic code that could work:
$activemq_home = "/usr/local/activemq"
package{"java-1.6.0-openjdk":
ensure => installed;
}
$activemq_version = "5.4.3"
user {"activemq":
ensure => present,
home => $activemq_home,
managehome => false,
shell => "/bin/sh",
}
group {"activemq":
ensure => present,
require => User["activemq"],
}
Exec{path => ["/usr/local/bin","/usr/bin","/bin"]}
$puppet_cache = "/usr/local/src/gitorious"
file {$puppet_cache:
ensure => directory,
owner => "root",
group => "root",
}
exec { 'download_amq_src':
unless => '/usr/bin/test -e ${activemq_home}/apache-activemq-${amq_version}-bin.tar.gz',
command => 'cd /tmp && /usr/bin/wget http://archive.apache.org/dist/activemq/apache-activemq/${amq_version}/apache-activemq-${amq_version}-bin.tar.gz',
require => File[$activemq_home],
}
# Unpack the archive in the amq user directory
exec { 'unpack_amq_src':
onlyif => '/usr/bin/test -d ${activemq_home}/apache-activemq-${amq_version}-bin',
command => 'cd $amq_home && /bin/tar -xf /tmp/apache-activemq-${amq_version}-bin.tar.gz',
require => Exec['download_amq'],
}
file {"/etc/init.d/activemq":
ensure => file,
mode => 755,
owner => "root",
group => "root",
content => template("activemq/etc/init.d/activemq.erb"),
require => File["/etc/activemq.conf"],
}
service{"activemq":
enable => true,
ensure => running,
require => File["/etc/init.d/activemq"],
}
file { "activemq.xml":
path => "$activemq_home/conf/activemq.xml",
ensure => present,
mode => 644,
owner => "activemq",
group => "activemq",
content => template("activemq/activemq.xml.erb"),
require => File["/etc/init.d/activemq"],
notify => Service["activemq"],
}

Puppet - Adding default nodes

I'm new to Puppet and following this tutorial to get into it:
http://www.pindi.us/blog/getting-started-puppet
I created an SSH module (/modules/ssh/manifests/init.pp) and added the following in the base node.pp (puppet/manifests/)
node default {
include ssh
}
The ssh module loks ike this:
class ssh {
include ssh::install, ssh::config, ssh::service
}
class ssh::install {
package {"ssh":
ensure => present,
}
}
class ssh::config {
file { "/etc/ssh/sshd_config":
ensure => present,
owner => 'root',
group => 'root',
mode => 600,
source => "puppet:///modules/ssh/sshd_config",
notify => Class["ssh::service"],
}
}
class ssh::service {
service { "ssh":
ensure => running,
hasstatus => true,
hasrestart => true,
enable => true,
}
}
Class["ssh::install"] -> Class["ssh::config"] -> Class["ssh::service"]
On the puppet I linked the module path with:
sudo puppet apply --modulepath=/vagrant/modules /vagrant/manifests/site.pp
which works.
If I then apply the nodes.pp I get the error:
Could not find class ssh for precise32 at /vagrant/manifests/nodes.pp:2 on node precise32...
Everything looks right, but I don't know where my error is.
It worked before as I installed SSH on the puppet yesterday, but I must have messed up something

How do I configure rabbitmq queue via puppet

I'm trying to install rabbitmq via puppet. I'm using the puppetlabs-rabbitmq module. It also has section to configure queues and exchanges, which are Native Types. I can't figure out how to use these native types.
My code for rabbitmq installation:
class rabbitmq-concrete{
$tools = ["vim-enhanced","mc"]
package { $tools: ensure => "installed" }
$interface = "enp0s8"
$address = inline_template("<%= scope.lookupvar('::ipaddress_${interface}') -%>")
class { 'rabbitmq':
config_cluster => true,
cluster_nodes => ['rml01', 'rml02'],
cluster_node_type => 'disc',
manage_repos => true,
node_ip_address => $address,
erlang_cookie => 'rmq_secret',
}
rabbitmq_exchange { "logging#${node_name}":
type => 'topic',
ensure => present,
}
rabbitmq_queue { "logging#${node_name}":
durable => true,
auto_delete => false,
arguments => {
x-message-ttl => 123,
x-dead-letter-exchange => 'other'
},
ensure => present,
}
rabbitmq_binding { "logging#logging#${node_name}":
destination_type => 'logging',
routing_key => '#',
arguments => {},
ensure => present,
}
}
include rabbitmq-concrete
I get following error:
==> rml01: Error: Puppet::Parser::AST::Resource failed with error ArgumentError: Invalid resource type rabbitmq_queue at /tmp/vagrant-puppet-2/manifests/site.pp:35 on node rml01
==> rml01: Wrapped exception:
==> rml01: Invalid resource type rabbitmq_queue
==> rml01: Error: Puppet::Parser::AST::Resource failed with error ArgumentError: Invalid resource type rabbitmq_queue at /tmp/vagrant-puppet-2/manifests/site.pp:35 on node rml01
Note: When I leave out these native types, rabbit installation works well.
How do I use Native Types to configure rabbitmq_queue, rabbitmq_exchange and rabbitmq_binding ?
Do you have the required prerequisites? You need the following packages from the Forge:
puppetlabs/stdlib
stahnma/epel
nanliu/staging
garethr/erlang
To your manifest I added:
include epel
include staging
class { 'erlang': epel_enable => true}
Your question is dated 13th Feb, yet looking on the Puppet Forge those features were only added to that module in the most recent release on 10th March in version 5.1.0.
Full changelog => https://forge.puppetlabs.com/puppetlabs/rabbitmq/changelog
Abridged:
"2015-03-10 - Version 5.1.0
Summary
This release adds several features for greater flexibility in configuration of rabbitmq, includes a number of bug fixes, and bumps the minimum required version of puppetlabs-stdlib to 3.0.0.
Features
Add rabbitmq_queue and rabbitmq_binding types"

How to order resources from a specific module?

I got some trouble ordering resources from a module.
class { 'postgres' :
charset => 'UTF8',
locale => 'fr_FR',
require => Service['postgresqld'],
}->
class { 'postgresql::server':
}
postgresql::role { 'role1' :
namevar => 'redmine',
password_hash => 'random_md5',
createdb => true,
require => Class['postgres'],
}
postgresql::database_user {'charly':
password => 'random',
role => 'redmine',
require => postgresql::role['role1'],
}
I want to order this, but it appears to have a syntax error on the last line at role.
I'm pretty sure it comes from the capitalized first letter. But Puppet doesn't want to run the manifest if I put a capital letter Postgresql::role['role1] or postgresql::Role['role1]. Without capital letter, I "just" get a warning :
warning: Deprecation notice: Resource references should now be capitalized on line 61 in file /home/charly/testManifests/part1.pp
I'm doing something wrong, but I don't know what. I searched for an answer on the Internet but can't find what I want neither in tutorials nor on the forums.
Try using chaining arrows to your resource group references e.g.
Class['postgres'] -> Class['postgresql::server']
class { 'postgres' :
charset => 'UTF8',
locale => 'fr_FR',
require => Service['postgresqld']
}
class { 'postgresql::server': }
More detail can be found here in the puppet reference Chaining Arrows