saltstack的配置配置

一、为不同的环境设置不同的文件目录

1.1 修改配置文件 /etc/salt/master

[root@node1 salt]# vim /etc/salt/master

 file_roots:
   base:
     - /srv/salt/base
   dev:
     - /srv/salt/dev
   prod:
     - /srv/salt/prod

1.2 创建对应的文件夹

[root@node1 salt]# mkdir -p /srv/salt/{base,dev,prod}

1.3 重启服务

[root@node1 salt]# systemctl restart salt-master

二、文件管理

2.1准备入口文件

更改 base 环境的 dns 配置,首先在 base 文件夹下创建入口文件 top.sls

[root@node1 salt]# cd /srv/salt/base/

[root@node1 base]# vim top.sls

base:
  'web1':
    - dns_file

2.2 配置分发文件说明

[root@node1 base]# vim dns_file.sls

resolv_conf:
  file.managed:
    - name: /etc/resolv.conf
    - source: salt://files/resolv.conf
    - user: root
    - group: root
    - mode: 644

2.3 执行文件分发操作

[root@node1 base]# mkdir files

[root@node1 base]# vim files/resolv.conf

nameserver 8.8.8.8

2.4 测试

[root@node1 base]# salt '*' state.highstate saltenv=base test

web2:
----------
          ID: states
    Function: no.None
      Result: False
     Comment: No Top file or master_tops data matches found.
     Changes:   

Summary for web2
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   0.000 ms
web3:
----------
          ID: states
    Function: no.None
      Result: False
     Comment: No Top file or master_tops data matches found.
     Changes:   
…………
web1:
----------
          ID: resolv_conf
    Function: file.managed
        Name: /etc/resolv.conf
      Result: None
     Comment: The file /etc/resolv.conf is set to be changed
     Started: 17:33:30.626072
    Duration: 48.745 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,3 +1 @@
                  -# Generated by NetworkManager
                  -search localdomain
                  -nameserver 172.25.254.2
                  +nameserver 8.8.8.8

Summary for web1
------------
Succeeded: 1 (unchanged=1, changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  48.745 ms

2.5 运行

[root@node1 base]# salt '*' state.highstate saltenv=base False

saltstack的配置配置

2.6 检测

[root@node1 base]# salt 'web1' cmd.run 'cat /etc/resolv.conf'

saltstack的配置配置

2.7 修改所有节点 的 resolv.conf 配置

给issue 文件追加内容

[root@node1 base]# vim top.sls

base:
  'L@web1,web2,web3,db1,db2':
    - dns- issue
    - rpm_pkgs

[root@node1 base]# vim dns_file.sls 

resolv_conf:
  file.managed:
    - name: /etc/resolv.conf
    - source: salt://files/resolv.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
      DNS_IP: 192.168.113.254

[root@node1 base]# mkdir -p /srv/salt/base/files

[root@node1 base]# vim files/resolv.conf

nameserver 8.8.4.4

[root@node1 base]# vim issue.sls

issue_file:
  file.append:
    - name: /etc/issue
    - text:
      - Red Hat Enterprise Linux 7
      - Tedu Ltd Server

[root@node1 base]# vim rpm_pkgs.sls

rpms:
  file.directory:
    - name: /opt/rpm_pkgs
    - user: root
    - group: root
    - dir_mode: 755
    - file_mode: 644

[root@node1 base]# salt '*' state.highstate saltenv=base test=True

saltstack的配置配置
web1:
----------
          ID: resolv_conf
    Function: file.managed
        Name: /etc/resolv.conf
      Result: None
     Comment: The file /etc/resolv.conf is set to be changed
     Started: 20:09:06.189301
    Duration: 893.458 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,3 +1 @@
                  -# Generated by NetworkManager
                  -search localdomain
                  -nameserver 172.25.254.2
                  +nameserver 8.8.4.4
----------
          ID: issue_file
    Function: file.append
        Name: /etc/issue
      Result: None
     Comment: File /etc/issue is set to be updated
     Started: 20:09:07.082933
    Duration: 2.67 ms
     Changes:   
              ----------
              diff:
                  --- 
                  
                  +++ 
                  
                  @@ -1,3 +1,5 @@
                  
                   \S
                   Kernel \r on an \m
                   
                  +Red Hat Enterprise Linux 7
                  +Tedu Ltd Server
----------
          ID: rpms
    Function: file.directory
        Name: /opt/rpm_pkgs
      Result: None
     Comment: The following files will be changed:
              /opt/rpm_pkgs: directory - new
     Started: 20:09:07.085724
    Duration: 0.432 ms
     Changes:   

Summary for web1
------------
Succeeded: 3 (unchanged=3, changed=2)
Failed:    0
------------
Total states run:     3
Total run time: 896.560 ms
web2:
----------
          ID: resolv_conf
    Function: file.managed
        Name: /etc/resolv.conf
      Result: None
     Comment: The file /etc/resolv.conf is set to be changed
     Started: 20:09:06.520386
    Duration: 1121.741 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,3 +1 @@
                  -# Generated by NetworkManager
                  -search localdomain
                  -nameserver 172.25.254.2
                  +nameserver 8.8.4.4
----------
          ID: issue_file
    Function: file.append
        Name: /etc/issue
      Result: None
     Comment: File /etc/issue is set to be updated
     Started: 20:09:07.642292
    Duration: 2.611 ms
     Changes:   
              ----------
              diff:
                  --- 
                  
                  +++ 
                  
                  @@ -1,3 +1,5 @@
                  
                   \S
                   Kernel \r on an \m
                   
                  +Red Hat Enterprise Linux 7
                  +Tedu Ltd Server
----------
          ID: rpms
    Function: file.directory
        Name: /opt/rpm_pkgs
      Result: None
     Comment: The following files will be changed:
              /opt/rpm_pkgs: directory - new
     Started: 20:09:07.645019
    Duration: 0.426 ms
     Changes:   

Summary for web2
------------
Succeeded: 3 (unchanged=3, changed=2)
Failed:    0
------------
Total states run:     3
Total run time:   1.125 s
db1:
----------
          ID: resolv_conf
    Function: file.managed
        Name: /etc/resolv.conf
      Result: None
     Comment: The file /etc/resolv.conf is set to be changed
     Started: 20:09:06.055239
    Duration: 1069.927 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,4 +1 @@
                  -# Generated by NetworkManager
                  -search localdomain
                  -nameserver 192.168.2.1
                  -nameserver 172.25.254.2
                  +nameserver 8.8.4.4
----------
          ID: issue_file
    Function: file.append
        Name: /etc/issue
      Result: None
     Comment: File /etc/issue is set to be updated
     Started: 20:09:07.125304
    Duration: 2.857 ms
     Changes:   
              ----------
              diff:
                  --- 
                  
                  +++ 
                  
                  @@ -1,3 +1,5 @@
                  
                   \S
                   Kernel \r on an \m
                   
                  +Red Hat Enterprise Linux 7
                  +Tedu Ltd Server
----------
          ID: rpms
    Function: file.directory
        Name: /opt/rpm_pkgs
      Result: None
     Comment: The following files will be changed:
              /opt/rpm_pkgs: directory - new
     Started: 20:09:07.128287
    Duration: 0.441 ms
     Changes:   

Summary for db1
------------
Succeeded: 3 (unchanged=3, changed=2)
Failed:    0
------------
Total states run:     3
Total run time:   1.073 s
web3:
----------
          ID: resolv_conf
    Function: file.managed
        Name: /etc/resolv.conf
      Result: None
     Comment: The file /etc/resolv.conf is set to be changed
     Started: 20:09:06.368308
    Duration: 915.69 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,3 +1 @@
                  -# Generated by NetworkManager
                  -search localdomain
                  -nameserver 172.25.254.2
                  +nameserver 8.8.4.4
----------
          ID: issue_file
    Function: file.append
        Name: /etc/issue
      Result: None
     Comment: File /etc/issue is set to be updated
     Started: 20:09:07.284136
    Duration: 3.062 ms
     Changes:   
              ----------
              diff:
                  --- 
                  
                  +++ 
                  
                  @@ -1,3 +1,5 @@
                  
                   \S
                   Kernel \r on an \m
                   
                  +Red Hat Enterprise Linux 7
                  +Tedu Ltd Server
----------
          ID: rpms
    Function: file.directory
        Name: /opt/rpm_pkgs
      Result: None
     Comment: The following files will be changed:
              /opt/rpm_pkgs: directory - new
     Started: 20:09:07.287324
    Duration: 0.45 ms
     Changes:   

Summary for web3
------------
Succeeded: 3 (unchanged=3, changed=2)
Failed:    0
------------
Total states run:     3
Total run time: 919.202 ms
db2:
----------
          ID: resolv_conf
    Function: file.managed
        Name: /etc/resolv.conf
      Result: None
     Comment: The file /etc/resolv.conf is set to be changed
     Started: 20:09:06.578547
    Duration: 493.823 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,3 +1 @@
                  -# Generated by NetworkManager
                  -search localdomain
                  -nameserver 172.25.254.2
                  +nameserver 8.8.4.4
----------
          ID: issue_file
    Function: file.append
        Name: /etc/issue
      Result: None
     Comment: File /etc/issue is set to be updated
     Started: 20:09:07.072509
    Duration: 2.762 ms
     Changes:   
              ----------
              diff:
                  --- 
                  
                  +++ 
                  
                  @@ -1,3 +1,5 @@
                  
                   \S
                   Kernel \r on an \m
                   
                  +Red Hat Enterprise Linux 7
                  +Tedu Ltd Server
----------
          ID: rpms
    Function: file.directory
        Name: /opt/rpm_pkgs
      Result: None
     Comment: The following files will be changed:
              /opt/rpm_pkgs: directory - new
     Started: 20:09:07.075408
    Duration: 0.423 ms
     Changes:   

Summary for db2
------------
Succeeded: 3 (unchanged=3, changed=2)
Failed:    0
------------
Total states run:     3
Total run time: 497.008 ms
View Code

[root@node1 base]# salt '*' state.highstate saltenv=base test=False

saltstack的配置配置

[root@node1 base]# salt '*' cmd.run 'cat /etc/resolv.conf'

saltstack的配置配置

[root@node1 base]# salt '*' cmd.run 'ls -l /opt/'

saltstack的配置配置

[root@node1 base]# salt '*' cmd.run "cat /etc/issue |grep -A 2 Red"

saltstack的配置配置

三、配置项目

配置history命令显示时间

配置系统内核参数 net.ipv4.ip_forward 打开路由转发

创建用户zhang3

设置初始化密码为123456

配置用户初次登陆必须修改密码

3.1 配置top.sls

[root@node1 base]# vim top.sls

base:
  'L@web1,web2,web3,db1,db2':
    - init/history
    - init/sysctl
    - init/add_user

3.2 配置history.sls

 [root@node1 base]# vim init/history.sls

history_file:
  file.append:
    - name: /etc/profile
    - text:
      - export HISTORYFORMAT="[%F_%T]:"

3.3 配置sysctl.sls

[root@node1 base]# vim init/sysctl.sls

net_ipv4.ip_forward:
  sysctl.present:
  - value: 1

3.4 配置add_user.sls

[root@node1 base]# vim init/add_user.sls

useradd zhang3:      #添加用户
  cmd.run:
  - unless: id zhang3
echo 123456|passwd --stdin root:     #修改密碼
  cmd.run:
  - onlyif: id zhang3  
chage -d0 zhang3:                    #设置初次登录修改密码策略
  cmd.run:
    - onlyif: id zhang3

3.5 测试

[root@node1 base]# salt '*' state.highstate saltenv=base test=True

saltstack的配置配置
web2:
----------
          ID: history_file
    Function: file.append
        Name: /etc/profile
      Result: None
     Comment: File /etc/profile is set to be updated
     Started: 20:53:48.826389
    Duration: 6.303 ms
     Changes:   
              ----------
              diff:
                  --- 
                  
                  +++ 
                  
                  @@ -74,3 +74,4 @@
                  
                   
                   unset i
                   unset -f pathmunge
                  +export HISTORYFORMAT="[%F_%T]:"
----------
          ID: net_ipv4.ip_forward
    Function: sysctl.present
      Result: None
     Comment: Sysctl option net_ipv4.ip_forward might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
     Started: 20:53:48.834509
    Duration: 444.162 ms
     Changes:   
----------
          ID: useradd_zhang3
    Function: cmd.run
      Result: None
     Comment: Command "useradd_zhang3" would have been executed
     Started: 20:53:49.279489
    Duration: 21.675 ms
     Changes:   
----------
          ID: echo 123456|passwd --stdin root
    Function: cmd.run
      Result: True
     Comment: onlyif execution failed
     Started: 20:53:49.301337
    Duration: 8.862 ms
     Changes:   
----------
          ID: chage -d0 zhang3
    Function: cmd.run
      Result: True
     Comment: onlyif execution failed
     Started: 20:53:49.310391
    Duration: 8.55 ms
     Changes:   

Summary for web2
------------
Succeeded: 5 (unchanged=3, changed=1)
Failed:    0
------------
Total states run:     5
Total run time: 489.552 ms
web3:
----------
          ID: history_file
    Function: file.append
        Name: /etc/profile
      Result: None
     Comment: File /etc/profile is set to be updated
     Started: 20:53:49.189855
    Duration: 7.214 ms
     Changes:   
              ----------
              diff:
                  --- 
                  
                  +++ 
                  
                  @@ -74,3 +74,4 @@
                  
                   
                   unset i
                   unset -f pathmunge
                  +export HISTORYFORMAT="[%F_%T]:"
----------
          ID: net_ipv4.ip_forward
    Function: sysctl.present
      Result: None
     Comment: Sysctl option net_ipv4.ip_forward might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
     Started: 20:53:49.198787
    Duration: 47.317 ms
     Changes:   
----------
          ID: useradd_zhang3
    Function: cmd.run
      Result: None
     Comment: Command "useradd_zhang3" would have been executed
     Started: 20:53:49.247100
    Duration: 8.712 ms
     Changes:   
----------
          ID: echo 123456|passwd --stdin root
    Function: cmd.run
      Result: True
     Comment: onlyif execution failed
     Started: 20:53:49.255980
    Duration: 10.439 ms
     Changes:   
----------
          ID: chage -d0 zhang3
    Function: cmd.run
      Result: True
     Comment: onlyif execution failed
     Started: 20:53:49.266583
    Duration: 15.065 ms
     Changes:   

Summary for web3
------------
Succeeded: 5 (unchanged=3, changed=1)
Failed:    0
------------
Total states run:     5
Total run time:  88.747 ms
db1:
----------
          ID: history_file
    Function: file.append
        Name: /etc/profile
      Result: None
     Comment: File /etc/profile is set to be updated
     Started: 20:53:49.934751
    Duration: 6.928 ms
     Changes:   
              ----------
              diff:
                  --- 
                  
                  +++ 
                  
                  @@ -74,3 +74,4 @@
                  
                   
                   unset i
                   unset -f pathmunge
                  +export HISTORYFORMAT="[%F_%T]:"
----------
          ID: net_ipv4.ip_forward
    Function: sysctl.present
      Result: None
     Comment: Sysctl option net_ipv4.ip_forward might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
     Started: 20:53:49.943448
    Duration: 572.594 ms
     Changes:   
----------
          ID: useradd_zhang3
    Function: cmd.run
      Result: None
     Comment: Command "useradd_zhang3" would have been executed
     Started: 20:53:50.516913
    Duration: 7.899 ms
     Changes:   
----------
          ID: echo 123456|passwd --stdin root
    Function: cmd.run
      Result: True
     Comment: onlyif execution failed
     Started: 20:53:50.524982
    Duration: 8.226 ms
     Changes:   
----------
          ID: chage -d0 zhang3
    Function: cmd.run
      Result: True
     Comment: onlyif execution failed
     Started: 20:53:50.533404
    Duration: 8.202 ms
     Changes:   

Summary for db1
------------
Succeeded: 5 (unchanged=3, changed=1)
Failed:    0
------------
Total states run:     5
Total run time: 603.849 ms
web1:
----------
          ID: history_file
    Function: file.append
        Name: /etc/profile
      Result: None
     Comment: File /etc/profile is set to be updated
     Started: 20:53:52.297910
    Duration: 6.668 ms
     Changes:   
              ----------
              diff:
                  --- 
                  
                  +++ 
                  
                  @@ -74,3 +74,4 @@
                  
                   
                   unset i
                   unset -f pathmunge
                  +export HISTORYFORMAT="[%F_%T]:"
----------
          ID: net_ipv4.ip_forward
    Function: sysctl.present
      Result: None
     Comment: Sysctl option net_ipv4.ip_forward might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
     Started: 20:53:52.306369
    Duration: 149.86 ms
     Changes:   
----------
          ID: useradd_zhang3
    Function: cmd.run
      Result: None
     Comment: Command "useradd_zhang3" would have been executed
     Started: 20:53:52.457076
    Duration: 9.451 ms
     Changes:   
----------
          ID: echo 123456|passwd --stdin root
    Function: cmd.run
      Result: True
     Comment: onlyif execution failed
     Started: 20:53:52.466704
    Duration: 8.66 ms
     Changes:   
----------
          ID: chage -d0 zhang3
    Function: cmd.run
      Result: True
     Comment: onlyif execution failed
     Started: 20:53:52.475542
    Duration: 9.109 ms
     Changes:   

Summary for web1
------------
Succeeded: 5 (unchanged=3, changed=1)
Failed:    0
------------
Total states run:     5
Total run time: 183.748 ms
db2:
----------
          ID: history_file
    Function: file.append
        Name: /etc/profile
      Result: None
     Comment: File /etc/profile is set to be updated
     Started: 20:53:50.796859
    Duration: 7.861 ms
     Changes:   
              ----------
              diff:
                  --- 
                  
                  +++ 
                  
                  @@ -74,3 +74,4 @@
                  
                   
                   unset i
                   unset -f pathmunge
                  +export HISTORYFORMAT="[%F_%T]:"
----------
          ID: net_ipv4.ip_forward
    Function: sysctl.present
      Result: None
     Comment: Sysctl option net_ipv4.ip_forward might be changed, we failed to check config file at /etc/sysctl.d/99-salt.conf. The file is either unreadable, or missing.
     Started: 20:53:50.806454
    Duration: 219.199 ms
     Changes:   
----------
          ID: useradd_zhang3
    Function: cmd.run
      Result: None
     Comment: Command "useradd_zhang3" would have been executed
     Started: 20:53:51.026499
    Duration: 10.024 ms
     Changes:   
----------
          ID: echo 123456|passwd --stdin root
    Function: cmd.run
      Result: True
     Comment: onlyif execution failed
     Started: 20:53:51.036731
    Duration: 166.817 ms
     Changes:   
----------
          ID: chage -d0 zhang3
    Function: cmd.run
      Result: True
     Comment: onlyif execution failed
     Started: 20:53:51.203873
    Duration: 11.691 ms
     Changes:   

Summary for db2
------------
Succeeded: 5 (unchanged=3, changed=1)
Failed:    0
------------
Total states run:     5
Total run time: 415.592 ms
View Code

3.6 运行检测

[root@node1 base]# salt '*' state.highstate saltenv=base test=False

saltstack的配置配置
web1:
----------
          ID: history_file
    Function: file.append
        Name: /etc/profile
      Result: True
     Comment: File /etc/profile is in correct state
     Started: 20:59:02.816332
    Duration: 6.71 ms
     Changes:   
----------
          ID: net.ipv4.ip_forward
    Function: sysctl.present
      Result: True
     Comment: Sysctl value net.ipv4.ip_forward = 1 is already set
     Started: 20:59:02.824619
    Duration: 119.326 ms
     Changes:   
----------
          ID: useradd zhang3
    Function: cmd.run
      Result: True
     Comment: Command "useradd zhang3" run
     Started: 20:59:02.944856
    Duration: 238.726 ms
     Changes:   
              ----------
              pid:
                  15438
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: echo 123456|passwd --stdin root
    Function: cmd.run
      Result: True
     Comment: Command "echo 123456|passwd --stdin root" run
     Started: 20:59:03.183785
    Duration: 441.791 ms
     Changes:   
              ----------
              pid:
                  15445
              retcode:
                  0
              stderr:
              stdout:
                  Changing password for user root.
                  passwd: all authentication tokens updated successfully.
----------
          ID: chage -d0 zhang3
    Function: cmd.run
      Result: True
     Comment: Command "chage -d0 zhang3" run
     Started: 20:59:03.625769
    Duration: 142.791 ms
     Changes:   
              ----------
              pid:
                  15461
              retcode:
                  0
              stderr:
              stdout:

Summary for web1
------------
Succeeded: 5 (changed=3)
Failed:    0
------------
Total states run:     5
Total run time: 949.344 ms
web2:
----------
          ID: history_file
    Function: file.append
        Name: /etc/profile
      Result: True
     Comment: File /etc/profile is in correct state
     Started: 20:59:03.052625
    Duration: 6.965 ms
     Changes:   
----------
          ID: net.ipv4.ip_forward
    Function: sysctl.present
      Result: True
     Comment: Sysctl value net.ipv4.ip_forward = 1 is already set
     Started: 20:59:03.061223
    Duration: 31.778 ms
     Changes:   
----------
          ID: useradd zhang3
    Function: cmd.run
      Result: True
     Comment: Command "useradd zhang3" run
     Started: 20:59:03.094172
    Duration: 898.344 ms
     Changes:   
              ----------
              pid:
                  15102
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: echo 123456|passwd --stdin root
    Function: cmd.run
      Result: True
     Comment: Command "echo 123456|passwd --stdin root" run
     Started: 20:59:03.992757
    Duration: 285.178 ms
     Changes:   
              ----------
              pid:
                  15136
              retcode:
                  0
              stderr:
              stdout:
                  Changing password for user root.
                  passwd: all authentication tokens updated successfully.
----------
          ID: chage -d0 zhang3
    Function: cmd.run
      Result: True
     Comment: Command "chage -d0 zhang3" run
     Started: 20:59:04.278136
    Duration: 200.56 ms
     Changes:   
              ----------
              pid:
                  15143
              retcode:
                  0
              stderr:
              stdout:

Summary for web2
------------
Succeeded: 5 (changed=3)
Failed:    0
------------
Total states run:     5
Total run time:   1.423 s
db2:
----------
          ID: history_file
    Function: file.append
        Name: /etc/profile
      Result: True
     Comment: File /etc/profile is in correct state
     Started: 20:59:01.635525
    Duration: 6.95 ms
     Changes:   
----------
          ID: net.ipv4.ip_forward
    Function: sysctl.present
      Result: True
     Comment: Sysctl value net.ipv4.ip_forward = 1 is already set
     Started: 20:59:01.645388
    Duration: 411.074 ms
     Changes:   
----------
          ID: useradd zhang3
    Function: cmd.run
      Result: True
     Comment: Command "useradd zhang3" run
     Started: 20:59:02.057348
    Duration: 1223.669 ms
     Changes:   
              ----------
              pid:
                  14948
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: echo 123456|passwd --stdin root
    Function: cmd.run
      Result: True
     Comment: Command "echo 123456|passwd --stdin root" run
     Started: 20:59:03.281214
    Duration: 1104.784 ms
     Changes:   
              ----------
              pid:
                  14957
              retcode:
                  0
              stderr:
              stdout:
                  Changing password for user root.
                  passwd: all authentication tokens updated successfully.
----------
          ID: chage -d0 zhang3
    Function: cmd.run
      Result: True
     Comment: Command "chage -d0 zhang3" run
     Started: 20:59:04.386271
    Duration: 41.183 ms
     Changes:   
              ----------
              pid:
                  14965
              retcode:
                  0
              stderr:
              stdout:

Summary for db2
------------
Succeeded: 5 (changed=3)
Failed:    0
------------
Total states run:     5
Total run time:   2.788 s
web3:
----------
          ID: history_file
    Function: file.append
        Name: /etc/profile
      Result: True
     Comment: File /etc/profile is in correct state
     Started: 20:59:03.435729
    Duration: 6.858 ms
     Changes:   
----------
          ID: net.ipv4.ip_forward
    Function: sysctl.present
      Result: True
     Comment: Sysctl value net.ipv4.ip_forward = 1 is already set
     Started: 20:59:03.444273
    Duration: 31.748 ms
     Changes:   
----------
          ID: useradd zhang3
    Function: cmd.run
      Result: True
     Comment: Command "useradd zhang3" run
     Started: 20:59:03.477440
    Duration: 376.603 ms
     Changes:   
              ----------
              pid:
                  14825
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: echo 123456|passwd --stdin root
    Function: cmd.run
      Result: True
     Comment: Command "echo 123456|passwd --stdin root" run
     Started: 20:59:03.854320
    Duration: 988.137 ms
     Changes:   
              ----------
              pid:
                  14832
              retcode:
                  0
              stderr:
              stdout:
                  Changing password for user root.
                  passwd: all authentication tokens updated successfully.
----------
          ID: chage -d0 zhang3
    Function: cmd.run
      Result: True
     Comment: Command "chage -d0 zhang3" run
     Started: 20:59:04.842672
    Duration: 616.962 ms
     Changes:   
              ----------
              pid:
                  14839
              retcode:
                  0
              stderr:
              stdout:

Summary for web3
------------
Succeeded: 5 (changed=3)
Failed:    0
------------
Total states run:     5
Total run time:   2.020 s
db1:
----------
          ID: history_file
    Function: file.append
        Name: /etc/profile
      Result: True
     Comment: File /etc/profile is in correct state
     Started: 20:59:04.695357
    Duration: 6.211 ms
     Changes:   
----------
          ID: net.ipv4.ip_forward
    Function: sysctl.present
      Result: True
     Comment: Sysctl value net.ipv4.ip_forward = 1 is already set
     Started: 20:59:04.703099
    Duration: 268.77 ms
     Changes:   
----------
          ID: useradd zhang3
    Function: cmd.run
      Result: True
     Comment: Command "useradd zhang3" run
     Started: 20:59:04.973150
    Duration: 274.212 ms
     Changes:   
              ----------
              pid:
                  14525
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: echo 123456|passwd --stdin root
    Function: cmd.run
      Result: True
     Comment: Command "echo 123456|passwd --stdin root" run
     Started: 20:59:05.247554
    Duration: 257.399 ms
     Changes:   
              ----------
              pid:
                  14531
              retcode:
                  0
              stderr:
              stdout:
                  Changing password for user root.
                  passwd: all authentication tokens updated successfully.
----------
          ID: chage -d0 zhang3
    Function: cmd.run
      Result: True
     Comment: Command "chage -d0 zhang3" run
     Started: 20:59:05.505137
    Duration: 144.971 ms
     Changes:   
              ----------
              pid:
                  14538
              retcode:
                  0
              stderr:
              stdout:

Summary for db1
------------
Succeeded: 5 (changed=3)
Failed:    0
------------
Total states run:     5
Total run time: 951.563 ms
View Code

[root@node1 base]# salt '*' cmd.run 'id  zhang3'

saltstack的配置配置

[root@node1 base]# salt '*' cmd.run 'sysctl -a|grep "net.ipv4.ip_forward = 1"'

saltstack的配置配置

四、 源码安裝nginx

使用Dev环境

编译源码安装步骤

由于 nginx 源码安装是一个独立工作,我们建立一个项目

4.1 创建项目

[root@node1 dev]# mkdir initpkg

[root@node1 dev]# vim initpkg/pkg_install.sls

init_pkg_install:
  pkg.installed:
    - names:
      - gcc
      - gcc-c++
      - make
      - autoconf
      - openssl-devel
      - pcre-devel              

4.2 安装依赖包 initpkg.sls

[root@node1 dev]# mkdir initpkg

[root@node1 dev]# vim initpkg/install.sls

init-pkg-install:
  pkg.installed:
    - names:
      - gcc
      - gcc-c++
      - make
      - autoconf
      - openssl
      - openssl-devel
      - lsof
      - tree
      - lrzsz

4.3 源码编译安装 pcre

[root@node1 dev]# mkdir pcre

[root@node1 dev]# vim pcre/install.sls 

include:
  - initpkg.install

pcre-source-install:
  file.managed:
    - name: /usr/local/src/pcre-8.38.tar.gz
    - source: salt://pcre/files/pcre-8.38.tar.gz
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: cd /usr/local/src && tar xf pcre-8.38.tar.gz && cd pcre-8.38 && ./configure --prefix=/usr/local/pcre && make && make install
    - unless: test -d /usr/local/pcre
    - reuqire:
      - file: pcre-source-install                                

 4.4 编译安装nginx

[root@node1 dev]# mkdir nginx

[root@node1 dev]# vim nginx/install.sls

include:
  - initpkg.install
  - pcre.install

nginx-source-install:
  file.managed:
    - name: /usr/local/src/nginx-1.12.2.tar.gz
    - source: salt://nginx/files/nginx-1.12.2.tar.gz
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: cd /usr/local/src && tar xf nginx-1.12.2.tar.gz && cd nginx-1.12.2 && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module --with-pcre=/usr/local/src/pcre-8.38 && make && make install
    - unless: test -d /usr/local/nginx
    - require:
      - file: nginx-source-install
      - pkg: init-pkg-install
      - file: pcre-source-install
nginx-init:
  file.managed:
    - name: /etc/init.d/nginx
    - source: salt://nginx/files/nginx
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: chkconfig --add nginx
    - unless: chkconfig --list |grep nginx
    - reuqire:
      - cmd: nginx-source-install
      - file: nginx-init

4.5 配置top

[root@node1 dev]# vim top.sls

[root@node1 dev]# cd nginx/

[root@node1 nginx]# mkdir files

[root@node1 nginx]# cd files/

4.6 下载包

[root@node1 files]# ll

-rw-r--r--. 1 root root   1936 Apr  3 22:28 nginx
-rw-r--r--. 1 root root 981687 Oct 17  2017 nginx-1.12.2.tar.gz

[root@node1 files]# cd ../../pcre/

[root@node1 pcre]# mkdir files

[root@node1 pcre]# cd files

[root@node1 files]# ll

[root@node1 files]# vim nginx

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

4.7 检测

[root@node1 dev]# tree

.
├── initpkg
│   └── install.sls
├── nginx
│   ├── files
│   │   ├── nginx
│   │   └── nginx-1.12.2.tar.gz
│   └── install.sls
├── pcre
│   ├── files
│   │   └── pcre-8.38.tar.gz
│   └── install.sls
└── top.sls

5 directories, 7 files

4.8 测试

[root@node1 files]# salt 'web1' state.highstate env=dev test

4.9 运行

[root@node1 files]# salt 'web1' state.highstate env=dev test=False

ID: nginx-init
    Function: file.managed
        Name: /etc/init.d/nginx
      Result: True
     Comment: File /etc/init.d/nginx updated
     Started: 22:39:24.379206
    Duration: 68.116 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0755
----------
          ID: nginx-init
    Function: cmd.run
        Name: chkconfig --add nginx
      Result: True
     Comment: Command "chkconfig --add nginx" run
     Started: 22:39:24.447622
    Duration: 81.706 ms
     Changes:   
              ----------
              pid:
                  34327
              retcode:
                  0
              stderr:
              stdout:

Summary for web1
-------------
Succeeded: 15 (changed=13)
Failed:     0
-------------
Total states run:     15
Total run time:  449.603 s

检测

[root@node1 dev]# salt 'web1' cmd.run 'ls -l  /usr/local/nginx'
web1:
    total 4
    drwxr-xr-x. 2 root root 4096 Apr  3 22:39 conf
    drwxr-xr-x. 2 root root   40 Apr  3 22:39 html
    drwxr-xr-x. 2 root root    6 Apr  3 22:39 logs
    drwxr-xr-x. 2 root root   19 Apr  3 22:39 sbin

4.10 启动

[root@node1 dev]# salt 'web1' cmd.run 'systemctl restart nginx'
web1:
[root@node1 dev]# salt 'web1' cmd.run 'netstat -ntlp|grep nginx'
web1:
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      35214/nginx: master

访问成功

saltstack的配置配置

 

上一篇:git三种文件状态


下一篇:常见git命令