问题
When I use saltstack
to manage my servers. I found an interesting thing:
When I run salt '*' pkg.installed httpd
, I get the following message: pkg.installed is not available
. But I can use pkg.installed
function in my .sls
files and it worked very well. So, I am confused about that. And I think this is happening because of saltstack
.
Who can help me?
回答1:
There are two related but different concepts here.
- Salt Execution Modules
- Salt State Modules.
Execution modules are where most of the work actually happens and is what you are running on the command line, generally. For example:
salt '*' pkg.install vim
That will call out directly to your OS's package manager, such as yum or apt, and install vim.
State modules are statefull commands that kind of sit "above" the execution modules. A state module will check if the desired result already exists and make any necessary changes to get the desired state. They are conjugated differently than the execution modules. For example in this salt state file (sls file):
cat /srv/salt/vim.sls
install_vim_please:
pkg.installed:
- name: vim
Then you could run the state.sls
execution module to apply this sls file with the pkg.installed
state.
salt '*' state.sls vim
Because we're using the pkg.installed
state Salt will check with your OS's package manager and see if vim is already installed. Salt will only attempt to install vim if the package manager says that vim is not installed already.
Keeping your Salt States in sls files makes it easy to keep them in git or whatever vcs you use to track them.
You could skip the sls file and run the command statefully from the command line like this:
salt '*' state.single pkg.installed name=vim
来源:https://stackoverflow.com/questions/37253393/whats-the-differences-between-functions-in-state-file-and-functions-in-command