天天看點

salt實作lamp自動化部署

三個檔案夾:

pkg 安裝軟體包

file  管理配置檔案

service   服務管理

思路: lamp需要安裝的軟體包有  httpd, php, mysql, mysql-server, php-mysql, php-pdo

cd  /srv/salt/dev/

mkdir   /srv/salt/dev/files/      ##放配置檔案

vim  /srv/salt/dev/lamp.sls 

lamp配置檔案如下:

lamp-pkg-install:

  pkg.installed:

    - names:

      - php

      - mysql

      - php-cli

      - php-common

      - php-mysql

      - php-pdo

apache-service:

    - name: httpd

  file.managed:

    - name: /etc/httpd/conf/httpd.conf

    - source: salt://files/httpd.conf

    - user: root

    - group: root

    - mode: 644

    - require:         

      - pkg: apache-service

  service.running:

    - enable: True

    - reload: True

    - watch:                      

      - file: apache-service

mysql-service:

    - name: mysql-server

    - require_in:    

      - file: mysql-service

    - name: /etc/my.cnf

    - source: salt://files/my.cnf

    - watch_in:    

      - service: mysql-service

    - name: mysqld

vim  /srv/salt/top.sls    

dev:

  'web2.coohx.com':

    - lamp

執行: 

 salt 'web2*' state.highstate

[root@web1 salt]# salt 'web2*' state.highstate

web2.coohx.com:

....

...

----------

          ID: apache-service

    Function: file.managed

        Name: /etc/httpd/conf/httpd.conf

      Result: True

     Comment: File /etc/httpd/conf/httpd.conf updated

     Started: 19:22:16.063742

    Duration: 56.123 ms

     Changes:

              ----------

              diff:

                  ---

                  +++

                  @@ -273,7 +273,7 @@

                   # You will have to access it by its address anyway, and this will make

                   # redirections work in a sensible way.

                   #

                  -#ServerName www.example.com:80

                  +ServerName www.coohx.com:80

                   # UseCanonicalName: Determines how Apache constructs self-referencing

    Function: service.running

        Name: httpd

     Comment: Service reloaded

     Started: 19:22:16.189186

    Duration: 267.497 ms

              httpd:

                  True

          ID: mysql-service

    Function: pkg.installed

        Name: mysql-server

     Comment: Package mysql-server is already installed.

     Started: 19:22:16.457774

    Duration: 4.411 ms

        Name: /etc/my.cnf

     Comment: File /etc/my.cnf updated

     Started: 19:22:16.463750

    Duration: 25.095 ms

                  @@ -13,6 +13,8 @@

                   # If you want to know which options a program supports, run the program

                   # with the "--help" option.

                  +#web2.coohx.com

                  +

                   # The following options will be passed to all MySQL clients

                   [client]

                   #password    = your_password

                  @@ -24,7 +26,7 @@

                   # The MySQL server

                   [mysqld]

                   port         = 3306

                  -socket               = /var/lib/mysql/mysql.sock

                  +socket               = /tmp/mysql.sock

                   character_set_server = utf8

                   skip-locking

                   key_buffer_size = 256M

        Name: mysqld

      Result: False

     Comment: Failed to restart the service

     Started: 19:22:16.548901

    Duration: 2260.008 ms

              mysqld:

                  False

Summary

-------------

Succeeded: 11 (changed=4)

Failed:     1

Total states run:     12

繼續閱讀