標籤:saltstack cmd grains pkg file
saltstack的探索-安裝vim、nginx服務和配置、自訂指令碼
1. 配置minion首先,移除原來的測試minion[[email protected] bin]# salt-key -d test230The following keys are going to be deleted:Accepted Keys:test230Proceed? [N/y] yKey for minion test230 deleted.新增minion:【svr205-2】[[email protected] ~]# yum install salt-minion -y[[email protected] ~]# vim /etc/salt/minionmaster: 10.0.200.21id: svr205-2[[email protected] ~]# service salt-minion startStarting salt-minion daemon: [ OK ]【svr205-2, 3, 4, 5類似】同上2. 配置master和minion的key[[email protected] bin]# salt-key -LAccepted Keys:Unaccepted Keys:svr205-2svr205-3svr205-4svr205-5Rejected Keys:[[email protected] bin]# salt-key -a svr205*The following keys are going to be accepted:Unaccepted Keys:svr205-2svr205-3svr205-4svr205-5Proceed? [n/Y] yKey for minion svr205-2 accepted.Key for minion svr205-3 accepted.[[email protected] bin]# salt-key -LAccepted Keys:svr205-2svr205-3svr205-4svr205-5Unaccepted Keys:Rejected Keys:測試:[[email protected] salt]# salt ‘svr205-*‘ test.pingsvr205-3: Truesvr205-2: Truesvr205-5: Truesvr205-4: True3. 組態管理-同步vim配置[[email protected] salt]# pwd/srv/salt[[email protected] salt]# cat edit/vim.sls vim-enhanced: pkg.installed: []/root/.vimrc: file.managed: - source: salt://edit/conf/vimrc - mode: 644 - uesr: root - group: root[[email protected] salt]# ls edit/conf/vimrc[[email protected] salt]# salt ‘svr205-*‘ state.sls edit.vim##########################################################################略##########################################################################4. 遠程執行-在4台主機上執行指令碼lvs-realsvr,並加入開機啟動。啟動指令碼:[[email protected] salt]# cat lvs/real/start.sls /etc/rc.d/init.d/lvs-realsvr: file.managed: - source: salt://lvs/bin/lvs-realsvr.sh - mode: 755lvs-realsvr: cmd.run: - require: - file: /etc/rc.d/init.d/lvs-realsvr - name: service lvs-realsvr start && chkconfig lvs-realsvr on關閉指令碼:[[email protected] salt]# cat lvs/real/stop.sls /etc/rc.d/init.d/lvs-realsvr: file.managed: - source: salt://lvs/bin/lvs-realsvr.sh - mode: 755lvs-realsvr: cmd.run: - require: - file: /etc/rc.d/init.d/lvs-realsvr - name: service lvs-realsvr stop && chkconfig lvs-realsvr off控制指令碼:[[email protected] salt]# cat lvs/bin/lvs-realsvr.sh #!/bin/bash# # 2015/2/28# lvs real server## chkconfig: - 85 15# description: control vip on lvs realserver # Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0lockfile="/var/lock/subsys/lvs-real" s_vip=‘10.0.205.100‘start() { ifconfig lo:1 ${s_vip} netmask 255.255.255.255 broadcast ${s_vip} route add -host ${s_vip} dev lo:1 echo 1 >/proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 >/proc/sys/net/ipv4/conf/lo/arp_announce echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore echo 2 >/proc/sys/net/ipv4/conf/all/arp_announce retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval}stop() { echo 0 >/proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 >/proc/sys/net/ipv4/conf/lo/arp_announce echo 0 >/proc/sys/net/ipv4/conf/all/arp_ignore echo 0 >/proc/sys/net/ipv4/conf/all/arp_announce ifconfig lo:1 down route del -host ${s_vip} retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval}status() { ip a |grep inet |grep -v inet6 }case $1 in start) start status ;; stop) stop status ;; status) status ;; *) echo $"Usage: $0 {start|stop|status}" exit 2 esac 執行:[[email protected] salt]# salt ‘svr205-*‘ state.sls lvs.real.start##########################################################################略##########################################################################查看minion的狀態:【svr205-2】[[email protected] ~]# ll /etc/init.d/lvs-realsvr -rwxr-xr-x 1 root root 1252 Feb 28 05:31 /etc/init.d/lvs-realsvr[[email protected] ~]# ip a |grep inet |grep -v inet6 inet 127.0.0.1/8 scope host lo inet 10.0.205.100/32 brd 10.0.205.100 scope global lo:1 inet 10.0.205.2/8 brd 10.255.255.255 scope global eth0 [[email protected] salt]# salt ‘svr205-*‘ state.sls lvs.real.stop##########################################################################略##########################################################################查看minion的狀態:【svr205-2】[[email protected] ~]# ip a |grep inet |grep -v inet6 inet 127.0.0.1/8 scope host lo inet 10.0.205.2/8 brd 10.255.255.255 scope global eth0 5. 組態管理-安裝nginx服務,並加入開機啟動。[[email protected] salt]# cat nginx/init.sls nginx: pkg.installed: [] service.running: - require: - pkg: nginx cmd.run: - require: - pkg: nginx - name: chkconfig nginx on[[email protected] salt]# salt ‘svr205-*‘ state.sls ##########################################################################略##########################################################################深入一步,使用模版部署自訂的index.html,更新nginx首頁,顯示當前web主機的hostname和ip資訊。[[email protected] salt]# cat nginx/init.sls nginx: pkg.installed: [] service.running: - watch: - file: /usr/share/nginx/html/index.html - require: - pkg: nginx cmd.run: - require: - pkg: nginx - name: chkconfig nginx on/usr/share/nginx/html/index.html: file.managed: - source: salt://nginx/conf/index.html - template: jinja[[email protected] salt]# cat nginx/conf/index.html <p> HOST: {{ grains[‘host‘] }} <br /> <br /><br />{% for k,v in grains.get(‘ip4_interfaces‘).items() %} {{ k }}: {{ v.pop() }} <br />{% endfor %}</p>[[email protected] salt]# salt ‘svr205-*‘ state.sls nginx##########################################################################略##########################################################################查看檔案是否有改變:【svr205-2】[[email protected] ~]# cat /usr/share/nginx/html/index.html <p> HOST: svr205-2 <br /> <br /><br /> lo: 10.0.205.100 <br /> eth0: 10.0.205.2 <br /></p>如上,得到了該主機的hostname和ip資訊,符合預期。
saltstack的探索-安裝vim、nginx服務和配置、自訂指令碼