Re: Passing data in bundle

thierry thunot <[email protected]>
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
Hello Nick and Mike,
I apologize for any silly mistakes I may have made. I was focused on how 
loops work and I didn't think about checking for errors in the Json!!! 
shame on me :) I thank you again for your speed in giving me the solution. 
I pass on my bride who could be of use to others. I added in tests the 
creation of files relating to the repository. Note that my production bride 
is not the one attached because it is a bit more complex. thanks again!!!!

bundle agent __main__ 
{ 
       vars: 

       "parametrage_des_sources" data => '{ 
                   "mogodb" : { 
                       "nom_de_la_liste"     : "mogodb.list", 
                       "nom_clef_brute"     : "GPG-KEY-mongodb.gpg.key", 
                       "url_repo"           : "
http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main" 
                   }, 
                   "elasticsearch" : { 
                       "nom_de_la_liste"     : "elasticsearch.list", 
                       "nom_clef_brute"     : 
"GPG-KEY-elasticsearch.gpg.key", 
                       "url_repo"           : "
https://artifacts.elastic.co/packages/oss-7.x/apt stable main" 
                   }, 

                   "graylog" : { 
                       "nom_de_la_liste"     : "graylog.list", 
                       "nom_clef_brute"     : "graylog-keyring.gpg.key", 
                       "url_repo"           : "
https://packages.graylog2.org/repo/debian/ stable 4.2" 
                   } 
                     }' 

               ; 
       "noms_depots" slist => getindices("parametrage_des_sources"); 

       methods: 
               "" usebundle => donnees(storejson(parametrage_des_sources)); 
            
}

bundle agent donnees(valeurs) 
{  
 vars: 
   "donnees_extraites" data => parsejson($(valeurs)); 
   "nom" slist => getindices("$(valeurs)"); 
  
 files: 
 "/root/$(donnees_extraites[$(nom)][nom_de_la_liste])" 
       create => "true", 
       edit_line => append_if_no_line("$(donnees_extraites[$(nom)][url_repo]
)"), 
       edit_defaults => empty; 
  
 reports: 
 "Nom des datas $(nom)"; 
 "Nom_de_la_liste pour l'entre '$(nom)' est  : $(donnees_extraites[$(nom)
][nom_clef_brute])  
                       l'adresse est $(donnees_extraites[$(nom)][url_repo]) 
 
                       le fichier est $(donnees_extraites[$(nom)
][nom_de_la_liste])"; 
}

bundle edit_line append_if_no_line(lines) 
# @brief Alias for `lines_present` 
# @param lines List or string that should be present in the file 
{ 
 insert_lines: 

     "$(lines)" 
       comment => "Append lines if they don't exist"; 
} 


body edit_defaults empty 
# @brief Empty the file before editing 
# 
# No backup is made 
{ 
     empty_file_before_editing => "true"; 
     edit_backup => "false"; 
     #max_file_size => "300000"; 
} 

root@cfeginetest:/var/cfengine/masterfiles# cf-agent -Kf ./
essais_bundle_parametrique.cf  
R: Nom des datas elasticsearch 
R: Nom des datas graylog 
R: Nom des datas mogodb 
R: Nom_de_la_liste pour l'entre 'elasticsearch' est  : 
GPG-KEY-elasticsearch.gpg.key  
                       l'adresse est 
https://artifacts.elastic.co/packages/oss-7.x/apt stable main  
                       le fichier est elasticsearch.list 
R: Nom_de_la_liste pour l'entre 'graylog' est  : graylog-keyring.gpg.key  
                       l'adresse est 
https://packages.graylog2.org/repo/debian/ stable 4.2  
                       le fichier est graylog.list 
R: Nom_de_la_liste pour l'entre 'mogodb' est  : GPG-KEY-mongodb.gpg.key  
                       l'adresse est http://repo.mongodb.org/apt/debian 
buster/mongodb-org/4.2 main  
                       le fichier est mogodb.list

 and 
root@cfeginetest:/var/cfengine/masterfiles# ls /root/*.list 
/root/elasticsearch.list  /root/graylog.list  /root/mogodb.list

then : 
cat /root/*.list 
https://artifacts.elastic.co/packages/oss-7.x/apt stable main 
https://packages.graylog2.org/repo/debian/ stable 4.2 
http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main



Le jeudi 16 juin 2022 à 01:51:01 UTC+2, Nick Anderson a écrit :

> Hey Mike!
>
> Indeed it is. I completely missed it. Good eye.
>
> On Wed, Jun 15, 2022, 6:27 PM Mike Weilgart <
> [email protected]> wrote:
>
>> Hey Nick,
>>
>> I second your conclusion, but I'll point out that it looks like the 
>> "elasticsearch" line *is* shown in the original post, it's just merged into 
>> the command itself, which I suspect is a copy-paste error.
>>
>> Best,
>> —Mike Weilgart
>>
>> On Jun 15, 2022, at 11:34 AM, 'Nick Anderson' via help-cfengine <
>> [email protected]> wrote:
>>
>> thierry thunot <[email protected]> writes: 
>>
>> Hi all i need help about this promise. I have been looking for many hours 
>>> why my promise does not work. I want in my report that all nom_clef_brute 
>>> values ​​are displayed in the report for each entry of the variable 
>>> parametrage_des_sources. only one is display… 
>>>
>>
>> Hi Thierry, 
>>
>> The first thing that I noticed in this policy is a bit of invalid JSON. 
>>
>> The last entry in graylog has a trailing comma which isn't valid JSON. 
>> Listing 1: /tmp/parametrage_des_sources.json
>>
>> {
>>     "mogodb" : {
>>         "nom_de_la_liste"     : "mogodb.list",
>>         "nom_clef_brute"     : "GPG-KEY-mongodb.gpg.key",
>>         "url_repo"           : "http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main"
>>     },
>>     "elasticsearch" : {
>>         "nom_de_la_liste"     : "elasticsearch.list",
>>         "nom_de_la_clef"     : "GPG-KEY-elasticsearch.gpg.key",
>>         "url_repo"           : "https://artifacts.elastic.co/packages/oss-7.x/apt stable main"
>>     },
>>
>>     "graylog" : {
>>         "nom_de_la_liste"     : "graylog.list",
>>         "nom_de_la_clef"     : "graylog-keyring.gpg.key",
>>         "url_repo"           : "https://packages.graylog2.org/repo/debian/ stable 4.2",
>>     }}
>>
>> Listing 2: Checking json syntax with jq and python
>>
>> exec 2>&1
>> jq < /tmp/parametrage_des_sources.json  
>> python3 -m json.tool < /tmp/parametrage_des_sources.json  
>> :
>>
>> parse error: Expected another key-value pair at line 17, column 5
>> Expecting property name enclosed in double quotes: line 17 column 5 (char 688)
>>
>> Though CFEngine handles it without complaint. 
>> Listing 3: Example illustrating how CFEngine deals with trailing commas 
>> in JSON without complaint
>>
>> bundle agent __main__{
>>   vars:
>>       "file" string => "/tmp/parametrage_des_sources.json";
>>
>>       "data_from_external_file" data => readjson( $(file)  );
>>
>>       "data_from_inline" data => '{    "mogodb" : {        "nom_de_la_liste"     : "mogodb.list",        "nom_clef_brute"     : "GPG-KEY-mongodb.gpg.key",        "url_repo"           : "http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main"    },    "elasticsearch" : {        "nom_de_la_liste"     : "elasticsearch.list",        "nom_de_la_clef"     : "GPG-KEY-elasticsearch.gpg.key",        "url_repo"           : "https://artifacts.elastic.co/packages/oss-7.x/apt stable main"    },
>>     "graylog" : {        "nom_de_la_liste"     : "graylog.list",        "nom_de_la_clef"     : "graylog-keyring.gpg.key",        "url_repo"           : "https://packages.graylog2.org/repo/debian/ stable 4.2",    }      }';
>>
>>   reports:
>>       "$(file)"
>>         printfile => cat( $(file) );
>>
>>       "CFEngine dealt with data from external file:$(const.n)$(with)" with => storejson( data_from_external_file );
>>
>>       "CFEngine dealt with data from inline:$(const.n)$(with)" with => storejson( data_from_inline );}
>>
>> R: /tmp/parametrage_des_sources.json
>> R: {
>> R:     "mogodb" : {
>> R:         "nom_de_la_liste"     : "mogodb.list",
>> R:         "nom_clef_brute"     : "GPG-KEY-mongodb.gpg.key",
>> R:         "url_repo"           : "http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main"
>> R:     },
>> R:     "elasticsearch" : {
>> R:         "nom_de_la_liste"     : "elasticsearch.list",
>> R:         "nom_de_la_clef"     : "GPG-KEY-elasticsearch.gpg.key",
>> R:         "url_repo"           : "https://artifacts.elastic.co/packages/oss-7.x/apt stable main"
>> R:     },
>> R: 
>> R:     "graylog" : {
>> R:         "nom_de_la_liste"     : "graylog.list",
>> R:         "nom_de_la_clef"     : "graylog-keyring.gpg.key",
>> R:         "url_repo"           : "https://packages.graylog2.org/repo/debian/ stable 4.2",
>> R:     }
>> R: }
>> R: CFEngine dealt with data from external file:
>> {
>>   "elasticsearch": {
>>     "nom_de_la_clef": "GPG-KEY-elasticsearch.gpg.key",
>>     "nom_de_la_liste": "elasticsearch.list",
>>     "url_repo": "https://artifacts.elastic.co/packages/oss-7.x/apt stable main"
>>   },
>>   "graylog": {
>>     "nom_de_la_clef": "graylog-keyring.gpg.key",
>>     "nom_de_la_liste": "graylog.list",
>>     "url_repo": "https://packages.graylog2.org/repo/debian/ stable 4.2"
>>   },
>>   "mogodb": {
>>     "nom_clef_brute": "GPG-KEY-mongodb.gpg.key",
>>     "nom_de_la_liste": "mogodb.list",
>>     "url_repo": "http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main"
>>   }
>> }
>> R: CFEngine dealt with data from inline:
>> {
>>   "elasticsearch": {
>>     "nom_de_la_clef": "GPG-KEY-elasticsearch.gpg.key",
>>     "nom_de_la_liste": "elasticsearch.list",
>>     "url_repo": "https://artifacts.elastic.co/packages/oss-7.x/apt stable main"
>>   },
>>   "graylog": {
>>     "nom_de_la_clef": "graylog-keyring.gpg.key",
>>     "nom_de_la_liste": "graylog.list",
>>     "url_repo": "https://packages.graylog2.org/repo/debian/ stable 4.2"
>>   },
>>   "mogodb": {
>>     "nom_clef_brute": "GPG-KEY-mongodb.gpg.key",
>>     "nom_de_la_liste": "mogodb.list",
>>     "url_repo": "http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main"
>>   }
>> }
>>
>> The second thing I notice is that nom_clef_brute is only present in 
>> mogodb, hence why that is the only GPG-KEY entry emitted. Nothing jumps 
>> out at me with respect to why elasticsearch is not emitted. 
>>
>> Back to your example … 
>>
>> I only altered some whitespace and yanked the trailing comma in the JSON 
>> and added a report for the version of CFEngine that I am running. 
>>
>> I get the output for each key, elasticsearch, graylog, and mongodb. as 
>> expected. What do you get if you run this policy? 
>> Listing 4: Original policy with fixed, reflowed, with minor whitespace 
>> changes for my own readability
>>
>> bundle agent __main__{
>>   vars:
>>
>>       "parametrage_des_sources"
>>         data => '{  "mogodb": {    "nom_de_la_liste": "mogodb.list",    "nom_clef_brute": "GPG-KEY-mongodb.gpg.key",    "url_repo": "http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main"  },  "elasticsearch": {    "nom_de_la_liste": "elasticsearch.list",    "nom_de_la_clef": "GPG-KEY-elasticsearch.gpg.key",    "url_repo": "https://artifacts.elastic.co/packages/oss-7.x/apt stable main"  },  "graylog": {    "nom_de_la_liste": "graylog.list",    "nom_de_la_clef": "graylog-keyring.gpg.key",    "url_repo": "https://packages.graylog2.org/repo/debian/ stable 4.2"  }}';
>>
>>   methods:
>>       "" usebundle => donnees( storejson(parametrage_des_sources) );
>>       #"" usebundle => donnees($(parametrage_des_sources));
>>
>>   reports:
>>       "CFEngine $(sys.cf_version)";}
>> bundle agent donnees(valeurs){
>>   vars:
>>       "donnees_extraites" data => parsejson( $(valeurs) );
>>       "nom" slist => getindices( $(valeurs) );
>>       #"decompo" data => readjson ($(valeurs));
>>
>>   reports:
>>       "Nom des datas $(nom)";
>>       "Nom_de_la_liste pour l'entre '$(nom)' est  $(donnees_extraites[$(nom)][nom_clef_brute])";}
>>
>> R: Nom des datas elasticsearch
>> R: Nom des datas graylog
>> R: Nom des datas mogodb
>> R: Nom_de_la_liste pour l'entre 'mogodb' est  GPG-KEY-mongodb.gpg.key
>> R: CFEngine 3.20.0a.8a11a6ae4
>>
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "help-cfengine" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/help-cfengine/878rpxzs80.fsf%40northern.tech 
>> <https://groups.google.com/d/msgid/help-cfengine/878rpxzs80.fsf%40northern.tech?utm_medium=email&utm_source=footer>
>> .
>> -- 
>> Nick Anderson | Doer of Things | (+1) 785-550-1767 <(785)%20550-1767> | 
>> https://northern.tech
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "help-cfengine" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/help-cfengine/878rpxzs80.fsf%40northern.tech
>> .
>>
>>

-- 
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/baba6f07-23e9-4ff4-9084-4b52de068b6bn%40googlegroups.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.