SaltStack: reverse engineering where the file comes from

If you look at the host that was configured on SaltStack, then sometimes it looks like viewing the binary with vi.

You do not know how the config / file was created.

This makes it difficult to resolve errors when shooting. Reverse engineering in which the file occurs takes too much time.

My goal: make it easier to find the path to the unix configuration file in the minion (created by salt) to the source where this configuration came from. Like $Id$ in svn and cvs.

One idea with a friend and me:

The state of file.managed should (optionally) add the source of the file.

Example:

My sls file contains the following:

 file_foo_bar: file.managed: - source: - salt://foo/bar 

Then the created file should contain this comment.

 # Source: salt://foo/bar 

Of course, this is not so simple, as there are different ways to put comments in configuration files.

Is it possible? Or is there a better solution for my purpose.

Update

Usually I know what I did wrong and can easily find the root. The problem arises if several people are working on the state tree.

+5
source share
1 answer

This is the starting point where you can get the date and time of the modified file when it is managed by Salt using the Salt column .

Lets call our salt_managed variable. Create a post file as follows:

 {% set managed_text = 'Salt managed: File modified on ' + salt.cmd.run('date "+%Y-%m-%d %H:%M:%S"') %} salt_managed: {{ managed_text | yaml_dquote }} 

Then in the minion, when you call the pillar, you will get the following result:

 $ salt-call pillar.get salt_managed local: Salt managed: File modified on 2016-10-18 11:12:40 

And you can use this by adding it to the top of the configuration files, for example:

 {{ pillar.get('salt_managed') }} 

Update:

I found a job that might be helpful to someone. Suppose we have several states that can modify the same file. How can we know that state X is responsible for modifying this file? by following these steps:

1- I created a state like this:

 Create a File: file.managed: - name: /path/to/foofile - source: salt://statedir/barfile Add file header: file.prepend: - name: /path/to/foofile - text: "This file was managed by using this salt state {{ sls }}" 

The contents of the barfile file:

 This is a new file 

2- Call the state from the minion, and this will be the result:

 $ salt-call state.sls statedir.test local: ---------- ID: Create a File Function: file.managed Name: /path/to/foofile Result: True Comment: File /path/to/foofile updated Started: 07:50:45.254994 Duration: 1034.585 ms Changes: ---------- diff: New file mode: 0644 ---------- ID: Add file header Function: file.prepend Name: /path/to/foofile Result: True Comment: Prepended 1 lines Started: 07:50:46.289766 Duration: 3.69 ms Changes: ---------- diff: --- +++ @@ -1,1 +1,2 @@ +This file was managed by using this salt state statedir.test This is a new file Summary for local ------------ Succeeded: 2 (changed=2) Failed: 0 ------------ Total states run: 2 

Currently foofile content:

 This file was managed by using this salt state statedir.test This is a new file 
+1
source

Source: https://habr.com/ru/post/1257388/


All Articles