天天看點

ansible

究竟要不要自動化?

首先确認批量管理我們需要什麼:無外乎主機分組管理、實時批量執行指令或腳本、實時批量分發檔案或目錄、定時同步檔案等。

ansible由于走的是ssh,是以它有認證的過程,以及加密碼的過程,這使得ansible非常慢,不适用于大規模環境(指上千台)。

4. 比對目标

ansible <pattern_goes_here> -m <module_name> -a <arguments>

ansible  qing   -m service -a "name=httpd state=restart"

<pattern_goes_here> 參數的用法

192.168.4.34 或www.qing.com  比對目标位址或主機名,多個ip或主機名使用‘:’号分隔

qing                         比對目标組為qing, 多個組使用用":" 分隔

all 或 "*"                   比對目标所有主機

~(web|db).*\.example\.com 或 192.168.1.*   支援正規表達式比對主機或ip位址

qing:!192.168.4.34         比對目标組qing 但是配置192.168.4.34的主機ip

qing:&ming                  比對qing和ming 兩個群組的交集

qing:!`excluded`:&`required`  支援變量比對方式

5. ansible 常用子產品及api

0. 預設子產品名為command 是以 -m command 可以省略  例: ansible qing -a 'ls /tmp'

1. ansible qing -m ping   檢測主機狀态

2. 子產品預設存儲目錄:/usr/share/ansible/

3. ansible-doc command  可以檢視子產品使用幫助

4. 常用子產品介紹:

   1. 遠端指令子產品

      1.1 功能  command 作為ansible的預設子產品,可以運作遠端權限範圍内的所有shell指令   ansible qing -m command -a 'free -m'

                script 功能是在遠端主機執行主要端存儲的shell腳本檔案,相當于scp+shell 組合   ansible qing -m script -a '/tmp/qing.sh'

                shell  執行遠端主機的shell腳本檔案    ansible qing -m shell -a '/tmp/ming.sh' 

   2. copy子產品

      ansible qing -m copy -a 'src=/tmp/fanqing.txt dest=/qing/ owner=root group=root mode=0755'

      copy 控制主機下/tmp/fanqing.txt 到目标主機/qing/下 并改變使用者、組、權限

   3. state子產品

      ansible qing -m stat -a 'path=/qing/fanqing.txt'

      擷取檔案狀态資訊

   4. get_url子產品

      ansible qing -m get_url -a 'url=http://www.baidu.com dest=/qing/index.html mode=0440 force=yes'

   5. yum子產品

      ansible 192.168.4.34 -m yum -a 'name=httpd state=latest'

      yum安裝軟體包

   6. cron子產品

      ansible 192.168.4.34 -m cron -a "name='check dirs' hour='5,2' job='ls -alh > /dev/null'"

      結果:#ansible: check dirs

             * 5,2 * * * ls -alh > /dev/null 

   7. mount

     ansible 192.168.4.34 -m mount -a 'name=/mnt/data src=/dev/sd0 fstype=ext3 opts=ro state=present'

   8. service 子產品

      ansible qing -m service -a 'name=httpd state=started'

      state參數:started,stopped,restarted,reloaded

   9. user子產品

      ansible qing -m service -a 'name=qing shell=/bin/bash groups=admins,developers append=yes'

6. playbook

handlers

tasks

templates

繼續閱讀