101: 自動化運維saltstack ansible

自動運維化工具:saltstack    ansible:python

傳統運維效率低,大多工做人爲完成;
傳統運維工做繁瑣,容易出錯;
傳統運維每日重複作相同的事情;
傳統運維沒有標準化流程;
傳統運維的腳本繁多,不能方便管理;
自動化運維就是要解決上面全部問題;git

經常使用的自動化運維工具:適用於十多臺    上百臺     上千臺等;github

puppet:    www.puppetlabs.com
基於rubby開發,C/S架構,支持多平臺,可管理配置文件,用戶,crond任務,軟件包,系統服務等,分爲社區版和企業版(收費,可是支持圖形化);web

saltstack:https://saltstack.com   文檔:docs.saltstack.comshell

基於python開發, C/S架構,多平臺,比puppet輕量,在遠程執行命令時很是快捷(由於它有消息隊列,它是並行的,因此快,如expect是串行,一個一個執行,比較慢),配置和使用puppet容易多了,能實習puppet的全部功能;apache

ansible:www.ansible.comcentos

基於python開發,更加方便簡潔的自動化運維工具,不須要在客戶端安裝anget,能夠實現批量操做系統配置,批量程序部署,批量運行命令;bash

註釋:saltstack與ansible相比,saltstack支持的機器更多,ansible是經過祕鑰認證的方式來執行後面的命令,更改文件,安裝服務呀;架構

一、ansible:  不須要安裝客戶端,經過sshd通訊,能夠基於模塊工做,支持命令行操做,也支持playbook,也支持UI WEB界面(收費的),運維

ansible被redhat公司收購,它在github的地址:htts://guhub.com/ansible/ansible

入門電子書:https://ansible-book.gitbooks.io/ansible-first-book/

安裝:準備兩臺機器001(129)  和002(130)     03(131)

只須要在001上安裝ansible就能夠了:

固然也能夠看到Centos自帶的源裏面的ansible的版本;    yum    list|grep    ansible

[root@localhost_001 ~]# yum list|grep ansible
ansible.noarch                            2.7.2-1.el7                  epel     
ansible-doc.noarch                        2.7.2-1.el7                  epel

1:安裝:  yum    install   -y    ansible    ansible-doc

[root@localhost_001 ~]# yum install -y ansible  ansible-doc
已加載插件:fastestmirror

2:在001(129)機器上生成祕鑰對,使用ssh-keygen  -t  rsa  ,而後把公鑰id_rs.pub放到002(130)機器和03(131)/root/.ssh/authorized_keys上了。

[root@localhost_001 ~]# ls /root/.ssh/
authorized_keys  id_rsa           id_rsa.pub       known_hosts
[root@localhost_001 ~]# cat /root/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7G1C6L20UA+jxG+2Umvx0KXex9xJNybaBXy6v1FiMA8xZpOzBd8++nndtNX8IpyiwaNls9l3LMUIn60WLMPuWOK91EpAVgUMHjtPQkPzB2qTb7ntg5GfOrRRCz+in96Z4cxhMHUh28gqsous83G0zaNI8XQ5RQIeUf0fIZ+9fxt/e4jIdmyf/01Ia96bW6rKQT6bWAXrOKQO5JhhG9u4GwYIsWJPkG+D4Mxa+Yah0ynTksORlBAsGmHz2vhbJQXhPkhs/XUUTw9lyjbt4cImj69TtZZdFWNAD4SBL+fXItr44v2KNasgBmBAOXJmdJg+NnfeAcwahm0B1p4BIIKMj root@localhost_001

註釋:如上圖,個人機器已經生成了,而後複製到002機器的/root/.ssh/authorzed_keys

2:守在001(129)機器上寫入到/etc/hosts文件,用於在後面ansible的hosts定義;以下;

[root@localhost_001 ~]# cat /etc/hosts
127.0.0.1   localhost_001 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.149.130  localhost_002
192.168.149.131  localhost_03

3:測試是否能夠遠程到002(130) 和03(131)這臺機器上來;

[root@localhost_001 ~]# ssh  localhost_002
Last login: Fri Nov 23 16:43:31 2018 from 192.168.149.129
[root@localhost_002 ~]# exit
登出
Connection to localhost_002 closed.
[root@localhost_001 ~]# ssh  localhost_03
Last login: Fri Nov 23 16:51:30 2018 from 192.168.149.129
[root@localhost_03 ~]# exit
登出
Connection to localhost_03 closed.

4:編輯ansible的hosts文件,配置主機組:    /etc/ansible/hosts

註釋:    能夠分紅多個組,好比web組合db組等;每一組裏有若干個機器,能夠針對某個組去操做了;

[root@localhost_001 ~]# cat /etc/ansible/hosts 
# This is the default ansible 'hosts' file.
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
#本次新增內容:----------------------
[testhost]          
127.0.0.1
localhost_002    #此處也能夠寫IP地址;
[webserver]
localhost_03     #此處也能夠寫IP地址;
#新增內容結束-------------------------------

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

註釋:如上圖示:新增兩個組 [testhost]  和  [webserver],而後裏定要操做的客戶端,能夠寫IP地址,也能夠寫主機名(需提早在/etc/hosts下定義);

註釋:對於001(129)本機,也須要把本身的/root/.ssh/id_rsa.pub複製到/root/.ssh/authorized_keys文件裏面去;

一、ansible遠程執行命令;

ansbile    testhost    -m   command    -a  'hostnamectl'

註釋:ansible  後面跟組的名字(在/etc/ansible/hosts定義),這裏是 testhost 爲主機名;  

-m   後面跟模塊的名字,這裏使用的command模塊;

-a     後面跟的命令,也能夠是 hostname  mv   cp   w 等;

[root@localhost_001 ~]# ansible testhost -m command -a 'hostname'
127.0.0.1 | CHANGED | rc=0 >>
localhost_001
localhost_002 | CHANGED | rc=0 >>
localhost_002
[root@localhost_001 ~]# ansible localhost_03 -m command -a 'w'
localhost_03 | CHANGED | rc=0 >>
 17:18:41 up 38 min,  4 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      1311月18  9days  0.03s  0.03s -bash
root     pts/1    192.168.149.135  16:48   27:45   0.02s  0.02s -bash
root     pts/2    192.168.149.129  17:18    1.00s  0.19s  0.00s w

註釋:如上,針對testhost這個組來執行命令以及針對localhost_03這臺主機來執行命令;

同時還有一個shell模塊,主要使用執行腳本的;命令格式以下;

ansible   192.168.149.132     -m     shell      -a    'w'

二、使用ansible複製和移動目錄及文件;  也能夠針對給一個組來複制;

在001(129)這臺機器上覆制/etc/ansible這個目錄到002(130)這臺機器上;

複製目錄:ansible   localhost_002  -m copy   -a  "src=/etc/ansible   dest=/tmp/ansibletest owner=root  group=root mode=755"

[root@localhost_001 ~]# ansible localhost_002 -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=755"
localhost_002 | CHANGED => {
    "changed": true, 
    "dest": "tmp/ansibletest/", 
    "src": "/etc/ansible"
}

而後在002這臺機器來查看,以下;   

[root@localhost_002 ~]# ls /tmp/ansibletest/
ansible
[root@localhost_002 ~]# ls /tmp/ansibletest/ansible/
ansible.cfg  hosts  roles

註釋:當複製或移動的目錄時,源目錄會放到目標目錄下,當目錄目錄不存在時,則會自動建立,若是存在,則直接放到該目錄下;

複製文件: ansible   localhost_002   -m copy   -a  "src=/etc/passwd  dest=/tmp/passwd  owner=root   group=root   mode=755"

在001(129)這臺機器上操做;

[root@localhost_001 ~]# ansible localhost_002 -m copy  -a "src=/etc/passwd dest=/tmp/passwd owner=root group=root mode=755"
localhost_002 | CHANGED => {
    "changed": true, 
    "checksum": "a1b2385096229bc513afc9af77a36619d1af0f77", 
    "dest": "/tmp/passwd", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "35430c216ac8c7834378501fdfc5e41f", 
    "mode": "0755", 
    "owner": "root", 
    "size": 1309, 
    "src": "/root/.ansible/tmp/ansible-tmp-1542965995.03-104940655542836/source", 
    "state": "file", 
    "uid": 0
}

而後在002(130)這臺機器上查看文件;

[root@localhost_002 ~]# ls /tmp/passwd 
/tmp/passwd

註釋:在拷貝文件時,當目標文件存在,則覆蓋目標文件,至關於重名了,當目標文件不存在,則直接複製;

也能夠自定義目標的文件名;

ansible   localhost_002   -m  copy   -a "src=/etc/passwd   dest=/tmp/1.txt   owner=root group=root   mode=755"

3:ansible遠程執行腳本;

1:在001(129)上寫一個腳本:內容以下;

[root@localhost_001 ~]# cat /tmp/1.sh 
#!/bin/bash
 echo `date` > /tmp/ansible_test.txt

2:把腳本拷貝到三個機器,而後再執行;       -m   copy    -a    "  "

[root@localhost_001 ~]# ansible testhost -m copy -a "src=/tmp/1.sh dest=/tmp/test.sh owner=root group=root mode=777"
[root@localhost_001 ~]# ansible localhost_03 -m copy -a "src=/tmp/1.sh dest=/tmp/test.sh owner=root group=root mode=777"

3:再三臺機器上遠程來執行:    -m   shell   -a  "   "

[root@localhost_001 ~]# ansible testhost -m shell -a "/tmp/test.sh"
localhost_002 | CHANGED | rc=0 >>
127.0.0.1 | CHANGED | rc=0 >>

[root@localhost_001 ~]# ansible localhost_03 -m shell -a "/tmp/test.sh"
localhost_03 | CHANGED | rc=0 >>

4:在另外兩臺機器來查看;

[root@localhost_002 ~]# ls /tmp/ansible_test.txt 
/tmp/ansible_test.txt
[root@localhost_03 ~]# cat /tmp/ansible_test.txt 
2018年 11月 23日 星期五 17:53:44 CST

註釋:在使用  command 模塊時,不支持帶管道的;會報以下錯誤;

[root@localhost_001 ~]# ansible testhost  -m command -a "cat /etc/passwd|wc -l"
127.0.0.1 | FAILED | rc=1 >>
cat:無效選項 -- l
Try 'cat --help' for more information.non-zero return code

而在使用shell模式,支持能夠帶管道符;以下:

[root@localhost_001 ~]# ansible testhost  -m shell -a "cat /etc/passwd|wc -l"
127.0.0.1 | CHANGED | rc=0 >>
28

localhost_002 | CHANGED | rc=0 >>
22

4:ansible管理任務計劃:    用到   cron  模塊;

ansible   testhost   -m  cron  -a "name='test cron'  job='/bin/touch  /tmp/123.txt'  weekday=6"

[root@localhost_001 ~]# ansible testhost -m cron -a "name='test cron' job='/bin/touch  /tmp/123.txt' weekday=6"
localhost_002 | CHANGED => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test cron"
    ]
}
127.0.0.1 | CHANGED => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test cron"
    ]
}

ansible   testhost   -m  cron  -a "name='test cron'  job='/bin/touch  /tmp/123.txt'  weekday=6"

註釋:腳本最後寫  分鐘minute    時 hour     日  day    月  mouth    周 weekday     不定義則模式是  *  

登陸002(130)機器查看;   會註釋表示是ansible ,也就是以前定義的名字;

[root@localhost_002 ~]# crontab -l
#Ansible: test cron
10 * * * * /bin/touch  /tmp/123.txt

刪除cron;          "name='test cron'    state=sbsent"

[root@localhost_001 ~]# ansible testhost -m cron -a "name='test cron' state=absent"
127.0.0.1 | CHANGED => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
localhost_002 | CHANGED => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}

這樣就能夠刪除了;注意:註釋的哪一行不能刪除,否則會沒法操做了;

4:再添加一個cron:星期六十點二十建立/tmp/123.txt腳本;

[root@localhost_001 ~]# ansible webserver -m cron  -a "name='test cron' job='/bin/touch /tmp/123.txt' minute=20  hour=10 weekday=6"
localhost_03 | CHANGED => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test cron"
    ]
}

5:在03(131)上查看;         crontab  -l

[root@localhost_03 ~]# crontab -l
#Ansible: test cron
20 10 * * 6 /bin/touch /tmp/123.txt

註釋:之後在生成環境中用到cron,不要手動去更改,否則就沒法操做了;

6:ansilbe安裝包及管理服務;     用到了  yum   模塊              server   模塊

ansible   webserver   -m  yum   "name=httpd"                                         #安裝httpd服務;

[root@localhost_001 ~]# ansible webserver -m yum -a "name=httpd"
[root@localhost_03 ~]# rpm -qa |grep httpd
httpd-tools-2.4.6-80.el7.centos.1.x86_64
httpd-2.4.6-80.el7.centos.1.x86_64

2:卸載一個包;

ansible   weserver   -m   yum    「name=httpd   state=removed」  

[root@localhost_001 ~]# ansible webserver -m yum -a "name=httpd state=removed"
[root@localhost_03 ~]# rpm -qa |grep httpd

3:啓動httpd服務,須要用到   server   模塊;

[root@localhost_001 ~]# ansible webserver -m service -a "name=httpd state=started enabled=yes"

[root@localhost_03 ~]# ps aux |grep httpd   #查看03(131)的httpd進程;
root      16307  0.1  0.5 226220  5144 ?        Ss   18:36   0:00 /usr/sbin/httpd -DFOREGROUND
apache    16308  0.0  0.3 226220  3016 ?        S    18:36   0:00 /usr/sbin/httpd -DFOREGROUND
apache    16309  0.0  0.3 226220  3016 ?        S    18:36   0:00 /usr/sbin/httpd -DFOREGROUND
apache    16310  0.0  0.3 226220  3016 ?        S    18:36   0:00 /usr/sbin/httpd -DFOREGROUND
apache    16311  0.0  0.3 226220  3016 ?        S    18:36   0:00 /usr/sbin/httpd -DFOREGROUND
apache    16312  0.0  0.3 226220  3016 ?        S    18:36   0:00 /usr/sbin/httpd -DFOREGROUND
root      16322  0.0  0.0    444     4 pts/1    R+   18:36   0:00 grep --color=auto httpd

註釋:列出全部模塊:      ansible-doc   -l

[root@localhost_001 ~]# ansible-doc -l
copy                                            Copies files to remote locations   
yum                                             Manages packages with the `yum' package manager

針對某個模塊查詢:      ansible-doc   -l    cron

 

若是管理100多臺機器,須要一臺一臺的把公鑰放到遠端的機器上,以下兩個方法:

可使用expect腳本批量傳送;

也可使用ansible authorized_keys;

相關文章
相關標籤/搜索