I want to know where config files on a minion come from.
Example
# /etc/postfix/main.cf
# source salt/mailserver/files/etc/postfix/main.cf.sls installed on 2016-10-18 12:42
....
I am lazy, and don't want to maintain above information myself.
There is a cool feature for ansible. The template system provides a variable {{ ansible_managed }}
This gets expanded to the source of the file and a time stamp.
See: http://docs.ansible.com/ansible/intro_configuration.html#ansible-managed
It would be great to have such a feature in salt, too.
Related StackO question: http://stackoverflow.com/questions/39747351/saltstack-reverse-engineering-where-a-file-comes-from
What do you think?
For a better following up I will add my comment in here.
First as a temporary solution you can create your own Pillar for example lets call it salt_managed and its value could be like this:
{% set managed_text = 'Salt managed: File modified on ' + salt.cmd.run('date "+DATE: %Y-%m-%d TIME: %H:%M:%S"') %}
salt_managed: {{ managed_text | yaml_dquote }}
Then on the minion you will get this result:
$ salt-call pillar.get salt_managed
local:
Salt managed: File modified on DATE: 2016-10-18 TIME: 11:12:40
And you can use this by adding it on the top of your config files for example like this
{{ pillar.get('salt_managed') }}
The main problem with this solution that i cannot get the user who executed a state from the event bus, if there is a way to achieve that it would be very helpful then.
what do you think ?
@mostafahussein implementing this via pillar looks good. I don't understand why you want to get the user who executed a state. At least in my environment it will always be root. I would like to know the source of this file or snippet (file.blockreplace)
I think this is a great idea. I'll approve as a feature request thanks
@guettli my environment has multiple user and we are using ssh keys. so i will be able to know who have updated the file and when.
For the source of the file you might add as you said on stackoverflow, if i found another way i will let you know
Should I add the above implementation as answer to your question ?
@Ch3LL Is there a way to access the event bus during state execution ? or even by creating a custom module ?
Update:
1- Can we get the file reference during execution ? (I can see it in state.show.sls results but i wish if it could be called inside the managed file itself)
2- Can we get the user who executed the formula ? (I know it could be done using returners but i mean during the execution)
@mostafahussein yes, please answer my StackO question with your sample implementation. It's a good starting point. I hope there is a way to figure out which file gets processed at the moment.
@guettli I have added it to your question. and if there is any other solutions then i will keep you updated.
@mostafahussein, to answer your first question, maybe try {{ tplfile }}. There are a few context variables that may be useful here.
https://github.com/saltstack/salt/blob/develop/salt/utils/templates.py#L141-L145
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.
I would like to see such a feature too.
@mostafahussein 's approach with the pillars is nice but has the downside that it always update the templates, even if nothing changes inside. That's not exactly what I would want in general.
Is there a solution to that?