Iterate over an array of hashes in puppet
up vote
0
down vote
favorite
I have hiera data in puppet as follows:
elasticsearch::cluster_name: 'elasticsearch-dev'
elasticsearch::masterlist: [ "elasticsearchdev01.domain.com", "elasticsearchdev02.domain.com", "elasticsearchdev03.domain.com" ]
elasticsearch::kibanalist: [ "kibanadev01.domain.com" ]
And my manifest contains this:
$masterlist = hiera('elasticsearch::masterlist')
$kibanalist = hiera('elasticsearch::kibanalist')
if ( $::fqdn in $masterlist ) or ( $::fqdn in $kibanalist ) {
$cluster_name = hiera('elasticsearch::cluster_name')
}
else {
notify { 'No cluster for node':
message => "${::fqdn} is not configured to be in any cluster in the hiera data",
}
}
I would like to modify this to allow multiple clusters to be configured for a single environment, and I'm not sure of the best way to implement this. I want to be able to group the master lists with their corresponding cluster names, and then call the relevant cluster name for each node.
I think perhaps I need to use an array of hashes and iterate over it, but I don't know how to cover this in my manifest.
arrays hash puppet
New contributor
clp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
I have hiera data in puppet as follows:
elasticsearch::cluster_name: 'elasticsearch-dev'
elasticsearch::masterlist: [ "elasticsearchdev01.domain.com", "elasticsearchdev02.domain.com", "elasticsearchdev03.domain.com" ]
elasticsearch::kibanalist: [ "kibanadev01.domain.com" ]
And my manifest contains this:
$masterlist = hiera('elasticsearch::masterlist')
$kibanalist = hiera('elasticsearch::kibanalist')
if ( $::fqdn in $masterlist ) or ( $::fqdn in $kibanalist ) {
$cluster_name = hiera('elasticsearch::cluster_name')
}
else {
notify { 'No cluster for node':
message => "${::fqdn} is not configured to be in any cluster in the hiera data",
}
}
I would like to modify this to allow multiple clusters to be configured for a single environment, and I'm not sure of the best way to implement this. I want to be able to group the master lists with their corresponding cluster names, and then call the relevant cluster name for each node.
I think perhaps I need to use an array of hashes and iterate over it, but I don't know how to cover this in my manifest.
arrays hash puppet
New contributor
clp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I wonder if the Puppet docs on iteration help at all? You may need a recent Puppet for this to work.
– larsks
2 days ago
I will point you at documentation since this is a theoretical question: puppet.com/docs/puppet/5.5/lang_iteration.html. If you make the question specific, then we can give you a specific answer. That documentation will be a push in the right direction for now though.
– Matt Schuchard
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have hiera data in puppet as follows:
elasticsearch::cluster_name: 'elasticsearch-dev'
elasticsearch::masterlist: [ "elasticsearchdev01.domain.com", "elasticsearchdev02.domain.com", "elasticsearchdev03.domain.com" ]
elasticsearch::kibanalist: [ "kibanadev01.domain.com" ]
And my manifest contains this:
$masterlist = hiera('elasticsearch::masterlist')
$kibanalist = hiera('elasticsearch::kibanalist')
if ( $::fqdn in $masterlist ) or ( $::fqdn in $kibanalist ) {
$cluster_name = hiera('elasticsearch::cluster_name')
}
else {
notify { 'No cluster for node':
message => "${::fqdn} is not configured to be in any cluster in the hiera data",
}
}
I would like to modify this to allow multiple clusters to be configured for a single environment, and I'm not sure of the best way to implement this. I want to be able to group the master lists with their corresponding cluster names, and then call the relevant cluster name for each node.
I think perhaps I need to use an array of hashes and iterate over it, but I don't know how to cover this in my manifest.
arrays hash puppet
New contributor
clp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have hiera data in puppet as follows:
elasticsearch::cluster_name: 'elasticsearch-dev'
elasticsearch::masterlist: [ "elasticsearchdev01.domain.com", "elasticsearchdev02.domain.com", "elasticsearchdev03.domain.com" ]
elasticsearch::kibanalist: [ "kibanadev01.domain.com" ]
And my manifest contains this:
$masterlist = hiera('elasticsearch::masterlist')
$kibanalist = hiera('elasticsearch::kibanalist')
if ( $::fqdn in $masterlist ) or ( $::fqdn in $kibanalist ) {
$cluster_name = hiera('elasticsearch::cluster_name')
}
else {
notify { 'No cluster for node':
message => "${::fqdn} is not configured to be in any cluster in the hiera data",
}
}
I would like to modify this to allow multiple clusters to be configured for a single environment, and I'm not sure of the best way to implement this. I want to be able to group the master lists with their corresponding cluster names, and then call the relevant cluster name for each node.
I think perhaps I need to use an array of hashes and iterate over it, but I don't know how to cover this in my manifest.
arrays hash puppet
arrays hash puppet
New contributor
clp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
clp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
clp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 days ago
clp
1
1
New contributor
clp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
clp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
clp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I wonder if the Puppet docs on iteration help at all? You may need a recent Puppet for this to work.
– larsks
2 days ago
I will point you at documentation since this is a theoretical question: puppet.com/docs/puppet/5.5/lang_iteration.html. If you make the question specific, then we can give you a specific answer. That documentation will be a push in the right direction for now though.
– Matt Schuchard
2 days ago
add a comment |
I wonder if the Puppet docs on iteration help at all? You may need a recent Puppet for this to work.
– larsks
2 days ago
I will point you at documentation since this is a theoretical question: puppet.com/docs/puppet/5.5/lang_iteration.html. If you make the question specific, then we can give you a specific answer. That documentation will be a push in the right direction for now though.
– Matt Schuchard
2 days ago
I wonder if the Puppet docs on iteration help at all? You may need a recent Puppet for this to work.
– larsks
2 days ago
I wonder if the Puppet docs on iteration help at all? You may need a recent Puppet for this to work.
– larsks
2 days ago
I will point you at documentation since this is a theoretical question: puppet.com/docs/puppet/5.5/lang_iteration.html. If you make the question specific, then we can give you a specific answer. That documentation will be a push in the right direction for now though.
– Matt Schuchard
2 days ago
I will point you at documentation since this is a theoretical question: puppet.com/docs/puppet/5.5/lang_iteration.html. If you make the question specific, then we can give you a specific answer. That documentation will be a push in the right direction for now though.
– Matt Schuchard
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You would probably want to restructure your data as a Hash of Hashes:
elasticsearch::clusters:
'elasticsearch-dev':
masterlist: [ "elasticsearchdev01.domain.com", "elasticsearchdev02.domain.com", "elasticsearchdev03.domain.com" ]
kibanalist: [ "kibanadev01.domain.com" ]
And then have in your manifests:
$clusters = hiera('elasticsearch::clusters')
$mycluster = $clusters.filter |$cluster, $data| {
($::fqdn in $data['masterlist']) or ($::fqdn in $data['kibanalist'])
}
if ($mycluster.empty) {
notify { 'No cluster for node':
message => "${::fqdn} is not configured to be in any cluster in the hiera data",
}
}
$cluster_name = $mycluster.keys[0]
notice($cluster_name)
You might want to also consider replacing the deprecated hiera()
call with lookup()
and using $facts['networking']['fqdn']
instead of the legacy $::fqdn
.
This gave me a great place to start. After implementing this I think that what I actually want to do is write a function that will search all the master/kibana lists in the hiera data (given multiple clusters configured) for the fqdn of the node. If it is not there it will send a notify, and if it is then it will return the corresponding cluster name, masterlist and kibanalist. I can see that I can search the lists from what you have suggested above, but I then don't seem to be able to use any variables set outside of the iteration.
– clp
yesterday
Oh, I see. I was thinking you just didn't know how to structure the data. It actually can be done. Let me refactor.
– Alex Harvey
yesterday
@clp, I think this is what you meant?
– Alex Harvey
12 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You would probably want to restructure your data as a Hash of Hashes:
elasticsearch::clusters:
'elasticsearch-dev':
masterlist: [ "elasticsearchdev01.domain.com", "elasticsearchdev02.domain.com", "elasticsearchdev03.domain.com" ]
kibanalist: [ "kibanadev01.domain.com" ]
And then have in your manifests:
$clusters = hiera('elasticsearch::clusters')
$mycluster = $clusters.filter |$cluster, $data| {
($::fqdn in $data['masterlist']) or ($::fqdn in $data['kibanalist'])
}
if ($mycluster.empty) {
notify { 'No cluster for node':
message => "${::fqdn} is not configured to be in any cluster in the hiera data",
}
}
$cluster_name = $mycluster.keys[0]
notice($cluster_name)
You might want to also consider replacing the deprecated hiera()
call with lookup()
and using $facts['networking']['fqdn']
instead of the legacy $::fqdn
.
This gave me a great place to start. After implementing this I think that what I actually want to do is write a function that will search all the master/kibana lists in the hiera data (given multiple clusters configured) for the fqdn of the node. If it is not there it will send a notify, and if it is then it will return the corresponding cluster name, masterlist and kibanalist. I can see that I can search the lists from what you have suggested above, but I then don't seem to be able to use any variables set outside of the iteration.
– clp
yesterday
Oh, I see. I was thinking you just didn't know how to structure the data. It actually can be done. Let me refactor.
– Alex Harvey
yesterday
@clp, I think this is what you meant?
– Alex Harvey
12 hours ago
add a comment |
up vote
0
down vote
You would probably want to restructure your data as a Hash of Hashes:
elasticsearch::clusters:
'elasticsearch-dev':
masterlist: [ "elasticsearchdev01.domain.com", "elasticsearchdev02.domain.com", "elasticsearchdev03.domain.com" ]
kibanalist: [ "kibanadev01.domain.com" ]
And then have in your manifests:
$clusters = hiera('elasticsearch::clusters')
$mycluster = $clusters.filter |$cluster, $data| {
($::fqdn in $data['masterlist']) or ($::fqdn in $data['kibanalist'])
}
if ($mycluster.empty) {
notify { 'No cluster for node':
message => "${::fqdn} is not configured to be in any cluster in the hiera data",
}
}
$cluster_name = $mycluster.keys[0]
notice($cluster_name)
You might want to also consider replacing the deprecated hiera()
call with lookup()
and using $facts['networking']['fqdn']
instead of the legacy $::fqdn
.
This gave me a great place to start. After implementing this I think that what I actually want to do is write a function that will search all the master/kibana lists in the hiera data (given multiple clusters configured) for the fqdn of the node. If it is not there it will send a notify, and if it is then it will return the corresponding cluster name, masterlist and kibanalist. I can see that I can search the lists from what you have suggested above, but I then don't seem to be able to use any variables set outside of the iteration.
– clp
yesterday
Oh, I see. I was thinking you just didn't know how to structure the data. It actually can be done. Let me refactor.
– Alex Harvey
yesterday
@clp, I think this is what you meant?
– Alex Harvey
12 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
You would probably want to restructure your data as a Hash of Hashes:
elasticsearch::clusters:
'elasticsearch-dev':
masterlist: [ "elasticsearchdev01.domain.com", "elasticsearchdev02.domain.com", "elasticsearchdev03.domain.com" ]
kibanalist: [ "kibanadev01.domain.com" ]
And then have in your manifests:
$clusters = hiera('elasticsearch::clusters')
$mycluster = $clusters.filter |$cluster, $data| {
($::fqdn in $data['masterlist']) or ($::fqdn in $data['kibanalist'])
}
if ($mycluster.empty) {
notify { 'No cluster for node':
message => "${::fqdn} is not configured to be in any cluster in the hiera data",
}
}
$cluster_name = $mycluster.keys[0]
notice($cluster_name)
You might want to also consider replacing the deprecated hiera()
call with lookup()
and using $facts['networking']['fqdn']
instead of the legacy $::fqdn
.
You would probably want to restructure your data as a Hash of Hashes:
elasticsearch::clusters:
'elasticsearch-dev':
masterlist: [ "elasticsearchdev01.domain.com", "elasticsearchdev02.domain.com", "elasticsearchdev03.domain.com" ]
kibanalist: [ "kibanadev01.domain.com" ]
And then have in your manifests:
$clusters = hiera('elasticsearch::clusters')
$mycluster = $clusters.filter |$cluster, $data| {
($::fqdn in $data['masterlist']) or ($::fqdn in $data['kibanalist'])
}
if ($mycluster.empty) {
notify { 'No cluster for node':
message => "${::fqdn} is not configured to be in any cluster in the hiera data",
}
}
$cluster_name = $mycluster.keys[0]
notice($cluster_name)
You might want to also consider replacing the deprecated hiera()
call with lookup()
and using $facts['networking']['fqdn']
instead of the legacy $::fqdn
.
edited 5 hours ago
answered 2 days ago


Alex Harvey
3,5481823
3,5481823
This gave me a great place to start. After implementing this I think that what I actually want to do is write a function that will search all the master/kibana lists in the hiera data (given multiple clusters configured) for the fqdn of the node. If it is not there it will send a notify, and if it is then it will return the corresponding cluster name, masterlist and kibanalist. I can see that I can search the lists from what you have suggested above, but I then don't seem to be able to use any variables set outside of the iteration.
– clp
yesterday
Oh, I see. I was thinking you just didn't know how to structure the data. It actually can be done. Let me refactor.
– Alex Harvey
yesterday
@clp, I think this is what you meant?
– Alex Harvey
12 hours ago
add a comment |
This gave me a great place to start. After implementing this I think that what I actually want to do is write a function that will search all the master/kibana lists in the hiera data (given multiple clusters configured) for the fqdn of the node. If it is not there it will send a notify, and if it is then it will return the corresponding cluster name, masterlist and kibanalist. I can see that I can search the lists from what you have suggested above, but I then don't seem to be able to use any variables set outside of the iteration.
– clp
yesterday
Oh, I see. I was thinking you just didn't know how to structure the data. It actually can be done. Let me refactor.
– Alex Harvey
yesterday
@clp, I think this is what you meant?
– Alex Harvey
12 hours ago
This gave me a great place to start. After implementing this I think that what I actually want to do is write a function that will search all the master/kibana lists in the hiera data (given multiple clusters configured) for the fqdn of the node. If it is not there it will send a notify, and if it is then it will return the corresponding cluster name, masterlist and kibanalist. I can see that I can search the lists from what you have suggested above, but I then don't seem to be able to use any variables set outside of the iteration.
– clp
yesterday
This gave me a great place to start. After implementing this I think that what I actually want to do is write a function that will search all the master/kibana lists in the hiera data (given multiple clusters configured) for the fqdn of the node. If it is not there it will send a notify, and if it is then it will return the corresponding cluster name, masterlist and kibanalist. I can see that I can search the lists from what you have suggested above, but I then don't seem to be able to use any variables set outside of the iteration.
– clp
yesterday
Oh, I see. I was thinking you just didn't know how to structure the data. It actually can be done. Let me refactor.
– Alex Harvey
yesterday
Oh, I see. I was thinking you just didn't know how to structure the data. It actually can be done. Let me refactor.
– Alex Harvey
yesterday
@clp, I think this is what you meant?
– Alex Harvey
12 hours ago
@clp, I think this is what you meant?
– Alex Harvey
12 hours ago
add a comment |
clp is a new contributor. Be nice, and check out our Code of Conduct.
clp is a new contributor. Be nice, and check out our Code of Conduct.
clp is a new contributor. Be nice, and check out our Code of Conduct.
clp is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373788%2fiterate-over-an-array-of-hashes-in-puppet%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I wonder if the Puppet docs on iteration help at all? You may need a recent Puppet for this to work.
– larsks
2 days ago
I will point you at documentation since this is a theoretical question: puppet.com/docs/puppet/5.5/lang_iteration.html. If you make the question specific, then we can give you a specific answer. That documentation will be a push in the right direction for now though.
– Matt Schuchard
2 days ago