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
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
I wonder if the Puppet docs on iteration help at all? You may need a recent Puppet for this to work.
– larsks
Nov 19 at 12:17
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
Nov 19 at 13:59
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
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
asked Nov 19 at 11:35
clp
1
1
I wonder if the Puppet docs on iteration help at all? You may need a recent Puppet for this to work.
– larsks
Nov 19 at 12:17
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
Nov 19 at 13:59
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
Nov 19 at 12:17
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
Nov 19 at 13:59
I wonder if the Puppet docs on iteration help at all? You may need a recent Puppet for this to work.
– larsks
Nov 19 at 12:17
I wonder if the Puppet docs on iteration help at all? You may need a recent Puppet for this to work.
– larsks
Nov 19 at 12:17
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
Nov 19 at 13:59
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
Nov 19 at 13:59
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
Nov 21 at 8:51
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
Nov 21 at 11:20
@clp, I think this is what you meant?
– Alex Harvey
Nov 21 at 22:55
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
Nov 21 at 8:51
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
Nov 21 at 11:20
@clp, I think this is what you meant?
– Alex Harvey
Nov 21 at 22:55
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
Nov 21 at 8:51
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
Nov 21 at 11:20
@clp, I think this is what you meant?
– Alex Harvey
Nov 21 at 22:55
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 Nov 22 at 5:54
answered Nov 19 at 14:00
Alex Harvey
3,5801823
3,5801823
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
Nov 21 at 8:51
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
Nov 21 at 11:20
@clp, I think this is what you meant?
– Alex Harvey
Nov 21 at 22:55
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
Nov 21 at 8:51
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
Nov 21 at 11:20
@clp, I think this is what you meant?
– Alex Harvey
Nov 21 at 22:55
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
Nov 21 at 8:51
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
Nov 21 at 8:51
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
Nov 21 at 11:20
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
Nov 21 at 11:20
@clp, I think this is what you meant?
– Alex Harvey
Nov 21 at 22:55
@clp, I think this is what you meant?
– Alex Harvey
Nov 21 at 22:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Nov 19 at 12:17
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
Nov 19 at 13:59