实验目标

1.使用SaltStack部署apache和php,

2.使用salt管理httpd.conf配置文件配置访问info.php使用账户密码

3.在salt里面增加对conf.d目录进行配置管理

4.如何使用salt在追加文件内容

5.学会如何使用 watch require unless

实现步骤

修改master的配置文件,指定base环境路径,base环境是必须指定的
  1. [root@linux-node1 base]# grep - ^file_roots /etc/salt/master |grep -v ^#
  2. file_roots:
  3. base:
  4. - /srv/salt/base
  5. dev:
  6. - /srv/salt/dev
  7. test:
  8. - /srv/salt/test
  9. prod:
  10. - /srv/salt/prod
创建目录
  1. [root@linux-node1 base]# mkdir -p /srv/salt/{base,dev,test,prod}
  2. [root@linux-node1 base]# tree /srv/salt/
  3. /srv/salt/
  4. ├── base
  5. ├── dev
  6. ├── prod
  7. └── test
重启master
  1. [root@linux-node1 base]# systemctl restart salt-master
在base目录下面创建一个web目录用于存放web相关的sls文件
  1. [root@linux-node1 base]# mkdir -p web
cd到bash/web目录里面创建apache.sls文件
  1. [root@linux-node1 base]# cd web/
  2. [root@linux-node1 web]# cat apache.sls
  3. apache-install: #id 名字自己取 需要形象一点, 一个id下面一个状态只能出现一次
  4. pkg.installed: #pkg 是状态模块,installed 是模块里面的方法
  5. - name: httpd #方法里面的参数
  6. apache-service:
  7. service.running:
  8. - name: httpd
  9. - enable: True #设置开机自动启动
    #yaml里面格式有严格的要求,注释用#号,不能有table,- 两边需要空格,缩进用2个空格层级关系后面要加分号
执行状态模块部署服务
  1. [root@linux-node1 base]# salt "linux-node2*" state.sls apache
  2. linux-node2.example.com:
  3. ----------
  4. ID: apache-install
  5. Function: pkg.installed
  6. Name: httpd
  7. Result: True
  8. Comment: Package httpd is already installed.
  9. Started: ::09.228934
  10. Duration: 633.681 ms
  11. Changes:
  12. ----------
  13. ID: apache-service
  14. Function: service.running
  15. Name: httpd
  16. Result: True
  17. Comment: Service httpd is already enabled, and is running
  18. Started: ::09.863302
  19. Duration: 310.567 ms
  20. Changes:
  21. ----------
  22. httpd:
  23. True
  24.  
  25. Summary
  26. ------------
  27. Succeeded: (changed=)
  28. Failed:
  29. ------------
  30. Total states run: 2
    #此时node2 上面已经部署好了apache
高级状态的使用 需要在master配置文件里面打开 state_top: top.sls并重启master

[root@linux-node1 web]# grep -n ^state_top /etc/salt/master
329:state_top: top.sls
[root@linux-node1 web]# systemctl restart salt-master

在bese环境目录下面添加top.sls
  1. [root@linux-node1 base]# more top.sls
  2. base:
  3. 'linux-node2.example.com':
  4. - web.apache
  5. 'linux-node1.example.com':
  6. - web.apache
  7. [root@linux-node1 base]# pwd
  8. /srv/salt/base
执行高级模块方法,高级方法到 base下面找top.sls  文件编排告诉每个minion需要干什么,一般生产环境用高级状态多些
  1. [root@linux-node1 base]# salt "*" state.highstate
  2. linux-node1.example.com:
  3. ----------
  4. ID: apache-install
  5. Function: pkg.installed
  6. Name: httpd
  7. Result: True
  8. Comment: Package httpd is already installed.
  9. Started: ::08.597951
  10. Duration: 709.521 ms
  11. Changes:
  12. ----------
  13. ID: apache-service
  14. Function: service.running
  15. Name: httpd
  16. Result: True
  17. Comment: Service httpd is already enabled, and is in the desired state
  18. Started: ::09.308417
  19. Duration: 233.623 ms
  20. Changes:
  21.  
  22. Summary
  23. ------------
  24. Succeeded:
  25. Failed:
  26. ------------
  27. Total states run:
  28. linux-node2.example.com:
  29. ----------
  30. ID: apache-install
  31. Function: pkg.installed
  32. Name: httpd
  33. Result: True
  34. Comment: Package httpd is already installed.
  35. Started: ::09.171596
  36. Duration: 721.901 ms
  37. Changes:
  38. ----------
  39. ID: apache-service
  40. Function: service.running
  41. Name: httpd
  42. Result: True
  43. Comment: Service httpd is already enabled, and is in the desired state
  44. Started: ::09.894209
  45. Duration: 221.615 ms
  46. Changes:
  47.  
  48. Summary
  49. ------------
  50. Succeeded:
  51. Failed:
  52. ------------
  53. Total states run:
上面我们使用了2个状态模块pkg和service,下面我们使用file文件配置模块

模块使用参考文档

  1. https://www.unixhot.com/docs/saltstack/ref/states/all/salt.states.file.html#module-salt.states.file
在base/web目录下面添加一个lamp.sls,一般在添加里面的内容之前需要在外面找一台服务器进行测试拿到准确的包信息后再进行配置

[root@linux-node1 web]# cat lamp.sls
lamp-install:
pkg.installed:
- pkgs:
- httpd
- php
- php-pdo
- php-mysql

apache-config:
file.managed:
- name: /etc/httpd/conf/httpd.conf #服务实际使用的文件路径
- source: salt://web/files/httpd.conf #salt的源文件用于分发到minion上面 路径是base目录下面的web 这里也支持http和ftp方式
- user: root
- group: root
- mode: 644

php-config:
file.managed:
- name: /etc/php.ini
- source: salt://web/files/php.ini
- user: root
- group: root
- mode: 644

lamp-service:
service.running:
- name: httpd
- enable: True

拷贝源文件到base/web目录下,这个根据自己的实际情况找源文件拷贝过来

[root@linux-node1 web]# cp /etc/httpd/conf/httpd.conf /srv/salt/base/web/files/
[root@linux-node1 web]# cp /etc/php.ini /srv/salt/base/web/files/

执行状态模块部署服务
  1. [root@linux-node1 web]# salt "*" state.sls web.lamp
  2. linux-node1.example.com:
  3. ----------
  4. ID: lamp-install
  5. Function: pkg.installed
  6. Result: True
  7. Comment: All specified packages are already installed.
  8. Started: ::56.883540
  9. Duration: 633.814 ms
  10. Changes:
  11. ----------
  12. ID: apache-config
  13. Function: file.managed
  14. Name: /etc/httpd/conf/httpd.conf
  15. Result: True
  16. Comment: File /etc/httpd/conf/httpd.conf is in the correct state
  17. Started: ::57.520199
  18. Duration: 4.242 ms
  19. Changes:
  20. ----------
  21. ID: php-config
  22. Function: file.managed
  23. Name: /etc/php.ini
  24. Result: True
  25. Comment: File /etc/php.ini is in the correct state
  26. Started: ::57.524589
  27. Duration: 4.149 ms
  28. Changes:
  29. ----------
  30. ID: lamp-service
  31. Function: service.running
  32. Name: httpd
  33. Result: True
  34. Comment: Service httpd is already enabled, and is in the desired state
  35. Started: ::57.529404
  36. Duration: 258.952 ms
  37. Changes:
  38.  
  39. Summary
  40. ------------
  41. Succeeded:
  42. Failed:
  43. ------------
  44. Total states run:
  45. linux-node2.example.com:
  46. ----------
  47. ID: lamp-install
  48. Function: pkg.installed
  49. Result: True
  50. Comment: All specified packages are already installed.
  51. Started: ::58.566172
  52. Duration: 611.409 ms
  53. Changes:
  54. ----------
  55. ID: apache-config
  56. Function: file.managed
  57. Name: /etc/httpd/conf/httpd.conf
  58. Result: True
  59. Comment: File /etc/httpd/conf/httpd.conf is in the correct state
  60. Started: ::59.180091
  61. Duration: 4.063 ms
  62. Changes:
  63. ----------
  64. ID: php-config
  65. Function: file.managed
  66. Name: /etc/php.ini
  67. Result: True
  68. Comment: File /etc/php.ini is in the correct state
  69. Started: ::59.184248
  70. Duration: 3.803 ms
  71. Changes:
  72. ----------
  73. ID: lamp-service
  74. Function: service.running
  75. Name: httpd
  76. Result: True
  77. Comment: Service httpd is already enabled, and is in the desired state
  78. Started: ::59.188496
  79. Duration: 208.1 ms
  80. Changes:
  81.  
  82. Summary
  83. ------------
  84. Succeeded:
  85. Failed:
  86. ------------
  87. Total states run:
使用file模块下面的recurse方法进行apache的conf.d目录管理配置如下
  1. apache-conf:
  2. file.recurse:
  3. - name: /etc/httpd/conf.d
  4. - source: salt://web/files/apache-conf.d
创建salt源目录,并拷贝数据导源文件目录,数据文件来源根据自己业务的实际情况
  1. [root@linux-node1 ~]# mkdir /srv/salt/base/web/files/apache-conf.d
  2. [root@linux-node1 ~]# cd /srv/salt/base/web/files/apache-conf.d/
  3. [root@linux-node1 apache-conf.d]# cp -a /etc/httpd/conf.d/* .
  4. [root@linux-node1 apache-conf.d]# ls
  5. autoindex.conf php.conf README userdir.conf welcome.conf
  6. [root@linux-node1 apache-conf.d]#

测试在files/apache-conf.d/welcome.conf 添加一行#xiewenming test

  1. [root@linux-node1 files]# echo "#xieweming test" >> apache-conf.d/welcome.conf
验证目录管理是否生效

可以先使用test=True 只做测试,不会在minion节点上面真正执行,确认无问题后再让minion去执行

  1. [root@linux-node1 files]# salt "linux-node2*" state.highstate test=True
  2. linux-node2.example.com:
  3. ----------
  4. ID: lamp-install
  5. Function: pkg.installed
  6. Result: True
  7. Comment: All specified packages are already installed.
  8. Started: ::56.440265
  9. Duration: 666.288 ms
  10. Changes:
  11. ----------
  12. ID: apache-config
  13. Function: file.managed
  14. Name: /etc/httpd/conf/httpd.conf
  15. Result: True
  16. Comment: The file /etc/httpd/conf/httpd.conf is in the correct state
  17. Started: ::57.108448
  18. Duration: 3.959 ms
  19. Changes:
  20. ----------
  21. ID: php-config
  22. Function: file.managed
  23. Name: /etc/php.ini
  24. Result: True
  25. Comment: The file /etc/php.ini is in the correct state
  26. Started: ::57.112503
  27. Duration: 3.61 ms
  28. Changes:
  29. ----------
  30. ID: lamp-service
  31. Function: service.running
  32. Name: httpd
  33. Result: True
  34. Comment: Service httpd is already enabled, and is in the desired state
  35. Started: ::57.116505
  36. Duration: 244.585 ms
  37. Changes:
  38. ----------
  39. ID: apache-conf
  40. Function: file.recurse
  41. Name: /etc/httpd/conf.d
  42. Result: None
  43. Comment: #### /etc/httpd/conf.d/welcome.conf ####
  44. The file /etc/httpd/conf.d/welcome.conf is set to be changed
  45. Started: ::57.361390
  46. Duration: 1096.52 ms
  47. Changes:
  48. ----------
  49. /etc/httpd/conf.d/welcome.conf:
  50. ----------
  51. diff:
  52. ---
  53. +++
  54. @@ -, +, @@
  55. Alias /noindex/css/open-sans.css /usr/share/httpd/noindex/css/open-sans.css
  56. Alias /images/apache_pb.gif /usr/share/httpd/noindex/images/apache_pb.gif
  57. Alias /images/poweredby.png /usr/share/httpd/noindex/images/poweredby.png
  58. +#xieweming test
  59.  
  60. Summary
  61. ------------
  62. Succeeded: (unchanged=, changed=)
  63. Failed:
  64. ------------
  65. Total states run:
验证没有问题在node1和node2上面都执行

我们这里就2台所有可以直接用*

  1. [root@linux-node1 files]# salt "*" state.highstate
  2. linux-node1.example.com:
  3. ----------
  4. ID: lamp-install
  5. Function: pkg.installed
  6. Result: True
  7. Comment: All specified packages are already installed.
  8. Started: ::31.724191
  9. Duration: 782.903 ms
  10. Changes:
  11. ----------
  12. ID: apache-config
  13. Function: file.managed
  14. Name: /etc/httpd/conf/httpd.conf
  15. Result: True
  16. Comment: File /etc/httpd/conf/httpd.conf is in the correct state
  17. Started: ::32.509438
  18. Duration: 4.62 ms
  19. Changes:
  20. ----------
  21. ID: php-config
  22. Function: file.managed
  23. Name: /etc/php.ini
  24. Result: True
  25. Comment: File /etc/php.ini is in the correct state
  26. Started: ::32.514200
  27. Duration: 4.418 ms
  28. Changes:
  29. ----------
  30. ID: lamp-service
  31. Function: service.running
  32. Name: httpd
  33. Result: True
  34. Comment: Service httpd is already enabled, and is in the desired state
  35. Started: ::32.519273
  36. Duration: 234.566 ms
  37. Changes:
  38. ----------
  39. ID: apache-conf
  40. Function: file.recurse
  41. Name: /etc/httpd/conf.d
  42. Result: True
  43. Comment: Recursively updated /etc/httpd/conf.d
  44. Started: ::32.754002
  45. Duration: 1082.389 ms
  46. Changes:
  47. ----------
  48. /etc/httpd/conf.d/welcome.conf:
  49. ----------
  50. diff:
  51. ---
  52. +++
  53. @@ -, +, @@
  54. Alias /noindex/css/open-sans.css /usr/share/httpd/noindex/css/open-sans.css
  55. Alias /images/apache_pb.gif /usr/share/httpd/noindex/images/apache_pb.gif
  56. Alias /images/poweredby.png /usr/share/httpd/noindex/images/poweredby.png
  57. +#xieweming test
  58.  
  59. Summary
  60. ------------
  61. Succeeded: (changed=)
  62. Failed:
  63. ------------
  64. Total states run:
  65. linux-node2.example.com:
  66. ----------
  67. ID: lamp-install
  68. Function: pkg.installed
  69. Result: True
  70. Comment: All specified packages are already installed.
  71. Started: ::32.296865
  72. Duration: 789.23 ms
  73. Changes:
  74. ----------
  75. ID: apache-config
  76. Function: file.managed
  77. Name: /etc/httpd/conf/httpd.conf
  78. Result: True
  79. Comment: File /etc/httpd/conf/httpd.conf is in the correct state
  80. Started: ::33.089019
  81. Duration: 3.807 ms
  82. Changes:
  83. ----------
  84. ID: php-config
  85. Function: file.managed
  86. Name: /etc/php.ini
  87. Result: True
  88. Comment: File /etc/php.ini is in the correct state
  89. Started: ::33.092933
  90. Duration: 3.459 ms
  91. Changes:
  92. ----------
  93. ID: lamp-service
  94. Function: service.running
  95. Name: httpd
  96. Result: True
  97. Comment: Service httpd is already enabled, and is in the desired state
  98. Started: ::33.096823
  99. Duration: 232.349 ms
  100. Changes:
  101. ----------
  102. ID: apache-conf
  103. Function: file.recurse
  104. Name: /etc/httpd/conf.d
  105. Result: True
  106. Comment: Recursively updated /etc/httpd/conf.d
  107. Started: ::33.329410
  108. Duration: 1079.801 ms
  109. Changes:
  110. ----------
  111. /etc/httpd/conf.d/welcome.conf:
  112. ----------
  113. diff:
  114. ---
  115. +++
  116. @@ -, +, @@
  117. Alias /noindex/css/open-sans.css /usr/share/httpd/noindex/css/open-sans.css
  118. Alias /images/apache_pb.gif /usr/share/httpd/noindex/images/apache_pb.gif
  119. Alias /images/poweredby.png /usr/share/httpd/noindex/images/poweredby.png
  120. +#xieweming test
  121.  
  122. Summary
  123. ------------
  124. Succeeded: (changed=)
  125. Failed:
  126. ------------
  127. Total states run:

salt "*" state.highstate

使用watch在apache配置文件发送变化时,重新加载apache配置

增加下面的红色字体部分

  1. [root@linux-node1 web]# more lamp.sls
  2. lamp-install:
  3. pkg.installed:
  4. - pkgs:
  5. - httpd
  6. - php
  7. - php-pdo
  8. - php-mysql
  9.  
  10. apache-config:
  11. file.managed:
  12. - name: /etc/httpd/conf/httpd.conf
  13. - source: salt://web/files/httpd.conf
  14. - user: root
  15. - group: root
  16. - mode:
  17.  
  18. php-config:
  19. file.managed:
  20. - name: /etc/php.ini
  21. - source: salt://web/files/php.ini
  22. - user: root
  23. - group: root
  24. - mode:
  25.  
  26. lamp-service:
  27. service.running:
  28. - name: httpd
  29. - enable: True
  30. - reload: True #如果不加reload 默认会重启服务
  31. - watch: #增加
  32. - file: apache-config #监控上面的apache-config ID 所以说 一个ID在一个状态只能出现一次
  1. apache-conf:
  2. file.recurse:
  3. - name: /etc/httpd/conf.d
  4. - source: salt://web/files/apache-conf.d

另外一种watc_in写法,我们只需要掌握一种就可以

  1. ...
  2. lamp-service:
  3. service.running:
  4. - name: httpd
  5. - enable: True
  6. - reload: True
  7. - watch:
  8. - file: apache-config
  9. apache-conf:
  10. file.recurse:
  11. - name: /etc/httpd/conf.d
  12. - source: salt://web/files/apache-conf.d
  13. - watch_in:
  14. - service: lamp-service
  15. ...

修改一下配置文件进行验证成功

  1. [root@linux-node1 files]# salt "*" state.highstate
  2. linux-node1.example.com:
  3. ----------
  4. ID: lamp-install
  5. Function: pkg.installed
  6. Result: True
  7. Comment: All specified packages are already installed.
  8. Started: ::08.336027
  9. Duration: 733.712 ms
  10. Changes:
  11. ----------
  12. ID: apache-config
  13. Function: file.managed
  14. Name: /etc/httpd/conf/httpd.conf
  15. Result: True
  16. Comment: File /etc/httpd/conf/httpd.conf updated
  17. Started: ::09.071795
  18. Duration: 13.576 ms
  19. Changes:
  20. ----------
  21. diff:
  22. ---
  23. +++
  24. @@ -, +, @@
  25. # same ServerRoot for multiple httpd daemons, you will need to change at
  26. # least PidFile.
  27. #
  28. +
  29. ServerRoot "/etc/httpd"
  30.  
  31. #
  32. ----------
  33. ID: php-config
  34. Function: file.managed
  35. Name: /etc/php.ini
  36. Result: True
  37. Comment: File /etc/php.ini is in the correct state
  38. Started: ::09.085478
  39. Duration: 3.597 ms
  40. Changes:
  41. ----------
  42. ID: lamp-service
  43. Function: service.running
  44. Name: httpd
  45. Result: True
  46. Comment: Service reloaded
  47. Started: ::09.337223
  48. Duration: 253.101 ms
  49. Changes:
  50. ----------
  51. httpd:
  52. True
  53. ----------
  54. ID: apache-conf
  55. Function: file.recurse
  56. Name: /etc/httpd/conf.d
  57. Result: True
  58. Comment: The directory /etc/httpd/conf.d is in the correct state
  59. Started: ::09.590622
  60. Duration: 25.654 ms
  61. Changes:
  62.  
  63. Summary
  64. ------------
  65. Succeeded: (changed=)
  66. Failed:
  67. ------------
  68. Total states run:
  69. linux-node2.example.com:
  70. ----------
  71. ID: lamp-install
  72. Function: pkg.installed
  73. Result: True
  74. Comment: All specified packages are already installed.
  75. Started: ::08.904921
  76. Duration: 735.305 ms
  77. Changes:
  78. ----------
  79. ID: apache-config
  80. Function: file.managed
  81. Name: /etc/httpd/conf/httpd.conf
  82. Result: True
  83. Comment: File /etc/httpd/conf/httpd.conf updated
  84. Started: ::09.643019
  85. Duration: 16.038 ms
  86. Changes:
  87. ----------
  88. diff:
  89. ---
  90. +++
  91. @@ -, +, @@
  92. # same ServerRoot for multiple httpd daemons, you will need to change at
  93. # least PidFile.
  94. #
  95. +
  96. ServerRoot "/etc/httpd"
  97.  
  98. #
  99. ----------
  100. ID: php-config
  101. Function: file.managed
  102. Name: /etc/php.ini
  103. Result: True
  104. Comment: File /etc/php.ini is in the correct state
  105. Started: ::09.659260
  106. Duration: 3.724 ms
  107. Changes:
  108. ----------
  109. ID: lamp-service
  110. Function: service.running
  111. Name: httpd
  112. Result: True
  113. Comment: Service reloaded
  114. Started: ::09.900780
  115. Duration: 255.082 ms
  116. Changes:
  117. ----------
  118. httpd:
  119. True
  120. ----------
  121. ID: apache-conf
  122. Function: file.recurse
  123. Name: /etc/httpd/conf.d
  124. Result: True
  125. Comment: The directory /etc/httpd/conf.d is in the correct state
  126. Started: ::10.156119
  127. Duration: 165.767 ms
  128. Changes:
  129.  
  130. Summary
  131. ------------
  132. Succeeded: (changed=)
  133. Failed:
  134. ------------
  135. Total states run:
使用require可以让各ID之间产生依赖关系,避免无效执行

比如执行apache-config ID之前要确保  lamp-install ID已经成功的完成了  添加下面红色字体部分

  1. lamp-install:
  2. pkg.installed:
  3. - pkgs:
  4. - httpd
  5. - php
  6. - php-pdo
  7. - php-mysql
  8.  
  9. apache-config:
  10. file.managed:
  11. - name: /etc/httpd/conf/httpd.conf
  12. - source: salt://web/files/httpd.conf
  13. - user: root
  14. - group: root
  15. - mode:
  16. - require:
  17. - pkg: lamp-install
测试apache php环境工作是否正常,在2个节点上的apache工作目录下创建phpinfo文件
  1. [root@linux-node2 conf.d]# cd /var/www/html/
  2. [root@linux-node2 html]# mkdir admin
  3. [root@linux-node2 html]# cd admin/
  4. [root@linux-node2 admin]# vi info.php
  5. [root@linux-node2 admin]# cat info.php
  6. <?php
  7. phpinfo()
  8. ?>

可以正常打开

现在使用salt添加访问phpinfo需要账号密码

在salt的apache的配置管理文件里面添加验证 如下红色字体部分

[root@linux-node1 files]# pwd
/srv/salt/base/web/files
[root@linux-node1 files]#
[root@linux-node1 files]# tail -15 httpd.conf
#EnableMMAP off
EnableSendfile on
<Directory "/var/www/html/admin">
AllowOverride All
Order allow,deny
Allow from All
AuthUserFile /etc/httpd/conf/htpasswd_file
AuthName "hehe"
AuthType Basic
Require user admin
</Directory>
# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

在lamp.sls 里面添加一个名为apache-auth 状态ID并使用require指定依赖关系
  1. [root@linux-node1 web]# pwd
  2. /srv/salt/base/web
  3. [root@linux-node1 web]# ls
  4. apache.sls files lamp.sls
  5. [root@linux-node1 web]# tail - lamp.sls
  6. - name: /etc/httpd/conf.d
  7. - source: salt://web/files/apache-conf.d
  8.  
  9. apache-auth:
  10. pkg.installed:
  11. - name: httpd-tools
  12. - require_in:
  13. - cmd: apache-auth #如果没有这个rpm包下面的cmd.run就不运行,指定依赖关系
  14. cmd.run:
  15. - name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin #创建 账号为admin 密码为admin的密码文件

执行状态模块,验证没有报错

  1. [root@linux-node1 web]# salt "*" state.highstate
  2. linux-node1.example.com:
  3. ----------
  4. ID: lamp-install
  5. Function: pkg.installed
  6. Result: True
  7. Comment: All specified packages are already installed.
  8. Started: ::36.171081
  9. Duration: 760.101 ms
  10. Changes:
  11. ----------
  12. ID: apache-config
  13. Function: file.managed
  14. Name: /etc/httpd/conf/httpd.conf
  15. Result: True
  16. Comment: File /etc/httpd/conf/httpd.conf is in the correct state
  17. Started: ::36.936510
  18. Duration: 12.034 ms
  19. Changes:
  20. ----------
  21. ID: php-config
  22. Function: file.managed
  23. Name: /etc/php.ini
  24. Result: True
  25. Comment: File /etc/php.ini is in the correct state
  26. Started: ::36.948778
  27. Duration: 5.661 ms
  28. Changes:
  29. ----------
  30. ID: lamp-service
  31. Function: service.running
  32. Name: httpd
  33. Result: True
  34. Comment: Service httpd is already enabled, and is in the desired state
  35. Started: ::36.955341
  36. Duration: 335.213 ms
  37. Changes:
  38. ----------
  39. ID: apache-conf
  40. Function: file.recurse
  41. Name: /etc/httpd/conf.d
  42. Result: True
  43. Comment: The directory /etc/httpd/conf.d is in the correct state
  44. Started: ::37.290804
  45. Duration: 26.826 ms
  46. Changes:
  47. ----------
  48. ID: apache-auth
  49. Function: pkg.installed
  50. Name: httpd-tools
  51. Result: True
  52. Comment: Package httpd-tools is already installed.
  53. Started: ::37.317767
  54. Duration: 0.518 ms
  55. Changes:
  56. ----------
  57. ID: apache-auth
  58. Function: cmd.run
  59. Name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
  60. Result: True
  61. Comment: Command "htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin" run
  62. Started: ::37.319460
  63. Duration: 17.893 ms
  64. Changes:
  65. ----------
  66. pid:
  67.  
  68. retcode:
  69.  
  70. stderr:
  71. Adding password for user admin
  72. stdout:
  73.  
  74. Summary
  75. ------------
  76. Succeeded: (changed=)
  77. Failed:
  78. ------------
  79. Total states run:
  80. linux-node2.example.com:
  81. ----------
  82. ID: lamp-install
  83. Function: pkg.installed
  84. Result: True
  85. Comment: All specified packages are already installed.
  86. Started: ::36.819001
  87. Duration: 801.418 ms
  88. Changes:
  89. ----------
  90. ID: apache-config
  91. Function: file.managed
  92. Name: /etc/httpd/conf/httpd.conf
  93. Result: True
  94. Comment: File /etc/httpd/conf/httpd.conf is in the correct state
  95. Started: ::37.625380
  96. Duration: 5.27 ms
  97. Changes:
  98. ----------
  99. ID: php-config
  100. Function: file.managed
  101. Name: /etc/php.ini
  102. Result: True
  103. Comment: File /etc/php.ini is in the correct state
  104. Started: ::37.630775
  105. Duration: 5.974 ms
  106. Changes:
  107. ----------
  108. ID: lamp-service
  109. Function: service.running
  110. Name: httpd
  111. Result: True
  112. Comment: Service httpd is already enabled, and is in the desired state
  113. Started: ::37.637798
  114. Duration: 276.924 ms
  115. Changes:
  116. ----------
  117. ID: apache-conf
  118. Function: file.recurse
  119. Name: /etc/httpd/conf.d
  120. Result: True
  121. Comment: The directory /etc/httpd/conf.d is in the correct state
  122. Started: ::37.914890
  123. Duration: 114.468 ms
  124. Changes:
  125. ----------
  126. ID: apache-auth
  127. Function: pkg.installed
  128. Name: httpd-tools
  129. Result: True
  130. Comment: Package httpd-tools is already installed.
  131. Started: ::38.029465
  132. Duration: 0.858 ms
  133. Changes:
  134. ----------
  135. ID: apache-auth
  136. Function: cmd.run
  137. Name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
  138. Result: True
  139. Comment: Command "htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin" run
  140. Started: ::38.031904
  141. Duration: 24.688 ms
  142. Changes:
  143. ----------
  144. pid:
  145.  
  146. retcode:
  147.  
  148. stderr:
  149. Adding password for user admin
  150. stdout:
  151.  
  152. Summary
  153. ------------
  154. Succeeded: (changed=)
  155. Failed:
  156. ------------
  157. Total states run:

再次访问phpinfo就有验证了

但是上面有些小问题

当多次执行apache-auth状态模块的时候,密码文件会被重新创建并覆盖

解决办法:我们可以使用unless进行判断,unless 如果条件为真就执行,为假就不执行

修改apache-auth状态模块如下

  1. [root@linux-node1 web]# tail - lamp.sls
  2. apache-auth:
  3. pkg.installed:
  4. - name: httpd-tools
  5. - require_in:
  6. - cmd: apache-auth #如果没有这个rpm包下面的cmd.run就不运行,解决依赖关系
  7. cmd.run:
  8. - name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
  9. - unless: test -f /etc/httpd/conf/htpasswd_file #unless 不只限于用test 支持脚本等任何命令 只要判断 期返回结果0 或者1 0为真 1为假 就可以
再次执行lamp.sls状态模块,就解决了上面的问题
  1. [root@linux-node1 web]# salt "*" state.highstate
  2. linux-node2.example.com:
  3. ----------
  4. ID: lamp-install
  5. Function: pkg.installed
  6. Result: True
  7. Comment: All specified packages are already installed.
  8. Started: ::08.415429
  9. Duration: 739.009 ms
  10. Changes:
  11. ----------
  12. ID: apache-config
  13. Function: file.managed
  14. Name: /etc/httpd/conf/httpd.conf
  15. Result: True
  16. Comment: File /etc/httpd/conf/httpd.conf is in the correct state
  17. Started: ::09.158151
  18. Duration: 4.038 ms
  19. Changes:
  20. ----------
  21. ID: php-config
  22. Function: file.managed
  23. Name: /etc/php.ini
  24. Result: True
  25. Comment: File /etc/php.ini is in the correct state
  26. Started: ::09.162303
  27. Duration: 4.511 ms
  28. Changes:
  29. ----------
  30. ID: lamp-service
  31. Function: service.running
  32. Name: httpd
  33. Result: True
  34. Comment: Service httpd is already enabled, and is in the desired state
  35. Started: ::09.167489
  36. Duration: 260.979 ms
  37. Changes:
  38. ----------
  39. ID: apache-conf
  40. Function: file.recurse
  41. Name: /etc/httpd/conf.d
  42. Result: True
  43. Comment: The directory /etc/httpd/conf.d is in the correct state
  44. Started: ::09.428715
  45. Duration: 27.714 ms
  46. Changes:
  47. ----------
  48. ID: apache-auth
  49. Function: pkg.installed
  50. Name: httpd-tools
  51. Result: True
  52. Comment: Package httpd-tools is already installed.
  53. Started: ::09.456576
  54. Duration: 0.529 ms
  55. Changes:
  56. ----------
  57. ID: apache-auth
  58. Function: cmd.run
  59. Name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
  60. Result: True
  61. Comment: unless execution succeeded
  62. Started: ::09.458220
  63. Duration: 7.17 ms
  64. Changes:
  65.  
  66. Summary
  67. ------------
  68. Succeeded:
  69. Failed:
  70. ------------
  71. Total states run:
  72. linux-node1.example.com:
  73. ----------
  74. ID: lamp-install
  75. Function: pkg.installed
  76. Result: True
  77. Comment: All specified packages are already installed.
  78. Started: ::08.069936
  79. Duration: 769.874 ms
  80. Changes:
  81. ----------
  82. ID: apache-config
  83. Function: file.managed
  84. Name: /etc/httpd/conf/httpd.conf
  85. Result: True
  86. Comment: File /etc/httpd/conf/httpd.conf is in the correct state
  87. Started: ::08.842186
  88. Duration: 4.2 ms
  89. Changes:
  90. ----------
  91. ID: php-config
  92. Function: file.managed
  93. Name: /etc/php.ini
  94. Result: True
  95. Comment: File /etc/php.ini is in the correct state
  96. Started: ::08.846533
  97. Duration: 4.393 ms
  98. Changes:
  99. ----------
  100. ID: lamp-service
  101. Function: service.running
  102. Name: httpd
  103. Result: True
  104. Comment: Service httpd is already enabled, and is in the desired state
  105. Started: ::08.851964
  106. Duration: 244.197 ms
  107. Changes:
  108. ----------
  109. ID: apache-conf
  110. Function: file.recurse
  111. Name: /etc/httpd/conf.d
  112. Result: True
  113. Comment: The directory /etc/httpd/conf.d is in the correct state
  114. Started: ::09.096343
  115. Duration: 20.85 ms
  116. Changes:
  117. ----------
  118. ID: apache-auth
  119. Function: pkg.installed
  120. Name: httpd-tools
  121. Result: True
  122. Comment: Package httpd-tools is already installed.
  123. Started: ::09.117331
  124. Duration: 0.53 ms
  125. Changes:
  126. ----------
  127. ID: apache-auth
  128. Function: cmd.run
  129. Name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
  130. Result: True
  131. Comment: unless execution succeeded
  132. Started: ::09.119400
  133. Duration: 6.484 ms
  134. Changes:
  135.  
  136. Summary
  137. ------------
  138. Succeeded:
  139. Failed:
  140. ------------
  141. Total states run:
base的目录结构如下
  1. [root@linux-node1 salt]# tree base
  2. base
  3. ├── fileappend.sls
  4. ├── top.sls
  5. └── web
  6. ├── apache.sls
  7. ├── files
  8.    ├── apache-conf.d
  9.       ├── autoindex.conf
  10.       ├── php.conf
  11.       ├── README
  12.       ├── userdir.conf
  13.       └── welcome.conf
  14.    ├── httpd.conf
  15.    └── php.ini
  16. └── lamp.sls
  17.  
  18. directories, files
cat lamp.sls
  1. [root@linux-node1 base]# cat web/lamp.sls
  2. lamp-install:
  3. pkg.installed:
  4. - pkgs:
  5. - httpd
  6. - php
  7. - php-pdo
  8. - php-mysql
  9.  
  10. apache-config:
  11. file.managed:
  12. - name: /etc/httpd/conf/httpd.conf
  13. - source: salt://web/files/httpd.conf
  14. - user: root
  15. - group: root
  16. - mode:
  17. - require:
  18. - pkg: lamp-install
  19.  
  20. php-config:
  21. file.managed:
  22. - name: /etc/php.ini
  23. - source: salt://web/files/php.ini
  24. - user: root
  25. - group: root
  26. - mode:
  27.  
  28. lamp-service:
  29. service.running:
  30. - name: httpd
  31. - enable: True
  32. - reload: True
  33. - watch:
  34. - file: apache-config
  35.  
  36. apache-conf:
  37. file.recurse:
  38. - name: /etc/httpd/conf.d
  39. - source: salt://web/files/apache-conf.d
  40.  
  41. apache-auth:
  42. pkg.installed:
  43. - name: httpd-tools
  44. - require_in:
  45. - cmd: apache-auth #如果没有这个rpm包下面的cmd.run就不运行,解决依赖关系
  46. cmd.run:
  47. - name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
  48. - unless: test -f /etc/httpd/conf/htpasswd_file
至此apache和php测试已完成
这里补充一个file模块的append方法

[root@linux-node1 base]# pwd
/srv/salt/base
[root@linux-node1 base]# cat fileappend.sls
/etc/profile:  #这里是ID的另一种用法,可以直接用文件的路径
file.append:
- text:
- "#xiewneming test" #注意如果添加的内容里面有特殊符合,需要加上引号

执行状态模块,添加成功
  1. [root@linux-node1 base]# salt "linux-node1*" state.sls fileappend
  2. linux-node1.example.com:
  3. ----------
  4. ID: /etc/profile
  5. Function: file.append
  6. Result: True
  7. Comment: Appended lines
  8. Started: ::02.877027
  9. Duration: 7.669 ms
  10. Changes:
  11. ----------
  12. diff:
  13. ---
  14. +++
  15. @@ -, +, @@
  16. unset i
  17. unset -f pathmunge
  18. None
  19. +#xiewneming test
  20.  
  21. Summary
  22. ------------
  23. Succeeded: (changed=)
  24. Failed:
  25. ------------
  26. Total states run:

总结

1.添加状态模块的时候,应该自己得先在别的机器上面跑一遍流程在添加
2.状态模块中- source: salt://web/files/apache-conf.d #支持ftp或者http上面 base环境的路径
3.使用的时候要先测试 test=True
4.ID 有不同表示方式
5.状态模块需要执行模块执行才能生效
6.相同的业务用目录进行分类管理
7.sls文件是从上往下按照顺序执行
8.一个ID下面一个状态只能出现一次
9.pkg是虚拟的包管理,在不同系统下面包的安装命令不同 比如 CentOS 和Ubuntu
10.watch 和watch_in require和require_in 在状态模块中可以统一用其中的一种
11.salt默认是有缓存的,可以无关紧要的配置重启进程刷新缓存
      salt任务执行过程中不删除缓存目录里面的文件 缓存目录默认为/var/cache/salt


附 赵班长的 GitHub saltbook-code网址

https://github.com/unixhot/saltbook-code/tree/master

SaltStack部署服务及配置管理apache+php-第二篇的更多相关文章

  1. Autofac 组件、服务、自动装配 《第二篇》

    一.组件 创建出来的对象需要从组件中来获取,组件的创建有如下4种(延续第一篇的Demo,仅仅变动所贴出的代码)方式: 1.类型创建RegisterType AutoFac能够通过反射检查一个类型,选择 ...

  2. Apache nifi 第二篇(小白初试) nifi数据对接流程初次尝试

     一.准备工作 1.官网下载nifi 2.上传到linux随便哪里把,因为nifi是用java写的,所以首先要保证你的linux装了jdk 其次保证系统在装了zookeeper,因为nifi是一个分布 ...

  3. 使用docker-compose 大杀器来部署服务 上

    使用docker-compose 大杀器来部署服务 上 我们都听过或者用过 docker,然而使用方式却是仅仅用手动的方式,这样去操作 docker 还是很原始. 好吧,可能在小白的眼中噼里啪啦的对着 ...

  4. salt-stack部署

    saltstack部署   环境准备 [root@server elasticsearch]# cat /etc/redhat-release CentOS release 6.6 (Final)[r ...

  5. 使用docker-compose 大杀器来部署服务 上(转)

    使用docker-compose 大杀器来部署服务 上 我们都听过或者用过 docker,然而使用方式却是仅仅用手动的方式,这样去操作 docker 还是很原始. 好吧,可能在小白的眼中噼里啪啦的对着 ...

  6. redis安装,windows,linux版本并部署服务

    一.使用场景         项目中采用数据库访问量过大或访问过于频繁,将会对数据库带来很大的压力.redis数据库是以非关系数据库的出现,后来redis的迭代版本支持了缓存数据.登录session状 ...

  7. [转]使用docker-compose 大杀器来部署服务 上

    本文转自:https://www.cnblogs.com/neptunemoon/p/6512121.html 使用docker-compose 大杀器来部署服务 上 我们都听过或者用过 docker ...

  8. docker swarm英文文档学习-8-在集群中部署服务

    Deploy services to a swarm在集群中部署服务 集群服务使用声明式模型,这意味着你需要定义服务的所需状态,并依赖Docker来维护该状态.该状态包括以下信息(但不限于): 应该运 ...

  9. 使用docker-compose 大杀器来部署服务

    使用docker-compose 大杀器来部署服务 上 我们都听过或者用过 docker,然而使用方式却是仅仅用手动的方式,这样去操作 docker 还是很原始. 好吧,可能在小白的眼中噼里啪啦的对着 ...

随机推荐

  1. HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. 转!!mybatis xml 传值 if test判断

    当mapper方法传参数 为 String时,且xml中药进行参数比较 比如 是不是等于1 或者等于2 方式1. 方式2. 转自:https://blog.csdn.net/chenaini119/a ...

  3. git学习------>如何修改git已提交的记录中的Author和Email?

    一.背景 最近搭建好GitLab后,准备陆陆续续的将之前在SVN仓库中保存的代码迁移到GitLab上,昨天顺利将三个Android组件的代码迁移到GitLab后,其他同事发现迁移是成功了,但是pull ...

  4. jupter nootbok 快捷键、NumPy模块、Pandas模块初识

    jupter nootbok 快捷键 插入cell:a b 删除cell:x cell模式的切换:m:Markdown模式 y:code模式 运行cell:shift+enter tab:补全 shi ...

  5. python 字符串的格式化

    python字符串的格式化分为两种:1)% 方式    2)str.format()  方式. str.format() 是比 % 较新的方式, 大多数的 Python 代码仍然使用 % 操作符.但最 ...

  6. Oracle中to_number()函数的用法

    to_number()函数是oracle中常用的类型转换函数之一,是将一些处理过的按一定格式编排过的字符串变回数值型的格式. 1.to_number()函数可以将char或varchar2类型的str ...

  7. java 多线程 day14 Semaphore 线程信号灯

    import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.c ...

  8. 记录:正确率、召回率、F值

    因为不理解召回率,所以去查看了一些资料.特此记录一下自己的理解,以便以后查看. 说明 正确率=查出来正确的样本数/全部查出来的样本数 (也可以理解为查准率) 召回率=查出来正确的样本数/数据集里全部正 ...

  9. 设置 Quick-Cocos2d-x 在 Windows 下的编译环境

    http://cn.cocos2d-x.org/tutorial/show?id=1304 设置 Quick-Cocos2d-x 在 Windows 下的编译环境 Liao Yulei2014-08- ...

  10. centos7命令1

    ls  查看当前路径下的文件或文件夹 pwd 查看当前路径,例如/home/python   表示根目录下的home文件夹下的python文件夹 clear清空屏幕 /斜杠 \反斜杠 |竖杠 _下划线 ...