首页 > 代码库 > salt实现lamp自动化部署

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:

  pkg.installed:

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

    - name: httpd

    - enable: True

    - reload: True

    - watch:                      

      - file: apache-service

mysql-service:

  pkg.installed:

    - name: mysql-server

    - require_in:    

      - file: mysql-service

  file.managed:

    - name: /etc/my.cnf

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

    - user: root

    - group: root

    - mode: 644

    - watch_in:    

      - service: mysql-service

  service.running:

    - name: mysqld

    - enable: True

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

----------

          ID: apache-service

    Function: service.running

        Name: httpd

      Result: True

     Comment: Service reloaded

     Started: 19:22:16.189186

    Duration: 267.497 ms

     Changes:

              ----------

              httpd:

                  True

----------

          ID: mysql-service

    Function: pkg.installed

        Name: mysql-server

      Result: True

     Comment: Package mysql-server is already installed.

     Started: 19:22:16.457774

    Duration: 4.411 ms

     Changes:

----------

          ID: mysql-service

    Function: file.managed

        Name: /etc/my.cnf

      Result: True

     Comment: File /etc/my.cnf updated

     Started: 19:22:16.463750

    Duration: 25.095 ms

     Changes:

              ----------

              diff:

                  ---

                  +++

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

----------

          ID: mysql-service

    Function: service.running

        Name: mysqld

      Result: False

     Comment: Failed to restart the service

     Started: 19:22:16.548901

    Duration: 2260.008 ms

     Changes:

              ----------

              mysqld:

                  False

Summary

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

Succeeded: 11 (changed=4)

Failed:     1

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

Total states run:     12


salt实现lamp自动化部署