STATES TUTORIAL(第四部分)

MOD AGGREGATE STATE RUNTIME MODIFICATIONS


 

ALTERING STATES


 

FILE STATE BACKUPS

可以在多个地方设置minion端的文件备份。
示例:

backup_mode: minion

1 /etc/ssh/sshd_config:
2   file.managed:
3     - source: salt://ssh/sshd_config
4     - backup: minion
View Code

BACKED-UP FILES

备份文件的位置位于minion端配置的file_backup目录下,后面添加了时间戳。


INTERACTING WITH BACKUPS

backup文件的操作,可以进行列出,删除,回滚等操作。


LISTING

# salt foo.bar.com file.list_backups /tmp/foo.txt
示例:

 1 foo.bar.com:
 2     ----------
 3     0:
 4         ----------
 5         Backup Time:
 6             Sat Jul 27 2013 17:48:41.738027
 7         Location:
 8             /var/cache/salt/minion/file_backup/tmp/foo.txt_Sat_Jul_27_17:48:41_738027_2013
 9         Size:
10             13
11     1:
12         ----------
13         Backup Time:
14             Sat Jul 27 2013 17:48:28.369804
15         Location:
16             /var/cache/salt/minion/file_backup/tmp/foo.txt_Sat_Jul_27_17:48:28_369804_2013
17         Size:
18             35
View Code

列出指定文件的备份文件列表


RESTORING

salt foo.bar.com file.restore_backup /tmp/foo.txt 1
示例:

1 foo.bar.com:
2     ----------
3     comment:
4         Successfully restored /var/cache/salt/minion/file_backup/tmp/foo.txt_Sat_Jul_27_17:48:28_369804_2013 to /tmp/foo.txt
5     result:
6         True
View Code

对指定备份文件进行回滚,现有的文件会被再次进行备份,可以使用file.list_backups查看


DELETING

使用file.delete_backup删除备份文件
# salt foo.bar.com file.delete_backup /tmp/foo.txt 0
示例:

1 foo.bar.com:
2     ----------
3     comment:
4         Successfully removed /var/cache/salt/minion/file_backup/tmp/foo.txt_Sat_Jul_27_18:00:19_822550_2013
5     result:
6         True
View Code

 

UNDERSTANDING STATE COMPILER ORDERING

state文件编译顺序


COMPILER BASICS

编译的简单知识


HIGH DATA AND LOW DATA

当我们讲state文件以yaml数据的格式编译之后,展示的是一个高级的数据结构,以一个大字典存在,可以被运行。在被编译成高级的数据结构之后接下来会被继续编译成底层的数据结构,底层的数据结构时执行的单状态调用的有序列表。

可以通过调用执行来查看细节:
  salt '*' state.show_highstate
  salt '*' state.show_lowstate

一个sls文件编写内容示例:

 1 apache:
 2   pkg.installed:
 3     - name: httpd
 4   service.running:
 5     - name: httpd
 6     - watch:
 7       - file: apache_conf
 8       - pkg: apache
 9 
10 apache_conf:
11   file.managed:
12     - name: /etc/httpd/conf.d/httpd.conf
13     - source: salt://apache/httpd.conf
View Code

一个High Data数据结构示例:

 1 {
 2     "apache": {
 3         "pkg": [
 4             {
 5                 "name": "httpd"
 6             },
 7             "installed",
 8             {
 9                 "order": 10000
10             }
11         ],
12         "service": [
13             {
14                 "name": "httpd"
15             },
16             {
17                 "watch": [
18                     {
19                         "file": "apache_conf"
20                     },
21                     {
22                         "pkg": "apache"
23                     }
24                 ]
25             },
26             "running",
27             {
28                 "order": 10001
29             }
30         ],
31         "__sls__": "blah",
32         "__env__": "base"
33     },
34     "apache_conf": {
35         "file": [
36             {
37                 "name": "/etc/httpd/conf.d/httpd.conf"
38             },
39             {
40                 "source": "salt://apache/httpd.conf"
41             },
42             "managed",
43             {
44                 "order": 10002
45             }
46         ],
47         "__sls__": "blah",
48         "__env__": "base"
49     }
50 }
View Code

一个Low Data数据结构示例:

 1 [
 2     {
 3         "name": "httpd",
 4         "state": "pkg",
 5         "__id__": "apache",
 6         "fun": "installed",
 7         "__env__": "base",
 8         "__sls__": "blah",
 9         "order": 10000
10     },
11     {
12         "name": "httpd",
13         "watch": [
14             {
15                 "file": "apache_conf"
16             },
17             {
18                 "pkg": "apache"
19             }
20         ],
21         "state": "service",
22         "__id__": "apache",
23         "fun": "running",
24         "__env__": "base",
25         "__sls__": "blah",
26         "order": 10001
27     },
28     {
29         "name": "/etc/httpd/conf.d/httpd.conf",
30         "source": "salt://apache/httpd.conf",
31         "state": "file",
32         "__id__": "apache_conf",
33         "fun": "managed",
34         "__env__": "base",
35         "__sls__": "blah",
36         "order": 10002
37     }
38 ]
View Code

ORDERING LAYERS

salt定义了两个顺序接口,这些接口在运行时进行评估。


DEFINITION ORDER

自定义顺序,设置state_auto_order为False
当top文件中在不包含include声明的情况下,顺序是比较容易确定的,在引入include之后排序规则会有一些简单的规则需要被掌握。


THE INCLUDE STATEMENT

示例:

foo.sls

1 include:
2   - bar
3   - baz
View Code

bar.sls

1 include:
2   - quo
View Code

baz.sls

1 include:
2   - qux
View Code

当执行state.apply时,执行的顺序将如下:

1 1.quo
2 2.bar
3 3.qux
4 4.baz
5 5.foo  
View Code

THE ORDER FLAG

顺序是通过默认的规则生成的,也可以显示的进行定义覆盖
示例:

1 apache:
2   pkg.installed:
3     - name: httpd
4     - order: 1
View Code

定义最后的顺序:order: last or order: -1


LEXICOGRAPHICAL FALL-BACK

REQUISITE ORDERING


RUNTIME REQUISITE EVALUATION

运行时对state中定义的组件顺序进行评估,需要根据一些依赖条件require,watch,prereq的情况进行判断。


SIMPLE RUNTIME EVALUATION EXAMPLE

以下是一个基本的评判执行顺序的规则,基于上面的示例:

  1、执行pkg.installed,因为它不包含任何依赖关系,所以第一个执行。
  2、接下来service.running将会被评估不被执行,按照顺序读取定义的依赖状态,对文件状态进行检查。
  3、执行apache_conf状态,因为它不依赖其他条件。
  4、service状态还是不被执行,需要继续检查pkg的执行状态,如果满足条件则执行。 

 


BEST PRACTICE

salt最佳的做法是选择其中一种方法坚持下去,设置failhard为True,确保在一次失败时停止所有的状态继续运行。

 

posted @ 2017-09-11 20:40  孤独的海浪  阅读(194)  评论(0编辑  收藏  举报