自动化部署--SaltStack

saltstack基本原理

Saltstack由master和minion构成,master是服务器端,表示一台服务器;minion是客户端,表示有多台服务器。在master上发送命令给符合条件的minion,minion就会执行相应的命令,master和minion之间是通过Zero(消息队列)进行通信的。

SaltStack的master端监听4505和4506端口,4505为master和minion认证通信端口,4506为master用来发送命令或接收minion的命令执行返回信息。

当客户端启动后,会主动连接master端注册,然后一直保持该TCP连接,而master通过这条TCP连接对客户端进行控制。如果断开连接,master对客户端将不能进行控制。但是,当客户端检查到连接断开后,会定期向master端请求注册连接

Master:控制中心,salt命令运行和资源状态管理端
Minions:需要管理的客户端机器,会主动去连接master端,并从master端得到资源状态,同步资源管理信息

安装配置saltstack

1.安装并开启saltstack
server1 server2 server3都执行

[root@server1 ~]# cd 2018/
[root@server1 2018]# ls
libsodium-1.0.16-1.el7.x86_64.rpm        PyYAML-3.11-1.el7.x86_64.rpm
openpgm-5.2.122-2.el7.x86_64.rpm         repodata
python2-libcloud-2.0.0-2.el7.noarch.rpm  salt-2018.3.3-1.el7.noarch.rpm
python-cherrypy-5.6.0-2.el7.noarch.rpm   salt-api-2018.3.3-1.el7.noarch.rpm
python-crypto-2.6.1-2.el7.x86_64.rpm     salt-cloud-2018.3.3-1.el7.noarch.rpm
python-futures-3.0.3-1.el7.noarch.rpm    salt-master-2018.3.3-1.el7.noarch.rpm
python-msgpack-0.4.6-1.el7.x86_64.rpm    salt-minion-2018.3.3-1.el7.noarch.rpm
python-psutil-2.2.1-1.el7.x86_64.rpm     salt-ssh-2018.3.3-1.el7.noarch.rpm
python-tornado-4.2.1-1.el7.x86_64.rpm    salt-syndic-2018.3.3-1.el7.noarch.rpm
python-zmq-15.3.0-3.el7.x86_64.rpm       zeromq-4.1.4-7.el7.x86_64.rpm
[root@server1 2018]# yum install -y *.rpm

server1安装salt-master,server2 server3安装salt-minion

[root@server1 2018]# systemctl start salt-master
[root@server1 2018]# systemctl enable salt-master

[root@server2 salt]# systemctl start salt-minion
[root@server2 salt]# systemctl enable salt-minion
[root@server2 2018]# cd /etc/salt/
[root@server2 salt]# vim minion
master: 172.25.68.1
[root@server2 salt]# systemctl restart salt-minion

[root@server3 salt]# systemctl start salt-minion
[root@server3 salt]# systemctl enable salt-minion
[root@server3 2018]# cd /etc/salt/
[root@server3 salt]# vim minion
master: 172.25.68.1
[root@server3 salt]# systemctl restart salt-minion

在这里插入图片描述
2.传key,建立连接

[root@server1 2018]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
Rejected Keys:

[root@server1 2018]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
server2
server3
Proceed? [n/Y] Y
Key for minion server2 accepted.
Key for minion server3 accepted.

[root@server1 2018]# salt-key -L
Accepted Keys:
server2
server3
Denied Keys:
Unaccepted Keys:
Rejected Keys:

在这里插入图片描述

自动化部署--SaltStack_第1张图片
测试:

[root@server1 2018]# salt '*' test.ping
server3:
    True
server2:
    True

在这里插入图片描述

3.查看端口关系

[root@server1 ~]# yum install -y lsof
[root@server1 ~]# netstat -ntlp
[root@server1 ~]# lsof -i :4505
[root@server1 2018]# lsof -i :4505
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
salt-mast 2209 root   15u  IPv4  28082      0t0  TCP *:4505 (LISTEN)
salt-mast 2209 root   17u  IPv4  31698      0t0  TCP server1:4505->server3:56638 (ESTABLISHED)
salt-mast 2209 root   18u  IPv4  31711      0t0  TCP server1:4505->server2:34006 (ESTABLISHED)
[root@server1 ~]# yum install python-setproctitle
[root@server1 ~]# systemctl restart salt-master
[root@server1 ~]# ps ax				##可以查看具体服务了

在这里插入图片描述

自动化部署--SaltStack_第2张图片
4.查看master minion关系

[root@server1 ~]# cd /etc/salt/
[root@server1 salt]# cd pki/
[root@server1 pki]# ls
master  minion
[root@server1 pki]# cd master/
[root@server1 master]# ls
master.pem  master.pub  minions  minions_autosign  minions_denied  minions_pre  minions_rejected
[root@server1 master]# md5sum master.pub 
6ab3d7a26afacec58f083f612269f359  master.pub

[root@server2 salt]# cd /etc/salt/pki/minion/
[root@server2 minion]# ls
minion_master.pub  minion.pem  minion.pub
[root@server2 minion]# md5sum minion_master.pub 
6ab3d7a26afacec58f083f612269f359  minion_master.pub

在这里插入图片描述

自动化部署--SaltStack_第3张图片

5.salt命令

[root@server1 master]# salt '*' cmd.run 'df -h'
server2:
    Filesystem             Size  Used Avail Use% Mounted on
    /dev/mapper/rhel-root  9.8G  1.3G  8.6G  13% /
    devtmpfs               486M     0  486M   0% /dev
    tmpfs                  497M   12K  497M   1% /dev/shm
    tmpfs                  497M   13M  484M   3% /run
    tmpfs                  497M     0  497M   0% /sys/fs/cgroup
    /dev/sda1             1014M  139M  876M  14% /boot
    tmpfs                  100M     0  100M   0% /run/user/0
server3:
    Filesystem             Size  Used Avail Use% Mounted on
    /dev/mapper/rhel-root  9.8G  1.3G  8.6G  13% /
    devtmpfs               486M     0  486M   0% /dev
    tmpfs                  497M   12K  497M   1% /dev/shm
    tmpfs                  497M   13M  484M   3% /run
    tmpfs                  497M     0  497M   0% /sys/fs/cgroup
    /dev/sda1             1014M  139M  876M  14% /boot
    tmpfs                  100M     0  100M   0% /run/user/0
[root@server1 master]# salt '*' cmd.run 'hostname'
server2:
    server2
server3:
    server3

自动化部署--SaltStack_第4张图片

配置

1.配置环境

[root@server1 salt]# cd /etc/salt
[root@server1 salt]# vim master
file_roots:
  base:
    - /srv/salt

[root@server1 salt]# mkdir /srv/salt
[root@server1 salt]# systemctl restart salt-master

自动化部署--SaltStack_第5张图片

2.创建角色,编写sls

[root@server1 salt]# cd /srv/salt/
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# mkdir apache
[root@server1 salt]# cd apache/
[root@server1 apache]# vim install.sls
[root@server1 apache]# cat install.sls 
httpd:
  pkg.installed
[root@server1 apache]# salt server2 state.sls apache.install	##apache执行install.sls(sls可以省略)
server2:
----------
          ID: httpd
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 20:53:24.417744
    Duration: 396.41 ms
     Changes:   

Summary for server2
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time: 396.410 ms

3.一次安装多个服务

[root@server1 apache]# cat install.sls 
httpd:
  pkg.installed:
    - pkgs:
      - httpd
      - php
      - httpd-tools
[root@server1 apache]# salt server2 state.sls apache.install
server2:
----------
          ID: httpd
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 09:06:04.837586
    Duration: 1264.0 ms
     Changes:   

Summary for server2
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time:   1.264 s

4.管理服务

[root@server1 apache]# cat install.sls 
httpd:
  pkg.installed:
    - pkgs:
      - httpd
      - php
      - httpd-tools

  service.running:
    - name: httpd
    - enable: true
    - reload: true

[root@server1 apache]# salt server2 state.sls apache.install
server2:
----------
          ID: httpd
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 09:08:20.411011
    Duration: 394.363 ms
     Changes:   
----------
          ID: httpd
    Function: service.running
      Result: True
     Comment: The service httpd is already running
     Started: 09:08:20.807379
    Duration: 67.23 ms
     Changes:   

Summary for server2
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 461.593 ms

自动化部署--SaltStack_第6张图片

5.更改httpd的配置文件

[root@server1 apache]# vim install.sls
 [root@server1 apache]# cat install.sls 
httpd:
  pkg.installed:
    - pkgs:
      - httpd
      - php
      - httpd-tools

  service.running:
    - name: httpd
    - enable: true
    - reload: true
    - watch:
      - file: /etc/httpd/conf/http.conf

/etc/httpd/conf/http.conf:
  file.managed:
    - source: salt://apache/files/httpd.conf
    - user: root
    - group: root
    - mode: 644

[root@server1 apache]# mkdir files
[root@server1 apache]# cd files/
[root@server1 files]# scp server2:/etc/httpd/conf/httpd.conf .

[root@server1 apache]# salt server2 state.sls apache.install
server2:
----------
          ID: httpd
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 09:13:07.628519
    Duration: 395.848 ms
     Changes:   
----------
          ID: httpd
    Function: service.running
      Result: True
     Comment: The service httpd is already running
     Started: 09:13:08.025016
    Duration: 21.584 ms
     Changes:   
----------
          ID: /etc/httpd/conf/http.conf
    Function: file.managed
      Result: True
     Comment: File /etc/httpd/conf/http.conf updated
     Started: 09:13:08.105342
    Duration: 40.392 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

Summary for server2
------------
Succeeded: 3 (changed=1)
Failed:    0
------------
Total states run:     3
Total run time: 457.824 ms

自动化部署--SaltStack_第7张图片

6.缓存机制,server1发送,server2 server3反馈

server1

[root@server1 apache]# cd /var/cache/
[root@server1 cache]# ls
ldconfig  man  salt  yum
[root@server1 cache]# cd salt/ 
[root@server1 salt]# ls
master
[root@server1 salt]# cd master/
[root@server1 master]# ls
file_lists  jobs  minions  proc  queues  roots  syndics  tokens


server2  在server2上有master的缓存文件

[root@server2 minion]# yum install -y tree
[root@server2 minion]# cd /var/cache/salt/minion/
[root@server2 minion]# tree .
.
├── accumulator
├── extmods
├── files
│   └── base
│       └── apache
│           ├── files
│           │   └── httpd.conf
│           └── install.sls
├── highstate.cache.p
├── pkg_refresh
├── proc
└── sls.p

7 directories, 5 files
[root@server2 minion]# ls
accumulator  extmods  files  highstate.cache.p  pkg_refresh  proc  sls.p
[root@server2 minion]# cd files/
[root@server2 files]# ls
base
[root@server2 files]# cd base/
[root@server2 base]# ls
apache
[root@server2 base]# cd apache/
[root@server2 apache]# ls
files  install.sls
[root@server2 apache]# cd files/
[root@server2 files]# ls
httpd.conf
源码配置nginx

1.解决依赖性,并把nginx安装包放到server3

root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# ls
apache
[root@server1 salt]# mkdir nginx
[root@server1 salt]# cd nginx/
[root@server1 nginx]# vim install.sls
nginx-install:
  pkg.installed:
    - pkgs:
      - gcc
      - make
      - pcre-devel
      - zlib-devel

  file.managed:
    - name: /mnt/nginx-1.15.8.tar.gz
    - source: salt://nginx/files/nginx-1.15.8.tar.gz
[root@server1 nginx]# mkdir files
[root@server1 nginx]# cd files/
[root@server1 files]# cp /root/nginx-1.15.8.tar.gz .
[root@server1 files]# ls
nginx-1.15.8.tar.gz
[root@server1 nginx]# salt server3 state.sls nginx.install
server3:
----------
          ID: nginx-install
    Function: pkg.installed
      Result: True
     Comment: 3 targeted packages were installed/updated.
              The following packages were already installed: make
     Started: 09:20:44.708498
    Duration: 14366.77 ms
     Changes:   
              ----------
              cpp:
                  ----------
                  new:
                      4.8.5-11.el7
                  old:
              gcc:
                  ----------
                  new:
                      4.8.5-11.el7
                  old:
              glibc-devel:
                  ----------
                  new:
                      2.17-157.el7
                  old:
              glibc-headers:
                  ----------
                  new:
                      2.17-157.el7
                  old:
              kernel-headers:
                  ----------
                  new:
                      3.10.0-514.el7
                  old:
              libmpc:
                  ----------
                  new:
                      1.0.1-3.el7
                  old:
              mpfr:
                  ----------
                  new:
                      3.1.1-4.el7
                  old:
              pcre-devel:
                  ----------
                  new:
                      8.32-15.el7_2.1
                  old:
              zlib-devel:
                  ----------
                  new:
                      1.2.7-17.el7
                  old:
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.15.8.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.15.8.tar.gz updated
     Started: 09:20:59.134417
    Duration: 166.688 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

Summary for server3
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time:  14.533 s

3上有包了,依赖性也安转完成
[root@server3 salt]# cd /mnt/
[root@server3 mnt]# ls
hostinfo  nginx-1.15.8.tar.gz

自动化部署--SaltStack_第8张图片

2.开始解压并编译
server1

[root@server1 nginx]# cat install.sls 
nginx-install:
  pkg.installed:
    - pkgs:
      - gcc
      - make
      - pcre-devel
      - zlib-devel

  file.managed:
    - name: /mnt/nginx-1.15.8.tar.gz
    - source: salt://nginx/files/nginx-1.15.8.tar.gz

  cmd.run:
    - name: cd /mnt && tar zxf nginx-1.15.8.tar.gz && cd nginx-1.15.8 && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx &> /dev/null && make &> /dev/null && make install &> /dev/null
    - creates: /usr/local/nginx


[root@server1 nginx]# salt server3 state.sls nginx.install
server3:
----------
          ID: nginx-install
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 09:25:13.289170
    Duration: 416.619 ms
     Changes:   
----------
          ID: nginx-install
    Function: file.managed
        Name: /mnt/nginx-1.15.8.tar.gz
      Result: True
     Comment: File /mnt/nginx-1.15.8.tar.gz is in the correct state
     Started: 09:25:13.707731
    Duration: 30.676 ms
     Changes:   
----------
          ID: nginx-install
    Function: cmd.run
        Name: cd /mnt && tar zxf nginx-1.15.8.tar.gz && cd nginx-1.15.8 && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx &> /dev/null && make &> /dev/null && make install &> /dev/null
      Result: True
     Comment: Command "cd /mnt && tar zxf nginx-1.15.8.tar.gz && cd nginx-1.15.8 && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx &> /dev/null && make &> /dev/null && make install &> /dev/null" run
     Started: 09:25:13.781866
    Duration: 12966.872 ms
     Changes:   
              ----------
              pid:
                  2607
              retcode:
                  0
              stderr:
              stdout:

Summary for server3
------------
Succeeded: 3 (changed=1)
Failed:    0
------------
Total states run:     3
Total run time:  13.414 s

自动化部署--SaltStack_第9张图片
server3

编译完成
[root@server3 nginx-1.15.8]# du -h /usr/local/nginx/
796K	/usr/local/nginx/sbin
68K	/usr/local/nginx/conf
0	/usr/local/nginx/logs
8.0K	/usr/local/nginx/html
872K	/usr/local/nginx/
[root@server3 nginx-1.15.8]# du -sh /usr/local/nginx/
872K	/usr/local/nginx/

3.配置nginx的配置文件

[root@server3 nginx-1.15.8]# scp /usr/local/nginx/conf/nginx.conf server1:/srv/salt/nginx/files
[root@server1 nginx]# vim service.sls
[root@server1 nginx]# cat service.sls
include:
  - nginx.install

nginx-service:
  file.managed:
    - name: /usr/local/nginx/conf/nginx.conf
    - source: salt://nginx/files/nginx.conf

4.给nginx可以使用systemctl命令

[kiosk@foundation68 0608]$ cat nginx.service 
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target


[kiosk@foundation68 0608]$ scp nginx.service [email protected]:/etc/systemd/system
[root@server3 system]# systemctl daemon-reload
[root@server3 ~]# systemctl start nginx


测试成功后
[root@server3 system]# scp nginx.service server1:/srv/salt/nginx/files

[root@server1 nginx]# vim service.sls
[root@server1 nginx]# cat service.sls 
include:
  - nginx.install

/usr/local/nginx/conf/nginx.conf:
  file.managed:
    - source: salt://nginx/files/nginx.conf

nginx-service:
  file.managed:
    - name: /etc/systemd/system/nginx.service
    - source: salt://nginx/files/nginx.service

  service.running:
    - name: nginx
    - enable: true
    - reload: true
    - watch:
      - file: /usr/local/nginx/conf/nginx.conf
[root@server1 nginx]# salt server3 state.sls nginx.service

测试:

[root@server3 system]# ps ax
[root@server3 system]# curl localhost

5.根据cpu核数自动创建响应worker_processes个数(更改配置文件)

[root@server1 files]# pwd
/srv/salt/nginx/files
[root@server1 files]# vim nginx.conf
worker_processes  auto				##根据cpu核数自动创建响应worker_processes个数
[root@server1 nginx]# salt server3 state.sls nginx.service

自动化部署--SaltStack_第10张图片
测试:

[root@server3 system]# ps ax    查看worker_processes个数
分开执行,给server2和server3执行不同的sls
[root@server1 salt]# pwd
/srv/sal
[root@server1 salt]# vim top.sls
[root@server1 salt]# cat top.sls 
base:
  'server2':
    - apache.install
  'server3':
    - nginx.service

[root@server1 salt]# ls
apache  nginx  top.sls
[root@server1 salt]# salt '*' state.highstate			##salt的高级state用法
保留关键字进行匹配
[root@server1 salt]# salt '*' grains.items

[root@server1 salt]# salt server2 grains.item os
server2:
    ----------
    os:
        RedHat
[root@server1 salt]# salt server2 grains.item shell
server2:
    ----------
    shell:
        /bin/sh

角色

方法一:改配置文件
[root@server2 base]# cd /etc/salt/
[root@server2 salt]# vim minion
grains:
  roles:
    - apache
[root@server2 salt]# systemctl restart salt-minion
[root@server1 salt]# salt '*' grains.item roles
server2:
    ----------
    roles:
        - apache
server3:
    ----------
    roles:

自动化部署--SaltStack_第11张图片

方法二:写文本
[root@server3 salt]# pwd
/etc/salt
[root@server3 salt]# vim grains
[root@server3 salt]# cat grains 
roles: nginx
[root@server3 salt]# systemctl restart salt-minion
[root@server1 salt]# salt server3 saltutil.sync_grains
[root@server1 salt]# salt '*' grains.item roles
server2:
    ----------
    roles:
        - apache
server3:
    ----------
    roles:
        nginx
方法三
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# ls
apache  nginx  top.sls
[root@server1 salt]# mkdir _grains
[root@server1 salt]# cd _grains/
[root@server1 _grains]# vim my_grains.py
[root@server1 _grains]# cat my_grains.py 
#!/usr/bin/env python
def my_grains():
    grains = {'foo':' bar', 'hello': 'world'}
    grains['salt'] = 'stack'
    return grains
[root@server1 _grains]# salt '*' saltutil.sync_grains
server2:
    - grains.my_grains
server3:
    - grains.my_grains
[root@server2 salt]# cd /var/cache/salt/minion/files/base/
[root@server2 base]# ls
apache  _grains  top.sls
[root@server2 base]# cd _grains/
[root@server2 _grains]# ls
my_grains.py
[root@server2 _grains]# cat my_grains.py 
#!/usr/bin/env python
def my_grains():
    grains = {'foo':' bar', 'hello': 'world'}
    grains['salt'] = 'stack'
    return grains

[root@server1 _grains]# salt '*' grains.item hello
server2:
    ----------
    hello:
        world
server3:
    ----------
    hello:
        world
[root@server1 _grains]# salt '*' grains.item salt
server2:
    ----------
    salt:
        stack
server3:
    ----------
    salt:
        stack

自动化部署--SaltStack_第12张图片

自动化部署--SaltStack_第13张图片

设置变量
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# cat top.sls 
base:
  'server2':
    - apache.install
  'server3':
    - nginx.service
[root@server1 salt]# salt -G 'roles:apache' test.ping
server2:
    True
[root@server1 salt]# salt -G 'roles:nginx' test.ping
server3:
    True
[root@server1 salt]# salt -G 'salt:stack' test.ping
server3:
    True
server2:
    True
[root@server1 salt]# vim top.sls 
[root@server1 salt]# cat top.sls 
base:
  'roles:apache':
    - match: grain
    - apache.install
  'roles:nginx':
    - match: grain
    - nginx.service
[root@server1 salt]# salt '*' state.highstate
[root@server1 salt]# salt server2 grains.item os
server2:
    ----------
    os:
        RedHat
[root@server1 salt]# salt server2 grains.item fqdn
server2:
    ----------
    fqdn:
        server2
[root@server1 salt]# salt server2 grains.item hostname
server2:
    ----------
    hostname:

自动化部署--SaltStack_第14张图片

自动化部署--SaltStack_第15张图片

变量根文件目录
[root@server1 salt]# vim /etc/salt/master
 844 pillar_roots:
 845   base:
 846     - /srv/pillar

自动化部署--SaltStack_第16张图片

[root@server1 salt]# mkdir /srv/pillar
[root@server1 salt]# systemctl restart salt-master
[root@server1 salt]# cd /srv/pillar/
[root@server1 pillar]# mkdir web
[root@server1 pillar]# cat top.sls 
base:
  '*':
    - web.vars

[root@server1 pillar]# cd web/
[root@server1 web]# cat vars.sls 
{% if grains['fqdn'] == 'server2' %}
webserver: httpd
state: master
{% elif grains['fqdn'] == 'server3' %}
webserver: nginx
state: backup
{% endif %}


[root@server1 pillar]# salt '*' pillar.items
server3:
    ----------
    state:
        backup
    webserver:
        nginx
server2:
    ----------
    state:
        master
    webserver:
        httpd



[root@server1 pillar]# salt '*' saltutil.refresh_pillar
server3:
    True
server2:
    True
[root@server1 pillar]# salt -I 'state:master' test.ping
server2:
    True
[root@server1 pillar]# salt -I 'state:backup' test.ping
server3:
    True
JINJA语法
[root@server1 apache]# pwd
/srv/salt/apache
[root@server1 apache]# cat install.sls 
httpd:
  pkg.installed:
    - pkgs:
      - httpd
      - php
      - httpd-tools

  service.running:
    - name: httpd
    - enable: true
    - reload: true
    - watch:
      - file: /etc/httpd/conf/http.conf

/etc/httpd/conf/http.conf:
  file.managed:
    - source: salt://apache/files/httpd.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - context:
        port: 80


[root@server1 apache]# cd files/
[root@server1 files]# vim httpd.conf
Listen {{ port }}
[root@server1 files]# salt server2 state.sls apache.install

[root@server1 apache]# cat install.sls
- template: jinja
    - context:
        port: 80
        host: 172.25.68.2
[root@server1 files]# vim httpd.conf
Listen {{ host }}:{{ port }}

[root@server1 files]# salt server2 state.sls apache.install
+++ 
                  @@ -39,7 +39,7 @@
                   # prevent Apache from glomming onto all bound IP addresses.
                   #
                   #Listen 12.34.56.78:80
                  -Listen 80
                  +Listen 172.25.68.2:80
                   
                   #
                   # Dynamic Shared Object (DSO) Support





[root@server1 files]# salt server2 grains.item ipv4
server2:
    ----------
    ipv4:
        - 127.0.0.1
        - 172.25.68.2
[root@server1 files]# vim httpd.conf
Listen {{ grains['ipv4'][1] }}:{{ port }}

你可能感兴趣的:(自动化部署--SaltStack)