Re: Passing data in bundle

Mike Weilgart <[email protected]>
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
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] <mailto:[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 | 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/CC6459C6-A9FD-4C36-89C5-5E94C3E7B313%40verticalsysadmin.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.