配置管理从这里开始
1、查看所有 states 列表
[root@SaltMaster1(10.182.88.136)]$~:>salt '*' sys.list_state_modules 10.182.76.78: - alias - alternatives - archive - artifactory - beacon - bigip - blockdev - buildout - ceph - chronos_job - cloud - cmd - composer - cron - ……
2、查看指定file.states的所有function
查看file.states的所有function,命令如下所示:
[root@SaltMaster1(10.182.88.136)]$~:>salt '10.182.76.78' sys.list_state_functions file 10.182.76.78: - file.absent - file.accumulated - file.append - file.blockreplace - file.cached - file.comment - file.copy - file.decode - file.directory - file.exists - file.line - file.managed - file.missing - file.mknod - file.mod_run_check_cmd - file.not_cached - file.patch - file.prepend - file.recurse - file.rename - file.replace - file.retention_schedule - file.sdecode - file.serialize - file.shortcut - file.symlink - file.touch - file.uncomment
3、查看指定states用法
要查看file.states的详细用法与例子,命令如下所示:
[root@SaltMaster1(10.182.88.136)]$~:>salt '10.182.76.78' sys.state_doc file|more 10.182.76.78: ---------- file: Operations on regular files, special files, directories, and symlinks ===================================================================== Salt States can aggressively manipulate files on a system. There are a number of ways in which files can be managed. Regular files can be enforced with the :mod:`file.managed <salt.states.file.managed>` state. This state downloads files from the salt master and places them on the target system. Managed files can be rendered as a jinja, mako, or wempy template, adding a dynamic component to file management. An example of :mod:`file.managed <salt.states.file.managed>` which makes use of the jinja templating system would look like this: /etc/http/conf/http.conf: file.managed: - source: salt://apache/http.conf - user: root - group: root - mode: 644 - attrs: ai - template: jinja - defaults: custom_var: "default value" other_var: 123 {% if grains['os'] == 'Ubuntu' %} - context: custom_var: "override" {% endif %} It is also possible to use the :mod:`py renderer <salt.renderers.py>` as a templating option. The template would be a Python script which would need to contain a function called ``run()``, which returns a string. All arguments to the state will be made available to the Python script as globals. The returned string will be the contents of the managed file. For example: def run(): lines = ['foo', 'bar', 'baz'] lines.extend([source, name, user, context]) # Arguments as globals return '\n\n'.join(lines)
……
由于结果显示行数较多,这里只贴出前部分。里面介绍了file所有states资源的用法与介绍,在我们日常编写states工作中,只需要按照例子编写即可。
4、查看指定states指定function用法
要查看file.managed states的详细用法与例子,命令如下所示:
[root@SaltMaster1(10.182.88.136)]$~:>salt '10.182.76.78' sys.state_doc file.managed|more 10.182.76.78: ---------- file: Operations on regular files, special files, directories, and symlinks ===================================================================== Salt States can aggressively manipulate files on a system. There are a number of ways in which files can be managed. Regular files can be enforced with the :mod:`file.managed <salt.states.file.managed>` state. This state downloads files from the salt master and places them on the target system. Managed files can be rendered as a jinja, mako, or wempy template, adding a dynamic component to file management. An example of :mod:`file.managed <salt.states.file.managed>` which makes use of the jinja templating system would look like this: /etc/http/conf/http.conf: file.managed: - source: salt://apache/http.conf - user: root - group: root - mode: 644 - attrs: ai - template: jinja - defaults: custom_var: "default value" other_var: 123 {% if grains['os'] == 'Ubuntu' %} - context: custom_var: "override" {% endif %} It is also possible to use the :mod:`py renderer <salt.renderers.py>` as a templating option. The template would be a Python script which would need to contain a function called ``run()``, which returns a string. All arguments to the state will be made available to the Python script as globals. The returned string will be the contents of the managed file. For example: def run(): lines = ['foo', 'bar', 'baz'] lines.extend([source, name, user, context]) # Arguments as globals return '\n\n'.join(lines) --More--
5、从一个简单的实例去了解states
上面介绍的是通过命令查看states相关的知识。下面我们开始通过一些小例子来实现一个简单的配置管理工作,这里引出的例子只是为了介绍states的一些流程与使用。
- 编写top.sls文件(非必须)
- 编写states.sls文件
在