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.










share|improve this question







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















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.










share|improve this question







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













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.










share|improve this question







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






share|improve this question







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.











share|improve this question







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.









share|improve this question




share|improve this question






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


















  • 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












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.






share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






clp is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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

























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.






share|improve this answer























  • 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















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.






share|improve this answer























  • 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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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










clp is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith