Skip to content

Instantly share code, notes, and snippets.

@nickanderson
nickanderson / search-and-replace-for-Xresources.org
Last active July 9, 2025 04:22
Search and replace for Xresources with CFEngine 3

CFEngine manage Xscreensaver mode

This example implements search and replace in a file (specifically an Xresources formatted file).

bundle agent __main__
{
  methods:
      "Test data"
        usebundle => init_test_file( $(example.file) );
@nickanderson
nickanderson / getuid-example-avoid-errors.org
Created June 4, 2025 13:39
Get the UID of a user, but only if the user exists as a local user on the system.

Using getuid() to get a users uid avoiding error from call where user does not exist

A question from Wolfgang in #CFEngine:matrix.org

I’m confused. I have a bundle that should only run on one specific host. What confuses me is that CFE tries to evaluate the variables. In that bundle, I have something like “uid” int => getuid(“my-user”), but of course this user does not exist on the other systems. However, cfe shows an error saying that it could not get the uid for that user.

CFEngine is eager to resolve variables (classes in common bundles too), for more read about pre-evaluation.

@nickanderson
nickanderson / example.org
Last active May 16, 2025 19:55
How can you use strings that look like CFEngine variables in CFEngine promises?

You can use $(const.dollar) if you are looking to render a literal $( ) into a file, alternatively you can use backticks.

bundle agent example_subshell_cfengine_files_promise
{
  files:
      "/tmp/using-const.dollar.txt"
        content => "#!/bin/env bash $(const.n)echo $(const.dollar)(hostname)",
        perms => m( "u+rwx" );
@nickanderson
nickanderson / newgist-org-mode-example-tangle-content-multiple-files-header-args.org
Last active April 23, 2025 18:26
Example tangling content to different places based on header arguments

Example for tangling the same content to different places based on header arguments

@nickanderson
nickanderson / CFEngine-bundle-parameters-are-optional.org
Created April 17, 2025 23:00
CFEngine Bundle Parameters Are Optional

Bundle parameters are optional.

Both in the sense of implementation, but also from the perspective of use.

I did not know (or if I did, i don’t even remember forgetting) that bundle parameters, if present are optional. In that, a bundle that has parameters can be called without parameters . However, if you call with non-zero parameters you must supply all (that part I knew).

bundle agent __main__
{
@nickanderson
nickanderson / example.org
Created April 17, 2025 21:08
variable index for masin
bundle agent __main__
{
  vars:
      "stuff" slist => { "some", "thing-s", "that's", "not valid", "variable names"};

      "classic_associative_array[$(stuff)]"
        string => string_upcase("$(stuff)");
}
@nickanderson
nickanderson / example-ifelse.org
Created April 17, 2025 13:10
ifelse() for masin
bundle agent php
{
	vars:
		"version" string => ifelse( "debian.!ubuntu", "8.0",
                                          "ubuntu", "8.2",
                                          "centos", "8.3",
                                          "freebsd", "82",
                                           " ¯\_(ツ)_/¯" );
@nickanderson
nickanderson / 20210818082229-variablesmatching_as_data.org
Created January 24, 2025 15:43
Examples illustrating variablesmatching_as_data() in CFEEngine

variablesmatching_as_data() is useful for centralizing information from disparate sources. For example, deriving a list of package names by extracting the values from variables that match a specific name.

 bundle agent package_names_from_variablesmatching_as_data
 # @brief Example illustrating deriving a list of package names from variables named with a specific naming convention
{
  vars:
      "packages_baseline" slist => { "bash-completion", "emacs" };
      "packages_os" slist => { "httpd" };
@nickanderson
nickanderson / README.org
Last active September 14, 2024 19:18
An example of doing something as the result of a promise outcome just for James Just James
@nickanderson The title piqued my interest so I had a read. Bearing in mind that I don't use cfengine,
so it's likely my fault for not understanding, but I would have liked to see how you do different
things _as_a_result_ of the different repaired vs kept vs reached scenarios.

Is it possible to do that? Say create a new file with contents that vary based on which of those
scenarios occured?

Thanks!
@nickanderson
nickanderson / bas-skipped-promises.org
Created July 3, 2024 14:08
Reproduce policy from chat
bundle agent __main__
{

    vars:
    "repo_data" data => '{"one": { "value": "something" }, "two": { "run_class": "linux" } }';
    "repo_names" slist => getindices(repo_data);
    reports:
          "$(this.bundle): yes or/not $(repo_names)"
              if => or(