SaltStack部署服務及配置管理apache+php-第二篇

實驗目標

1.使用SaltStack部署apache和php,php

2.使用salt管理httpd.conf配置文件配置訪問info.php使用帳戶密碼css

3.在salt裏面增長對conf.d目錄進行配置管理html

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

5.學會如何使用 watch require unlessmysql

實現步驟

修改master的配置文件,指定base環境路徑,base環境是必須指定的
[root@linux-node1 base]# grep  -9  ^file_roots /etc/salt/master  |grep -v ^#
file_roots:
  base:
    - /srv/salt/base
  dev:
    - /srv/salt/dev
  test:
    - /srv/salt/test
  prod:
    - /srv/salt/prod
建立目錄
[root@linux-node1 base]# mkdir -p /srv/salt/{base,dev,test,prod}
[root@linux-node1 base]# tree /srv/salt/
/srv/salt/
├── base
├── dev
├── prod
└── test
重啓master
[root@linux-node1 base]# systemctl restart salt-master
在base目錄下面建立一個web目錄用於存放web相關的sls文件
[root@linux-node1 base]# mkdir -p web
cd到bash/web目錄裏面建立apache.sls文件
[root@linux-node1 base]# cd web/
[root@linux-node1 web]# cat apache.sls 
apache-install:   #id 名字本身取 須要形象一點, 一個id下面一個狀態只能出現一次
  pkg.installed:  #pkg 是狀態模塊,installed 是模塊裏面的方法
    - name: httpd #方法裏面的參數
apache-service:
  service.running:
    - name: httpd
    - enable: True #設置開機自動啓動
#yaml裏面格式有嚴格的要求,註釋用#號,不能有table,- 兩邊須要空格,縮進用2個空格層級關係後面要加分號
 執行狀態模塊部署服務
[root@linux-node1 base]# salt "linux-node2*" state.sls apache
linux-node2.example.com:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 14:58:09.228934
    Duration: 633.681 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is running
     Started: 14:58:09.863302
    Duration: 310.567 ms
     Changes:   
              ----------
              httpd:
                  True

Summary
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
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-masterlinux

在bese環境目錄下面添加top.sls
[root@linux-node1 base]# more top.sls 
base:
  'linux-node2.example.com':
    - web.apache
  'linux-node1.example.com':
    - web.apache
[root@linux-node1 base]# pwd
/srv/salt/base
執行高級模塊方法,高級方法到 base下面找top.sls  文件編排告訴每一個minion須要幹什麼,通常生產環境用高級狀態多些
[root@linux-node1 base]#   salt "*" state.highstate 
linux-node1.example.com:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 15:23:08.597951
    Duration: 709.521 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 15:23:09.308417
    Duration: 233.623 ms
     Changes:   

Summary
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
linux-node2.example.com:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 15:23:09.171596
    Duration: 721.901 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 15:23:09.894209
    Duration: 221.615 ms
     Changes:   

Summary
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
 上面咱們使用了2個狀態模塊pkg和service,下面咱們使用file文件配置模塊 

模塊使用參考文檔git

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-mysqlgithub

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: 644web

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

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/

執行狀態模塊部署服務
[root@linux-node1 web]# salt "*" state.sls web.lamp
linux-node1.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 15:43:56.883540
    Duration: 633.814 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 15:43:57.520199
    Duration: 4.242 ms
     Changes:   
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 15:43:57.524589
    Duration: 4.149 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 15:43:57.529404
    Duration: 258.952 ms
     Changes:   

Summary
------------
Succeeded: 4
Failed:    0
------------
Total states run:     4
linux-node2.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 15:43:58.566172
    Duration: 611.409 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 15:43:59.180091
    Duration: 4.063 ms
     Changes:   
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 15:43:59.184248
    Duration: 3.803 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 15:43:59.188496
    Duration: 208.1 ms
     Changes:   

Summary
------------
Succeeded: 4
Failed:    0
------------
Total states run:     4
 使用file模塊下面的recurse方法進行apache的conf.d目錄管理配置以下
apache-conf:
  file.recurse:
    - name: /etc/httpd/conf.d
    - source: salt://web/files/apache-conf.d
建立salt源目錄,並拷貝數據導源文件目錄,數據文件來源根據本身業務的實際狀況
[root@linux-node1 ~]# mkdir /srv/salt/base/web/files/apache-conf.d
[root@linux-node1 ~]# cd /srv/salt/base/web/files/apache-conf.d/
[root@linux-node1 apache-conf.d]# cp -a /etc/httpd/conf.d/* .
[root@linux-node1 apache-conf.d]# ls
autoindex.conf php.conf README userdir.conf welcome.conf
[root@linux-node1 apache-conf.d]#

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

[root@linux-node1 files]# echo "#xieweming test" >> apache-conf.d/welcome.conf 
驗證目錄管理是否生效

能夠先使用test=True 只作測試,不會在minion節點上面真正執行,確認無問題後再讓minion去執行

[root@linux-node1 files]# salt "linux-node2*" state.highstate test=True
linux-node2.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 16:12:56.440265
    Duration: 666.288 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: The file /etc/httpd/conf/httpd.conf is in the correct state
     Started: 16:12:57.108448
    Duration: 3.959 ms
     Changes:   
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: The file /etc/php.ini is in the correct state
     Started: 16:12:57.112503
    Duration: 3.61 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 16:12:57.116505
    Duration: 244.585 ms
     Changes:   
----------
          ID: apache-conf
    Function: file.recurse
        Name: /etc/httpd/conf.d
      Result: None
     Comment: #### /etc/httpd/conf.d/welcome.conf ####
              The file /etc/httpd/conf.d/welcome.conf is set to be changed
     Started: 16:12:57.361390
    Duration: 1096.52 ms
     Changes:   
              ----------
              /etc/httpd/conf.d/welcome.conf:
                  ----------
                  diff:
                      --- 
                      +++ 
                      @@ -20,3 +20,4 @@
                       Alias /noindex/css/open-sans.css /usr/share/httpd/noindex/css/open-sans.css
                       Alias /images/apache_pb.gif /usr/share/httpd/noindex/images/apache_pb.gif
                       Alias /images/poweredby.png /usr/share/httpd/noindex/images/poweredby.png
                      +#xieweming test

Summary
------------
Succeeded: 5 (unchanged=1, changed=1)
Failed:    0
------------
Total states run:     5
驗證沒有問題在node1和node2上面都執行

咱們這裏就2臺全部能夠直接用*

[root@linux-node1 files]# salt "*" state.highstate 
linux-node1.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 16:19:31.724191
    Duration: 782.903 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 16:19:32.509438
    Duration: 4.62 ms
     Changes:   
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 16:19:32.514200
    Duration: 4.418 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 16:19:32.519273
    Duration: 234.566 ms
     Changes:   
----------
          ID: apache-conf
    Function: file.recurse
        Name: /etc/httpd/conf.d
      Result: True
     Comment: Recursively updated /etc/httpd/conf.d
     Started: 16:19:32.754002
    Duration: 1082.389 ms
     Changes:   
              ----------
              /etc/httpd/conf.d/welcome.conf:
                  ----------
                  diff:
                      --- 
                      +++ 
                      @@ -20,3 +20,4 @@
                       Alias /noindex/css/open-sans.css /usr/share/httpd/noindex/css/open-sans.css
                       Alias /images/apache_pb.gif /usr/share/httpd/noindex/images/apache_pb.gif
                       Alias /images/poweredby.png /usr/share/httpd/noindex/images/poweredby.png
                      +#xieweming test

Summary
------------
Succeeded: 5 (changed=1)
Failed:    0
------------
Total states run:     5
linux-node2.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 16:19:32.296865
    Duration: 789.23 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 16:19:33.089019
    Duration: 3.807 ms
     Changes:   
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 16:19:33.092933
    Duration: 3.459 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 16:19:33.096823
    Duration: 232.349 ms
     Changes:   
----------
          ID: apache-conf
    Function: file.recurse
        Name: /etc/httpd/conf.d
      Result: True
     Comment: Recursively updated /etc/httpd/conf.d
     Started: 16:19:33.329410
    Duration: 1079.801 ms
     Changes:   
              ----------
              /etc/httpd/conf.d/welcome.conf:
                  ----------
                  diff:
                      --- 
                      +++ 
                      @@ -20,3 +20,4 @@
                       Alias /noindex/css/open-sans.css /usr/share/httpd/noindex/css/open-sans.css
                       Alias /images/apache_pb.gif /usr/share/httpd/noindex/images/apache_pb.gif
                       Alias /images/poweredby.png /usr/share/httpd/noindex/images/poweredby.png
                      +#xieweming test

Summary
------------
Succeeded: 5 (changed=1)
Failed:    0
------------
Total states run:     5
salt "*" state.highstate
使用watch在apache配置文件發送變化時,從新加載apache配置

增長下面的紅色字體部分

[root@linux-node1 web]# more 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
    - 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
    - reload: True  #若是不加reload 默認會重啓服務
    - watch: #增長
       - file: apache-config #監控上面的apache-config ID 因此說 一個ID在一個狀態只能出現一次
apache-conf:
  file.recurse:
    - name: /etc/httpd/conf.d
    - source: salt://web/files/apache-conf.d

 另一種watc_in寫法,咱們只須要掌握一種就能夠

...
lamp-service:
  service.running:
    - name: httpd
    - enable: True
    - reload: True
    - watch:
      - file: apache-config
apache-conf:
  file.recurse:
    - name: /etc/httpd/conf.d
    - source: salt://web/files/apache-conf.d
    - watch_in:
      - service: lamp-service
...
View Code

修改一下配置文件進行驗證成功

[root@linux-node1 files]# salt "*" state.highstate
linux-node1.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 16:39:08.336027
    Duration: 733.712 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf updated
     Started: 16:39:09.071795
    Duration: 13.576 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -28,6 +28,7 @@
                   # same ServerRoot for multiple httpd daemons, you will need to change at
                   # least PidFile.
                   #
                  +
                   ServerRoot "/etc/httpd"
                   
                   #
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 16:39:09.085478
    Duration: 3.597 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service reloaded
     Started: 16:39:09.337223
    Duration: 253.101 ms
     Changes:   
              ----------
              httpd:
                  True
----------
          ID: apache-conf
    Function: file.recurse
        Name: /etc/httpd/conf.d
      Result: True
     Comment: The directory /etc/httpd/conf.d is in the correct state
     Started: 16:39:09.590622
    Duration: 25.654 ms
     Changes:   

Summary
------------
Succeeded: 5 (changed=2)
Failed:    0
------------
Total states run:     5
linux-node2.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 16:39:08.904921
    Duration: 735.305 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf updated
     Started: 16:39:09.643019
    Duration: 16.038 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -28,6 +28,7 @@
                   # same ServerRoot for multiple httpd daemons, you will need to change at
                   # least PidFile.
                   #
                  +
                   ServerRoot "/etc/httpd"
                   
                   #
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 16:39:09.659260
    Duration: 3.724 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service reloaded
     Started: 16:39:09.900780
    Duration: 255.082 ms
     Changes:   
              ----------
              httpd:
                  True
----------
          ID: apache-conf
    Function: file.recurse
        Name: /etc/httpd/conf.d
      Result: True
     Comment: The directory /etc/httpd/conf.d is in the correct state
     Started: 16:39:10.156119
    Duration: 165.767 ms
     Changes:   

Summary
------------
Succeeded: 5 (changed=2)
Failed:    0
------------
Total states run:     5
View Code
 使用require可讓各ID之間產生依賴關係,避免無效執行

好比執行apache-config ID以前要確保  lamp-install ID已經成功的完成了  添加下面紅色字體部分

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
    - user: root
    - group: root
    - mode: 644
    - require:
      - pkg: lamp-install
測試apache php環境工做是否正常,在2個節點上的apache工做目錄下建立phpinfo文件
[root@linux-node2 conf.d]# cd /var/www/html/
[root@linux-node2 html]# mkdir admin
[root@linux-node2 html]# cd admin/
[root@linux-node2 admin]# vi info.php
[root@linux-node2 admin]# cat info.php 
<?php
phpinfo()
?>

能夠正常打開

如今使用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指定依賴關係
[root@linux-node1 web]# pwd
/srv/salt/base/web
[root@linux-node1 web]# ls
apache.sls  files  lamp.sls
[root@linux-node1 web]# tail -10 lamp.sls 
    - name: /etc/httpd/conf.d
    - source: salt://web/files/apache-conf.d

apache-auth:
  pkg.installed:
    - name: httpd-tools
    - require_in:
      - cmd: apache-auth   #若是沒有這個rpm包下面的cmd.run就不運行,指定依賴關係
  cmd.run:
    - name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin #建立 帳號爲admin 密碼爲admin的密碼文件

執行狀態模塊,驗證沒有報錯

[root@linux-node1 web]# salt "*" state.highstate
linux-node1.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 17:26:36.171081
    Duration: 760.101 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 17:26:36.936510
    Duration: 12.034 ms
     Changes:   
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 17:26:36.948778
    Duration: 5.661 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 17:26:36.955341
    Duration: 335.213 ms
     Changes:   
----------
          ID: apache-conf
    Function: file.recurse
        Name: /etc/httpd/conf.d
      Result: True
     Comment: The directory /etc/httpd/conf.d is in the correct state
     Started: 17:26:37.290804
    Duration: 26.826 ms
     Changes:   
----------
          ID: apache-auth
    Function: pkg.installed
        Name: httpd-tools
      Result: True
     Comment: Package httpd-tools is already installed.
     Started: 17:26:37.317767
    Duration: 0.518 ms
     Changes:   
----------
          ID: apache-auth
    Function: cmd.run
        Name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
      Result: True
     Comment: Command "htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin" run
     Started: 17:26:37.319460
    Duration: 17.893 ms
     Changes:   
              ----------
              pid:
                  20315
              retcode:
                  0
              stderr:
                  Adding password for user admin
              stdout:

Summary
------------
Succeeded: 7 (changed=1)
Failed:    0
------------
Total states run:     7
linux-node2.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 17:26:36.819001
    Duration: 801.418 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 17:26:37.625380
    Duration: 5.27 ms
     Changes:   
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 17:26:37.630775
    Duration: 5.974 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 17:26:37.637798
    Duration: 276.924 ms
     Changes:   
----------
          ID: apache-conf
    Function: file.recurse
        Name: /etc/httpd/conf.d
      Result: True
     Comment: The directory /etc/httpd/conf.d is in the correct state
     Started: 17:26:37.914890
    Duration: 114.468 ms
     Changes:   
----------
          ID: apache-auth
    Function: pkg.installed
        Name: httpd-tools
      Result: True
     Comment: Package httpd-tools is already installed.
     Started: 17:26:38.029465
    Duration: 0.858 ms
     Changes:   
----------
          ID: apache-auth
    Function: cmd.run
        Name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
      Result: True
     Comment: Command "htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin" run
     Started: 17:26:38.031904
    Duration: 24.688 ms
     Changes:   
              ----------
              pid:
                  8667
              retcode:
                  0
              stderr:
                  Adding password for user admin
              stdout:

Summary
------------
Succeeded: 7 (changed=1)
Failed:    0
------------
Total states run:     7
View Code

再次訪問phpinfo就有驗證了 

 

 可是上面有些小問題

當屢次執行apache-auth狀態模塊的時候,密碼文件會被從新建立並覆蓋

解決辦法:咱們可使用unless進行判斷,unless 若是條件爲真就執行,爲假就不執行

修改apache-auth狀態模塊以下

[root@linux-node1 web]# tail -8 lamp.sls 
apache-auth:
  pkg.installed:
    - name: httpd-tools
    - require_in:
      - cmd: apache-auth   #若是沒有這個rpm包下面的cmd.run就不運行,解決依賴關係
  cmd.run:
    - name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
    - unless: test -f /etc/httpd/conf/htpasswd_file #unless 不僅限於用test 支持腳本等任何命令 只要判斷 期返回結果0 或者1 0爲真 1爲假 就能夠
再次執行lamp.sls狀態模塊,就解決了上面的問題
[root@linux-node1 web]# salt "*" state.highstate
linux-node2.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 17:41:08.415429
    Duration: 739.009 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 17:41:09.158151
    Duration: 4.038 ms
     Changes:   
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 17:41:09.162303
    Duration: 4.511 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 17:41:09.167489
    Duration: 260.979 ms
     Changes:   
----------
          ID: apache-conf
    Function: file.recurse
        Name: /etc/httpd/conf.d
      Result: True
     Comment: The directory /etc/httpd/conf.d is in the correct state
     Started: 17:41:09.428715
    Duration: 27.714 ms
     Changes:   
----------
          ID: apache-auth
    Function: pkg.installed
        Name: httpd-tools
      Result: True
     Comment: Package httpd-tools is already installed.
     Started: 17:41:09.456576
    Duration: 0.529 ms
     Changes:   
----------
          ID: apache-auth
    Function: cmd.run
        Name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
      Result: True
     Comment: unless execution succeeded
     Started: 17:41:09.458220
    Duration: 7.17 ms
     Changes:   

Summary
------------
Succeeded: 7
Failed:    0
------------
Total states run:     7
linux-node1.example.com:
----------
          ID: lamp-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 17:41:08.069936
    Duration: 769.874 ms
     Changes:   
----------
          ID: apache-config
    Function: file.managed
        Name: /etc/httpd/conf/httpd.conf
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 17:41:08.842186
    Duration: 4.2 ms
     Changes:   
----------
          ID: php-config
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 17:41:08.846533
    Duration: 4.393 ms
     Changes:   
----------
          ID: lamp-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is in the desired state
     Started: 17:41:08.851964
    Duration: 244.197 ms
     Changes:   
----------
          ID: apache-conf
    Function: file.recurse
        Name: /etc/httpd/conf.d
      Result: True
     Comment: The directory /etc/httpd/conf.d is in the correct state
     Started: 17:41:09.096343
    Duration: 20.85 ms
     Changes:   
----------
          ID: apache-auth
    Function: pkg.installed
        Name: httpd-tools
      Result: True
     Comment: Package httpd-tools is already installed.
     Started: 17:41:09.117331
    Duration: 0.53 ms
     Changes:   
----------
          ID: apache-auth
    Function: cmd.run
        Name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
      Result: True
     Comment: unless execution succeeded
     Started: 17:41:09.119400
    Duration: 6.484 ms
     Changes:   

Summary
------------
Succeeded: 7
Failed:    0
------------
Total states run:     7
View Code
base的目錄結構以下
[root@linux-node1 salt]# tree  base
base
├── fileappend.sls
├── top.sls
└── web
    ├── apache.sls
    ├── files
    │   ├── apache-conf.d
    │   │   ├── autoindex.conf
    │   │   ├── php.conf
    │   │   ├── README
    │   │   ├── userdir.conf
    │   │   └── welcome.conf
    │   ├── httpd.conf
    │   └── php.ini
    └── lamp.sls

3 directories, 11 files
cat lamp.sls
[root@linux-node1 base]# cat web/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
    - user: root
    - group: root
    - mode: 644
    - require:
      - pkg: lamp-install

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
    - reload: True
    - watch:
       - file: apache-config

apache-conf:
  file.recurse:
    - name: /etc/httpd/conf.d
    - source: salt://web/files/apache-conf.d

apache-auth:
  pkg.installed:
    - name: httpd-tools
    - require_in:
      - cmd: apache-auth   #若是沒有這個rpm包下面的cmd.run就不運行,解決依賴關係
  cmd.run:
    - name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
    - unless: test -f /etc/httpd/conf/htpasswd_file
View Code
至此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" #注意若是添加的內容裏面有特殊符合,須要加上引號

執行狀態模塊,添加成功
[root@linux-node1 base]# salt "linux-node1*" state.sls fileappend
linux-node1.example.com:
----------
          ID: /etc/profile
    Function: file.append
      Result: True
     Comment: Appended 1 lines
     Started: 17:53:02.877027
    Duration: 7.669 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -75,3 +75,4 @@
                   unset i
                   unset -f pathmunge
                   None
                  +#xiewneming test

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

總結

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

相關文章
相關標籤/搜索