Iterate over an array of hashes in puppet












0















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























  • I wonder if the Puppet docs on iteration help at all? You may need a recent Puppet for this to work.

    – larsks
    Nov 19 '18 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 '18 at 13:59
















0















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























  • I wonder if the Puppet docs on iteration help at all? You may need a recent Puppet for this to work.

    – larsks
    Nov 19 '18 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 '18 at 13:59














0












0








0








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














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













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 11:35









clpclp

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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 13:59












1 Answer
1






active

oldest

votes


















0














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
    Nov 21 '18 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 '18 at 11:20











  • @clp, I think this is what you meant?

    – Alex Harvey
    Nov 21 '18 at 22:55











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',
autoActivateHeartbeat: false,
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
});


}
});














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









0














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
    Nov 21 '18 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 '18 at 11:20











  • @clp, I think this is what you meant?

    – Alex Harvey
    Nov 21 '18 at 22:55
















0














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
    Nov 21 '18 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 '18 at 11:20











  • @clp, I think this is what you meant?

    – Alex Harvey
    Nov 21 '18 at 22:55














0












0








0







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 Nov 22 '18 at 5:54

























answered Nov 19 '18 at 14:00









Alex HarveyAlex Harvey

4,3691923




4,3691923













  • 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 '18 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 '18 at 11:20











  • @clp, I think this is what you meant?

    – Alex Harvey
    Nov 21 '18 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 '18 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 '18 at 11:20











  • @clp, I think this is what you meant?

    – Alex Harvey
    Nov 21 '18 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 '18 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 '18 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 '18 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 '18 at 11:20













@clp, I think this is what you meant?

– Alex Harvey
Nov 21 '18 at 22:55





@clp, I think this is what you meant?

– Alex Harvey
Nov 21 '18 at 22:55




















draft saved

draft discarded




















































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.




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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()