Ever wanted to manipulate a string - temporarily - for an individual promise? Check out the with attribute and its special, $(with) variable.
Sometimes you need some variation on a string for a specific case. Traditionally, to achieve this you’d simply define another variable.
Here is a contrived example:
I have a string, nginx and I want to emit a report that contains both the string itself and the upper case version of the string.
Looking for a way to concisely set a variable conditionally? Have you heard of ifelse()?
In CFEngine, traditionally class expressions are used to constrain promises to different contexts. Setting a variable to different values based on context might look like this:
/tmp/feature-friday-1.cf bundle agent __main__ { vars: "MyVariable" string => "My Default value"; redhat_8|centos_8|rocky_8:: "MyVariable" string => "My value for EL 8"; ubuntu_22:: "MyVariable" string => "My value for Ubuntu 22"; any:: "MyVariable" string => "My value on Friday", if => "Friday"; reports: "It's $(sys.date) and I am running on $(sys.os_release[PRETTY_NAME])"; "MyVariable is '$(MyVariable)'"; } command cf-agent --no-lock --log-level info --file /tmp/feature-friday-1.cf output R: It's Mon Mar 11 12:36:41 2024 and I am running on Ubuntu 22.04.4 LTS R: MyVariable is 'My value for Ubuntu 22' That’s great, lots of flexibility, but with an increasing number of options the policy can get quite long and it’s easier for a human interpreter to lose track of the context. The same can be achieved in a single statement using ifelse().
Last year in commemoration of 30 years of CFEngine I received this lovely coin.
I thought it would be fun to celebrate by writing 42 blog posts, each with some little tip about CFEngine. So, be sure to come check in on Friday for the first post in the Feature Friday series.
Series Posts ifelse() $(with) execresult_as_data() cf-remote cfbs cf-promises depends_on –bundlesequence body file control - inputs cf-support namespaces special variables classesmatching() variablesmatching() & variablesmatching_as_data() bundlesmatching() host info report Tags for Inventory and Reporting Augments - def.json What variables and classes are defined? Macros Promisees or Stakeholdes Don’t fix, just warn Agent say! Augments - host_specific.json Unprivileged execution Groups custom promise type Multiple outcomes Restricting individual promises using if and unless Variable class expressions Agent svg Seeing a data structure with storejson() Doing math in policy with eval() Why associative arrays when data containers exist? Self organizing groups Groups in Mission Portal Formatting policy with cffmt Decisions based on arbitrary semantic versions Developing modules that take input The power of lists and implicit iteration What would CFEngine do? How can I quote thee, let me count the ways ob-cfengine3