Re: Policy/Promise to agents

"'Nick Anderson' via help-cfengine" <[email protected]>
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
Ugh, ignore the links to promises.cf and creat_a_file.cf, something is 
trying to be helpful and make links to things that shouldn't be links.

On Tuesday, August 9, 2022 at 5:57:11 PM UTC-5 Nick Anderson wrote:

> Rakesh Patel <[email protected]> writes: 
>
> First new to cf CFengine. 
>
> Hi Rakesh, Welcome! I hope you enjoy your stay. 
>
> I have a sample file called creat_a_file.cf in the /var/cfengine/master 
> directory on the hub server. 
>
> File content 
>
> Listing 1: File content
>
> #!/var/cfengine/bin/cf-agent --no-lock
> body common control{
> bundlesequence =>{"create_test_file"};}
> bundle agent create_test_file{
>     files:
>             "/tmp/test-cfengine/test-file"
>
>             edit_line => addline,
>             create => "true";}
> bundle edit_line addline{
>     insert_lines: "Cfengine is running nicely.";
>
> This policy above seems to have been truncated early (it's missing a 
> trailing }, probably just a copy pasta error since you said that you get 
> the output you expect if you run the file directly. 
>
> The file is copying to the agents but not executing. I can manually run 
> the file and get the expected output but again not automatically. What am I 
> missing. I have read that I need to modify the promise.cf file on the hub 
> server but where and what? 
>
> What you read probably mentioned promises.cf (note the s). It's in the 
> root of the policy set. promises.cf is the first policy file read by 
> cfengine components if one is not specified. 
>
> However, that's rather old advice, at least if you are running the 
> Masterfiles Policy Framework (MPF), which is the default policy set you get 
> with CFEngine. 
>
> I assume you are running the MPF (documentation here 
> <https://docs.cfengine.com/docs/master/reference-masterfiles-policy-framework.html>). 
>
>
> Now, to what you are missing … 
>
> First, your snippet is written as a *standalone* policy. It's got a body 
> common control and there can only be one of those per policy entry 
> (policy entry is the first policy file read, e.g. promises.cf or whatever 
> you give to -f or --file options to the component like cf-agent -f 
> update.cf). So, if this policy file were included into the main policy (
> promises.cf) you would get errors. So, start by stripping out body common 
> control from the file. Let's replace it with a library main bundle 
> <https://docs.cfengine.com/docs/master/reference-language-concepts-bundles.html#library-main-bundles> 
> ( bundle agent __main__, a bundle that gets called as part of the default 
> bundlesequence if none is provided). In that bundle, let's add a methods 
> type promise 
> <https://docs.cfengine.com/docs/master/reference-promise-types-methods.html> 
> to actuate your bundle agent create_test_file. 
>
> So, you end up with this: 
> Listing 2: Example Policy
>
> #!/var/cfengine/bin/cf-agent --no-lockbundle agent __main__{
>   methods:
>       
>
> "create_test_file";}bundle agent create_test_file{
>   files:
>       "/tmp/test-cfengine/test-file"
>         edit_line => addline,
>         create => "true";}
> bundle edit_line addline{
>       insert_lines: "Cfengine is running nicely.";}
>
> Now, when you run the policy file directly it should still work as 
> expected: 
>
>
> # cf-agent --no-lock --log-level info --file /home/nickanderson/org/roam/daily/work/cfengine3-yn06AG
>     info: Created directory for '/tmp/test-cfengine/test-file'
>     info: Created file '/tmp/test-cfengine/test-file', mode 0600
>     info: Inserted the promised line 'Cfengine is running nicely.' into '/tmp/test-cfengine/test-file' after locator
>     info: insert_lines promise 'Cfengine is running nicely.' repaired
>     info: Edited file '/tmp/test-cfengine/test-file'
>
> Note: Pay no attention to my filename there, it's just a temporary file 
> that I stuck your policy into when I ran it with (ob-cfengine3 
> <https://github.com/nickanderson/ob-cfengine3>) 
>
> Now, you want to include this policy file as part of your larger policy 
> set (you want it to run with promises.cf runs). 
>
> For this I recommend using Augments 
> <https://docs.cfengine.com/docs/master/reference-language-concepts-augments.html>. 
> Create /var/cfengine/masterfiles/def.json with the following content. 
>
> {
>     "inputs": [ "creat_a_file.cf" ],
>     "vars": {
>         "control_common_bundlesequence_end": [ "create_test_file" ]
>     }}
>
> The *inputs* key is relative to the policy entry or a fully qualified 
> path. You said you have creat_a_file.cf in /var/cfengine/masterfiles/. 
> *masterfiles* is the *distribution* point. The update.cf policy copies 
> things to /var/cfengine/inputs and the components 
> <https://docs.cfengine.com/docs/master/reference-components-cf-monitord.html> 
> all read from /var/cfengine/inputs by default. So here you would either 
> specify /var/cfengine/inputs/creat_a_file.cf or simply creat_a_file.cf as 
> shown. Also, the inputs key is used only by the promises.cf policy entry. 
>
> Next we defined control_common_bundlesequence_end this gets defined as 
> default:def.control_common_bundlesequence_end (the default namespace, 
> bundle def, variable control_common_bundlesequence_end). Which the MPF 
> uses as the last entry in it's body common control bundlesequence 
> <https://github.com/cfengine/masterfiles/blob/7bd864a8848180f3650940d4d353148083f8cfea/promises.cf.in#L61>. 
> This tun-able is documented here 
> <https://docs.cfengine.com/docs/master/reference-masterfiles-policy-framework.html#append-to-the-main-bundlesequence> 
> in the Masterfiles Policy Framework section of the Reference MPF Reference. 
>
> Also, I know the agents update every 5 minutes, but how can I force to 
> update it manually? 
>
> After you have made the above changes force a policy update (/var/cfengine/bin/cf-agent 
> -KIf update.cf) then try a normal policy run, perhaps with -K (aka 
> --no-lock) and -I (aka --inform, or --log-level=info). 
>
> I hope you find this helpful, good luck on your journey! 
>
> You might also enjoy the ongoing The Agent is In 
> <https://www.youtube.com/watch?v=YYS1CUggomU&list=PLh71Vl9YjMaji_jnHFZ3k4mDbyOCE3A2x> 
> series. 
>

-- 
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/38c95acb-933c-4910-b99a-671cfb325d89n%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.