saltstack(七):saltstack配置管理-lamp状态管理简单样例

1.1         minion端lamp状态管理

提示:master:提前装好httpd,php,mysql用于配置文件的生成。

文件结构

[root@pe-jira salt]# tree
.
├── base
│   ├── top.sls
│   └── web
│       ├── apache.sls
│       └── apache.sls_bak
├── dev
├── prod
│   ├── apache
│   │   ├── files
│   │   │   └── httpd.conf
│   │   └── init.sls
│   ├── mysql
│   │   ├── files
│   │   │   └── my.cnf
│   │   └── init.sls
│   └── php
│       ├── files
│       │   └── php.ini
│       └── init.sls
└── test

11 directories, 9 files

Apache配置

apache-install:
  pkg.installed:
    - name: httpd

apache-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://apache/files/httpd.conf  #file://环境的根路径
    - user: root
    - group: root
    - mode: 644

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

PHP配置

php-install:
  pkg.installed:
    - pkgs:
      - php
      - php-pdo
      - php-mysql

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

MySQL配置

mysql-install:
  pkg.installed:
    - pkgs:
      - mariadb
      - mariadb-server

mysql-config:
  file.managed:
    - name: /etc/my.cnf
    - source: salt://mysql/files/my.cnf
    - user: root
    - group: root
    - mode : 644

mysql-service:
  service.running:
    - name : mariadb
    - enable: True

 测试

高级状态执行

[root@pe-jira salt]# cd /srv/salt/base/
[root@pe-jira base]# ls
top.sls  web
[root@pe-jira base]# cat top.sls
#base:
#  'pe-db':
#    - web.apache
#  'test-tms':
#    - web.apache

prod:
  'test-tms':
    - apache.init
    - php.init
    - mysql.init

  'test-mall':
    - apache.init
    - php.init
    - mysql.init
[root@pe-jira base]#

[root@pe-jira base]# salt -E 'test-tms|test-mall' state.highstate
test-tms:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 15:05:11.071241
    Duration: 666.091 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:05:11.740164
    Duration: 6.992 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:05:11.747665
    Duration: 367.001 ms
     Changes:
----------
          ID: php-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 15:05:12.114950
    Duration: 0.875 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:05:12.115956
    Duration: 4.982 ms
     Changes:
----------
          ID: mysql-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 15:05:12.121067
    Duration: 0.565 ms
     Changes:
----------
          ID: mysql-config
    Function: file.managed
        Name: /etc/my.cnf
      Result: True
     Comment: File /etc/my.cnf is in the correct state
     Started: 15:05:12.121788
    Duration: 3.483 ms
     Changes:
----------
          ID: mysql-service
    Function: service.running
        Name: mariadb
      Result: True
     Comment: Service mariadb is already enabled, and is in the desired state
     Started: 15:05:12.125409
    Duration: 346.613 ms
     Changes:

Summary
------------
Succeeded: 8
Failed:    0
------------
Total states run:     8
test-mall:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 15:05:11.070874
    Duration: 661.325 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:05:11.735025
    Duration: 5.373 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:05:11.740900
    Duration: 364.142 ms
     Changes:
----------
          ID: php-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 15:05:12.105335
    Duration: 0.887 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:05:12.106356
    Duration: 5.18 ms
     Changes:
----------
          ID: mysql-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 15:05:12.111677
    Duration: 0.586 ms
     Changes:
----------
          ID: mysql-config
    Function: file.managed
        Name: /etc/my.cnf
      Result: True
     Comment: File /etc/my.cnf is in the correct state
     Started: 15:05:12.112386
    Duration: 4.06 ms
     Changes:
----------
          ID: mysql-service
    Function: service.running
        Name: mariadb
      Result: True
     Comment: Service mariadb is already enabled, and is in the desired state
     Started: 15:05:12.116583
    Duration: 361.319 ms
     Changes:

Summary
------------
Succeeded: 8
Failed:    0
------------
Total states run:     8
[root@pe-jira base]#

include 包含多个sls文件 高级状态执行

[root@pe-jira prod]# pwd
/srv/salt/prod
[root@pe-jira prod]# ls
apache  lamp.sls  mysql  php
[root@pe-jira prod]# cat lamp.sls
#/srv/salt/prod
include:
  - apache.init
  - php.init
  - mysql.init
[root@pe-jira prod]# cat  ../base/top.sls
prod:
  'test-tms':
    - lamp
[root@pe-jira prod]#

[root@pe-jira prod]# salt 'test-tms' state.highstate
test-tms:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 17:18:42.840882
    Duration: 666.973 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:18:43.510531
    Duration: 5.027 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: 17:18:43.516109
    Duration: 362.105 ms
     Changes:
----------
          ID: php-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 17:18:43.878434
    Duration: 0.828 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:18:43.879389
    Duration: 5.198 ms
     Changes:
----------
          ID: mysql-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed.
     Started: 17:18:43.884744
    Duration: 0.569 ms
     Changes:
----------
          ID: mysql-config
    Function: file.managed
        Name: /etc/my.cnf
      Result: True
     Comment: File /etc/my.cnf is in the correct state
     Started: 17:18:43.885437
    Duration: 4.044 ms
     Changes:
----------
          ID: mysql-service
    Function: service.running
        Name: mariadb
      Result: True
     Comment: Service mariadb is already enabled, and is in the desired state
     Started: 17:18:43.889607
    Duration: 352.718 ms
     Changes:

Summary
------------
Succeeded: 8
Failed:    0
------------
Total states run:     8
[root@pe-jira prod]#

下发/增加多个文件/目录 file.recurse

 lnmp.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

apache-auth:
  pkg.installed:
    - name: httpd-tools
    - require_in:
      - cmd: apache-auth
  cmd.run:
    - name: htpasswd -bc /etc/httpd/conf/htpasswd_file admin admin
    - unless: test -f /etc/httpd/conf/htpasswd_file

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

/etc/php.ini:
  file.managed:
    - source: salt://web/files/php.ini
    - user: root
    - group: root
    - mode: 644
    - watch_in:
      - service: lamp-service

lamp-service:
  service.running:
    - name: httpd
    - enable: True
    - reload: True
    - watch:
    - file: apache-config

 

测试执行test=True 再批量执行

[root@pe-jira web]# cat ../top.sls
base:
  'test-tms':
    - web.lnmp
  'test-mall':
    - web.lnmp

 

posted on 2019-05-31 11:12  光阴8023  阅读(389)  评论(0编辑  收藏  举报