天天看點

1.1 salt 常用指令以及 grain pillar

Saltstack 常用指令

salt 'slaver.test.com' cp.get_file salt://apache.sls /tmp/cp.txt

salt 'slaver.test.com' cp.get_dir salt://test /tmp

salt-run manage.up

salt 'slaver.test.com' cmd.script salt://test/shell.sh

顯示存活的用戶端

salt

# 更新所有的minion的狀态

salt '*' state.highstate

salt-call '192.168.0.100' state.sls webserver

調試Salt

#開啟debug日志

salt-minion -l debug

#設定預設逾時

salt '*' state.highstate -t 60

salt '*' state.sls apache

使用salt對所有的機器使用state這個子產品下的sls方法來執行apache這個狀态子產品

使用進階狀态 有一個入口檔案預設是top.sls

Grains  and    Pillar

Grains裡面收集了minion啟動時候的所有系統資訊,存儲在minion端。靜态資料,隻有重新開機的時候才重新收集

salt 'linux-node1*' grains.ls

主機比對

salt -G roles:cgt cmd.run 'uptime'

如果你覺得寫在配置檔案中不友善,可以寫在他的一個預設檔案中

vim /etc/salt/grains

web:nginx

在top.sls中比對使用

[root@linux-node1 salt]# cat /srv/salt/top.sls 

base:

  'os:CentOS':

    - match: grain

    - apache

[root@linux-node1 salt]# salt '*' state.highstate

Pillar:給minion指定它想要的資料

# 在服務端開啟pillar

[root@linux-node1 ~]# vim /etc/salt/master

529 pillar_roots:

530   base:

531     - /srv/pillar

552 pillar_opts: False

cat  /srv/pillar/apache.sls

{%if grains['os'] == 'CentOS' %}

apache: httpd

{% elif grains['os'] == 'Debian' %}

apache: apache2

{% endif %}

# 告訴pillar下面哪些主機可以來使用apache.sls檔案,這是所有主機*

[root@linux-node1 ~]#  cat /srv/pillar/top.sls

  '*':

salt '*' pillar.items

給定标簽後

對apache:httpd的minion執行指令

# 重新整理pillar,grains是需要重新開機minion端,pillar是需要重新整理

salt '*' saltutil.refresh_pillar

salt -I 'apache:httpd' test.ping

建立使用者,且不允許登入

[root@linux-node1 prod]# mkdir user

[root@linux-node1 prod]# cd user/

[root@linux-node1 user]# vim www.sls

www-user-group:

  group.present:

    - name: www

    - gid: 1000

  user.present:

    - fullname: www

    - shell: /sbin/nologin

    - uid: 1000

本文轉自 liqius 51CTO部落格,原文連結:http://blog.51cto.com/szgb17/1915108,如需轉載請自行聯系原作者

繼續閱讀