saltstack的深刻-再次理解state的基本用法html
1、基礎環境 一、在tvm-saltmaster的基礎上操做。 二、網絡: eth0:host-only(用於虛擬內網,手動固定IP,這樣從宿主機能夠直接鏈接到這個vm) eth1:NAT(用於上外網,動態IP) [root@tvm-saltmaster ~]# cd /etc/sysconfig/network-scripts/ [root@tvm-saltmaster network-scripts]# cat ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=192.168.56.253 PREFIX=24 GATEWAY=192.168.56.1 DNS1=192.168.56.254 [root@tvm-saltmaster network-scripts]# cat ifcfg-eth1 DEVICE=eth1 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=dhcp DNS1=192.168.56.254 2、salt-master大體上是啥樣的 一、基本結構 [root@tvm-saltmaster salt]# tree /srv/ /srv/ ├── pillar │ ├── job │ │ └── init.sls │ ├── package │ │ └── init.sls │ └── top.sls └── salt ├── base │ ├── abc │ │ ├── hosts.sls │ │ ├── init.sls │ │ ├── locale.sls │ │ ├── ntp.sls │ │ ├── packages.sls │ │ ├── resolv.sls │ │ └── yum.sls │ ├── conf.d │ │ ├── dnsmasq │ │ │ └── office.conf │ │ │ ├── locale │ │ │ │ ├── ntp.conf │ │ │ │ ├── README.txt │ │ │ │ ├── sysconfig_i18n │ │ │ │ ├── sysconfig_ntpd │ │ │ │ └── tpl.ntp.conf │ │ ├── monit │ │ │ ├── monit-mail.conf │ │ │ └── salt-minion.conf │ │ ├── resolv │ │ │ ├── client.conf │ │ │ └── server.conf │ │ ├── ssh │ │ │ └── sshd_config │ │ ├── vim │ │ │ └── vimrc │ │ └── yum │ │ └── local-office.repo │ ├── crontab │ │ └── init.sls │ ├── dnsmasq │ │ └── init.sls │ ├── monit │ │ └── init.sls │ ├── postfix │ │ └── init.sls │ ├── README.TXT │ ├── salt │ │ └── minion.sls │ ├── ssh │ │ └── init.sls │ ├── top.sls │ ├── vim │ │ └── init.sls │ └── web │ └── init.sls ├── dev │ ├── top.sls │ └── web.sls ├── prod │ ├── top.sls │ └── web.sls └── qa ├── conf.d │ └── crontab │ └── client.conf ├── crontab │ └── init.sls └── top.sls 27 directories, 33 files 注意: 1)/srv/salt & /srv/pillar是安裝完成後默認的路徑 2)/srv/salt 下面粉了4個環境,這個是測試多環境的意圖,具體配置是在這裏定義的: [root@tvm-saltmaster salt]# cat /etc/salt/master.d/file_roots.conf # Master file_roots configuration: file_roots: base: - /srv/salt/base dev: - /srv/salt/dev qa: - /srv/salt/qa prod: - /srv/salt/prod [root@tvm-saltmaster salt]# mkdir /srv/salt/{base,dev,qa,prod}/ -p [root@tvm-saltmaster salt]# service salt-master restart 3)默認是在base環境下,這是不可缺的。以base環境爲根,咱們再繼續往下看 4)【top.sls】,這個是一個環境的入口,能夠匹配target,指定以「.sls」爲後綴來定義的salt state文件,例如: [root@tvm-saltmaster salt]# cat base/top.sls base: 'tvm-yum': - dnsmasq - crontab - web '*': - abc - monit - postfix - salt.minion - ssh - vim 上面指定了dnsmasq,其實對應的就是/srv/salt/base/dnsmasq.sls,但咱們改進成經過目錄來管理,所以變成: /srv/salt/base/dnsmasq/init.sls,所以咱們獲得6)的經驗 5)【init.sls】,這個特殊的sls會繼承當前目錄的名稱 6)一般是用多級目錄來分類管理sls文件,例如: salt.minion,對應的是:/srv/salt/base/salt/minion.sls 依此類推,還能夠劃分的更細。 二、如何讓minion的state變成咱們指望的那樣呢? 1)首先,咱們知道能夠執行指定的模塊.函數來執行salt命令 salt 'tvm-test' cmd.run 'hostname' 2)其實執行state也是相似的。指定了target主機爲:'tvm-test' ,再指定一個sls文件「web.sls」,默認的saltenv='base' salt 'tvm-test' state.sls web 3)指定一個其餘的saltenv salt 'tvm-test' state.sls crontab saltenv='qa' 4)測試開關 salt 'tvm-test' state.sls crontab saltenv='qa' test=True 5)highstate方式,minions從salt-master拉取本身匹配的全部的state數據,並執行 salt '*' state.highstate 6)salt執行後的輸出太多怎麼辦? [root@tvm-saltmaster base]# salt '*' state.highstate --out-file=/tmp/salt.log 強制將輸出重定向到文件,默認是在cat時帶有顏色顯示。能夠對比一下,用>/tmp/salt.log來輸出是沒有顏色顯示的。 3、/srv/pillar pillar常常拿來和grains作對比。官網也有介紹: Grains and Pillar are sometimes confused, just remember that Grains are data about a minion which is stored or generated from the minion. This is why information like the OS and CPU type are found in Grains. Pillar is information about a minion or many minions stored or generated on the Salt Master. 後者(grains)記錄了minions中包括了os,cpu等相對靜態的k/v鍵值對的數據,一般是來自salt-minion上報的信息。 前者(pillar)我們能夠定義一些自定義的參數供salt的sls文件來引用,這個一般是在salt-master上爲minions而定義的。 一、入口文件 [root@tvm-saltmaster salt]# cat /srv/pillar/top.sls base: '*': - package - job 二、針對安裝包,指定vim和apache的k/v鍵值對 [root@tvm-saltmaster salt]# cat /srv/pillar/package/init.sls pkgs: {% if grains['os_family'] == 'RedHat' %} vim: vim-enhanced apache: httpd {% elif grains['os_family'] == 'Debian' %} vim: vim apache: apache2 {% elif grains['os'] == 'Arch' %} vim: vim apache: httpd {% endif %} 三、針對highstate,定義一個schedule [root@tvm-saltmaster salt]# cat /srv/pillar/job/init.sls schedule: highstate: function: state.highstate minutes: 2 4、/srv/salt 這裏定義了一些state文件,重點是「base」這個環境,其次是在「qa」這個環境作一些測試的工做。 一、首先,咱們保證「tvm-yum」上安裝的dnsmasq,crontab和web中的服務是符合預期 1)配置dnsmasq服務的狀態是:正在運行、開機啓動,配置文件變得能夠重啓服務。 這裏用到了:「pkg.installed, service.running, file.replace」 -------------------------------------------------------------dnsmasq [root@tvm-saltmaster salt]# cat base/dnsmasq/init.sls dnsmasq: pkg.installed: [] service.running: - enable: True - restart: True - watch: - file: /etc/dnsmasq.d/office.conf - file: /etc/dnsmasq.conf /etc/dnsmasq.d/office.conf: file.managed: - source: salt://conf.d/dnsmasq/office.conf /etc/dnsmasq.conf: file.replace: - pattern: '#addn-hosts=/etc/banner_add_hosts' - repl: 'addn-hosts=/etc/dnsmasq.d/office.conf' -------------------------------------------------------------dnsmasq end 2)配置crontab服務的狀態是:定時執行指定的腳本。 這裏用到了:「cron.present」 -------------------------------------------------------------crontab [root@tvm-saltmaster salt]# cat base/crontab/init.sls ## 使用cron.present這個方法來控制,默認是追加到現有的crontab中 crontab-REPO-UPDATE: cron.present: - identifier: CRON-REPO-UPDATE - name: '/bin/bash /data/ops/bin/repo_update.sh >/tmp/repo_update.log 2>&1 &' - user: root - minute: '0' - hour: '12' - daymonth: '*' - month: '*' - dayweek: '*' -------------------------------------------------------------crontab end 3)配置web服務的狀態是:正在運行和開機啓動。(固然,此處的處理簡略粗糙) 這裏用到了:「pkg.installed, service.running, pillar」 -------------------------------------------------------------web [root@tvm-saltmaster salt]# cat base/web/init.sls apache: pkg.installed: - name: {{ pillar['pkgs']['apache'] }} service.running: - name: {{ pillar['pkgs']['apache'] }} - enable: True - require: - pkg: apache -------------------------------------------------------------web end 二、接着,咱們在「abc」中列出了基本的安裝包和域名解析相關的配置文件,指望的定位是:在主機上線前固定不變的一些初始化操做。 1)「init.sls」用「include」來包含了幾個分類的狀態文件。 這裏用到了:「include」 -------------------------------------------------------------abc init [root@tvm-saltmaster salt]# cat base/abc/init.sls include: - abc.hosts - abc.resolv - abc.yum - abc.locale - abc.ntp - abc.packages -------------------------------------------------------------abc init end 2)「hosts.sls」更新了「/etc/hosts」文件。 這裏用到了:「file.append」: -------------------------------------------------------------abc hosts [root@tvm-saltmaster salt]# cat base/abc/hosts.sls /etc/hosts: file.append: - text: - '192.168.56.253 salt-m.office.test' - '192.168.56.254 mirrors.office.test' - "127.0.0.1 {{ grains['id'] }}" -------------------------------------------------------------abc hosts end 3)「resolv.sls」更新了/etc/resolv.conf文件。 這裏用到了:「if..else..endif, grains」 -------------------------------------------------------------abc resolv [root@tvm-saltmaster salt]# cat base/abc/resolv.sls /etc/resolv.conf: file.managed: {% if grains['id'] == 'tvm-yum' %} - source: salt://conf.d/resolv/server.conf {% else %} - source: salt://conf.d/resolv/client.conf {% endif %} -------------------------------------------------------------abc resolv end 4)指定yum源的配置。 這裏用到了:「file.managed」 -------------------------------------------------------------abc yum [root@tvm-saltmaster salt]# cat base/abc/yum.sls /etc/yum.repos.d/local-office.repo: file.managed: - name: /etc/yum.repos.d/local-office.repo - source: salt://conf.d/yum/local-office.repo - mode: 644 -------------------------------------------------------------abc end 5)指定i18n,timezone和ntp這類本地化的配置 這裏用到了:「file.managed, file.copy, pkg.installed, service.running」 -------------------------------------------------------------abc locale [root@tvm-saltmaster salt]# cat base/abc/locale.sls ## update i18n settings # # via pc @ 2015/8/19 /etc/sysconfig/i18n: file.managed: - source: salt://conf.d/locale/sysconfig_i18n -------------------------------------------------------------abc locale end -------------------------------------------------------------abc ntp [root@tvm-saltmaster salt]# cat base/abc/ntp.sls ## use local timezone and ntp settings # # via pc @ 2015/8/19 /etc/localtime: file.copy: - source: /usr/share/zoneinfo/Asia/Shanghai - force: True pkg-ntp-start: pkg.installed: - name: ntp file.managed: - name: /etc/ntp.conf - source: salt://conf.d/locale/ntp.conf - requires: - pkg: ntp service.running: - name: ntpd - enable: True - reload: True - watch: - file: /etc/ntp.conf - require: - pkg: ntp - require_in: - file: /etc/sysconfig/ntpd /etc/sysconfig/ntpd: file.managed: - source: salt://conf.d/locale/sysconfig_ntpd -------------------------------------------------------------abc ntp end 6)指定要安裝的包。 這裏用到了:「pkg.installed, pkgs, pkg.latest」 -------------------------------------------------------------abc packages [root@tvm-saltmaster salt]# cat base/abc/packages.sls ## 此處列出主機上線時須要的軟件包 # common-pkgs: pkg.installed: - pkgs: - lrzsz - wget - curl - rsync - screen - dos2unix - tree - ntp - bind-utils - nc - telnet - git ## 此處列出須要update的軟件包 # up2date-pkgs: pkg.latest: - pkgs: - bash - openssl -------------------------------------------------------------abc packages end 三、最後,給出了一些自定義的配置的示例,例如針對monit,postfix,salt,ssh和vim的配置。 1)配置monit,salt的狀態是:正在運行和開機啓動,並配置禁用了postfix服務。 這裏用到了:「pkg.installed, service.running, file.managed, service.disabled, pillar」 -------------------------------------------------------------monit [root@tvm-saltmaster salt]# cat base/monit/init.sls monit: pkg.installed: [] service.running: - enable: True /etc/monit.d/monit-mail.conf: file.managed: - source: salt://conf.d/monit/monit-mail.conf - require: - pkg: monit /etc/monit.d/salt-minion.conf: file.managed: - source: salt://conf.d/monit/salt-minion.conf - require: - pkg: monit -------------------------------------------------------------monit end -------------------------------------------------------------postfix [root@tvm-saltmaster salt]# cat base/postfix/init.sls postfix: pkg.installed: [] service.disabled: [] -------------------------------------------------------------postfix end -------------------------------------------------------------salt [root@tvm-saltmaster salt]# cat base/salt/minion.sls salt-minion: pkg.installed: [] service.running: - enable: True -------------------------------------------------------------salt end -------------------------------------------------------------ssh [root@tvm-saltmaster salt]# cat base/ssh/init.sls openssh-clients: pkg.installed: [] openssh-server: pkg.installed: [] sshd: service.running: - enable: True - require: - pkg: openssh-clients - pkg: openssh-server - file: /etc/ssh/sshd_config /etc/ssh/sshd_config: file.managed: - source: salt://conf.d/ssh/sshd_config - require: - pkg: openssh-server -------------------------------------------------------------ssh end -------------------------------------------------------------vim [root@tvm-saltmaster salt]# cat base/vim/init.sls vim: pkg.installed: - name: {{ pillar['pkgs']['vim'] }} /root/.vimrc: file.managed: - source: salt://conf.d/vim/vimrc - require: - pkg: vim -------------------------------------------------------------vim end 四、中間的2個環境略過,再看看qa環境的配置. 這裏用到了:「cron.file」 -------------------------------------------------------------qa [root@tvm-saltmaster salt]# cat qa/top.sls qa: '* and not tvm-yum': - crontab [root@tvm-saltmaster salt]# cat qa/crontab/init.sls ## 使用cron.file這個方法來控制,能夠替換所有的crontab內容 cron-ntpdate-office: cron.file: - name: salt://conf.d/crontab/client.conf -------------------------------------------------------------qa end 要注意,這裏是用「name」,而不是「source」來指定文件路徑。 5、小結一下state 一、state.sls, state.highstate 二、pkg pkg.installed pkg.latest pkgs 三、file file.managed file.replace file.append file.copy 四、service service.running service.disabled 五、cron cron.present cron.file 六、grains 七、pillars 八、include ZYXW、參考 一、官網doc http://docs.saltstack.com/en/latest/topics/tutorials/pillar.html http://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.state.html http://docs.saltstack.com/en/latest/ref/states/requisites.html http://docs.saltstack.com/en/latest/ref/states/all/salt.states.cron.html http://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html http://docs.saltstack.com/en/latest/ref/states/all/salt.states.pkg.html http://docs.saltstack.com/en/latest/ref/states/all/salt.states.service.html