Hi, I'm new to this list and to CFEngine in general, so forgive me if there's a simple answer to this question that I'm not aware of.
I'm trying to pull in a list of packages for installation. Some packages require a specific version number, while many others do not. So the package list looks something like this: package1 package2 package3==1.5 package4 package5==2.0 package6 I'm using readstringarrayidx to pull the package list in from a file and parse it into an associative array (full test bundle below). Each line gets split into its own array containing the package name and, if applicable, the version number. The parsed array looks something like this: package_list[0][0] = "package1" package_list[1][0] = "package2" package_list[2][0] = "package3" package_list[2][1] = "1.5" package_list[3][0] = "package4" package_list[4][0] = "package5" package_list[4][1] = "2.0" package_list[5][0] = "package6" Note that if a package specifies a version number its array has two elements, but if no version is specified it only has one. This causes a problem when I try to iterate over the package list. Where the second element doesn't exist, its variable call is interpreted by CFEngine as a string literal, which is then passed to the package manager in place of the non-existent version number. Obviously that's no good. To illustrate what I mean: When I execute the attached bundle (omitting the 'packages' section), the reports look like this: R: package count: 6 R: package name & version: package1 - $(package_list[0][1]) R: package name & version: package2 - $(package_list[1][1]) R: package name & version: package3 - 1.5 R: package name & version: package4 - $(package_list[3][1]) R: package name & version: package5 - 2.0 R: package name & version: package6 - $(package_list[5][1]) Is it normal behavior for CFEngine to interpret calls to non-existent variables as string literals? I was expecting that it would expand to an empty string, which I confess was mostly based on wishful thinking. It seems my best bet is to somehow normalize the package list before CFEngine parses it. But does anyone have any tips on a better way to structure this recipe to allow for optional inclusion of version numbers without handing bogus variable calls over to the package manager? Thanks. Full bundle: bundle agent install_some_packages { vars: "package_count" int => readstringarrayidx("package_list", "$(sys.workdir)/inputs/packages.txt", "\s*#[^\n]*", "==", "inf", "inf"); "package_indices" slist => getindices("package_list"); packages: # this is non-functional and is included as an example of the desired functionality "$(package_list[$(package_indices)][0])" comment => "install all the packages in the package list", package_policy => "addupdate", package_select => "==", package_version => "$(package_list[$(package_indices)][1])"; reports: cfengine:: "package count: $(package_count)"; "package name & version: $(package_list[$(package_indices)][0]) - $(package_list[$(package_indices)][1])"; } Ian Westcott i...@percolate.com m: 401-954-9506
_______________________________________________ Help-cfengine mailing list Help-cfengine@cfengine.org https://cfengine.org/mailman/listinfo/help-cfengine