第二十五節 ansible之文件的批量分發
標簽(空格分隔): Linux實戰教學筆記-陳思齊
---本教學筆記是本人學習和工作生涯中的摘記整理而成,此為初稿(尚有諸多不完善之處),為原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章原始出處,作者信息和本聲明。否則將追究法律責任。http://www.cnblogs.com/chensiqiqi/
第1章 介紹:
python語言是運維人員必會的語言!
ansible是一個基於Python開發的自動化運維工具
ansible的功能實現基於SSH遠程連接服務
ansible可以實現批量系統配置,批量軟件部署,批量文件拷貝,批量運行命令等功能
特點:
1)不需要單獨安裝客戶端,基於系統自帶的sshd服務,sshd就相當於ansible的客戶端
2)不需要服務端
3)需要依靠大量的模塊實現批量管理
4)配置文件/etc/ansible/ansible.cfg
第2章 IP列表
服務器說明 | 外網IP | 內網IP | 主機名 |
---|---|---|---|
apache web | 10.0.0.7/24 | 172.16.1.7/24 | web02 |
nginx web | 10.0.0.8/24 | 172.16.1.8/24 | web01 |
NFS存儲服務器 | 10.0.0.31/24 | 172.16.1.31/24 | nfs01 |
rsync備份服務器 | 10.0.0.41/24 | 172.16.1.41/24 | backup |
管理服務器 | 10.0.0.61/24 | 172.16.1.61/24 | m01 |
第3章 必備准備
實現從管理機m01到其他機器的密鑰認證關系:
3.1 sshpass非交互方式工具使用
請參考http://www.cnblogs.com/chensiqiqi/p/6550221.html《實戰教學筆記第二十四節》
3.2 開始安裝ansible
搭建企業yum倉庫及定制rpm包是自動化運維關鍵內容,在實戰教學筆記第三階段在進行講解。
3.2.1 管理端m01安裝ansible
需要epel.repo源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum -y install ansible
3.2.2 所有被管理端需要安裝:
yum -y install libselinux-python
3.2.3 ansible基礎配置
ansible的配置文件:
[root@m01 ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg #ansible配置文件
├── hosts #被ansible管理的主機名單(分組)
└── roles
1 directory, 2 files
編輯ansible的主機配置文件hosts,添加主機組chensiqi
[root@m01 ~]# cp /etc/ansible/hosts{,.bak} #改前備份可是個好習慣
[root@m01 ~]# tail -6 /etc/ansible/hosts
[chensiqi]
172.16.1.31
172.16.1.41
172.16.1.61
172.16.1.7
172.16.1.8
如果設置了ssh密鑰連接的話,hosts文件到這里就算配置完畢了。但是我們還沒有設置,因此還需要對ansible的主機映射文件/etc/ansible/hosts繼續加工
[root@m01 ~]# tail -6 /etc/ansible/hosts
[chensiqi]
172.16.1.31 ansible_ssh_user=root ansible_ssh_pass=登錄密碼
172.16.1.41 ansible_ssh_user=root ansible_ssh_pass=登錄密碼
172.16.1.61 ansible_ssh_user=root ansible_ssh_pass=登錄密碼
172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=登錄密碼
172.16.1.8 ansible_ssh_user=root ansible_ssh_pass=登錄密碼
命令說明:
ansible_ssh_user:ssh連接的用戶名
ansible_ssh_pass:ssh連接的密碼
注意:
如果沒有做密鑰認證,hosts又沒有如上方式配置的話,ansible進行遠程連接是會失敗的。
3.2.4 利用ansible遠程批量執行命令
語法:
ansible chensiqi -m command -a 'uptime'
ansible 主機組 -m ansible內置功能模塊名 -a 命令
ansible命令測試
示例1:獲取172.16.1.8的主機的w信息
[root@m01 ~]# ansible 172.16.1.8 -m command -a "w"
172.16.1.8 | SUCCESS | rc=0 >>
13:44:07 up 5:32, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - Sat09 14:16 0.07s 0.07s -bash
root pts/0 172.16.1.1 Mon23 13:29m 0.08s 0.00s -bash
root pts/1 m01 13:44 0.00s 0.14s 0.00s /bin/sh -c /usr
[root@m01 ~]#
示例2:獲取整個chensiqi主機組的對應的“w”信息
[root@m01 ~]# ansible chensiqi -m command -a "w"
172.16.1.8 | SUCCESS | rc=0 >>
13:45:12 up 5:33, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - Sat09 15:21 0.07s 0.07s -bash
root pts/0 172.16.1.1 Mon23 13:30m 0.08s 0.00s -bash
root pts/1 m01 13:45 0.00s 0.12s 0.00s /bin/sh -c /usr
172.16.1.61 | SUCCESS | rc=0 >>
21:05:58 up 6:34, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - 14:41 4:22m 0.07s 0.07s -bash
root pts/0 172.16.1.1 16:44 2.00s 1.11s 0.67s /usr/bin/python
root pts/8 m01 21:05 1.00s 0.10s 0.00s /bin/sh -c /usr
172.16.1.7 | SUCCESS | rc=0 >>
12:05:07 up 1:05, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - 11:00 26:21 0.09s 0.09s -bash
root pts/0 m01 12:05 0.00s 0.30s 0.00s /bin/sh -c /usr
172.16.1.41 | SUCCESS | rc=0 >>
22:36:51 up 18:39, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - 03:58 58:54 0.11s 0.11s -bash
root pts/0 m01 22:36 0.00s 0.32s 0.00s /bin/sh -c /usr
172.16.1.31 | SUCCESS | rc=0 >>
13:45:13 up 6:49, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - Mon20 13:42 0.15s 0.15s -bash
root pts/0 m01 13:45 0.00s 0.30s 0.00s /bin/sh -c /usr
[root@m01 ~]#
示例3:調用ansible內置的copy模塊
[root@m01 ~]# ansible 172.16.1.8 -m copy -a "src=/etc/hosts dest=/tmp" #輸入命令
172.16.1.8 | SUCCESS => {
"changed": true,
"checksum": "dba0126bf49ea8d4cdc476828f9edb37085c6afe",
"dest": "/tmp/hosts",
"gid": 0,
"group": "root", #文件復制過去以后的屬組
"md5sum": "09bad48d0c62411850fd04b68f836335",
"mode": "0644", #文件復制過去以后的權限
"owner": "root", #文件復制過去以后的屬主
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 294,
"src": "/root/.ansible/tmp/ansible-tmp-1489411262.1-267125154401179/source",
"state": "file",
"uid": 0
}
[root@m01 ~]# ansible 172.16.1.8 -m command -a "ls /tmp" #查看一下對方主機目錄下有啥
172.16.1.8 | SUCCESS | rc=0 >>
ansible_0qlGau
hosts #在這里呢,拷貝成功
pulse-d3qHAaSjkIhZ
pulse-PbcqlrG9QxEK
virtual-root.yrc60j
yum.log
[root@m01 ~]# ssh root@172.16.1.8 "ls /tmp" #用ssh再看一下
`root@172.16.1.8's password: `
hosts #在這里呢拷貝成功
pulse-d3qHAaSjkIhZ
pulse-PbcqlrG9QxEK
virtual-root.yrc60j
yum.log
[root@m01 ~]#
命令說明:
-m:調用ansible內置模塊 copy 拷貝模塊
-a:接命令。由於調用了copy模塊,命令格式發生改變。src=本地文件路徑 dest=目的地所在路徑
示例4:調用copy模塊實現保存文件的屬性改變
[root@m01 ~]# ansible 172.16.1.8 -m copy -a "src=/etc/hosts dest=/tmp owner=chensiqi group=chensiqi mode=600"
172.16.1.8 | SUCCESS => {
"changed": true,
"checksum": "dba0126bf49ea8d4cdc476828f9edb37085c6afe",
"dest": "/tmp/hosts",
"gid": 502,
"group": "chensiqi",
"mode": "0600",
"owner": "chensiqi",
"path": "/tmp/hosts",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 294,
"state": "file",
"uid": 502
}
[root@m01 ~]# ssh root@172.16.1.8 "ls -l /tmp/hosts"
`root@172.16.1.8's password: `
-rw-------. 1 chensiqi chensiqi 294 3月 14 14:00 /tmp/hosts
命令說明:
對於ansible內置模塊的使用,大家參考命令執行后的返回信息里看就可以了。寫的很清楚。
備注:
copy模塊,如果復制的對方主機路徑下沒有目錄,那么會遞歸創建
特別提示:
ansible的部分模塊並不支持
1)管道符 “|”
2)重定向 “> < >> <<”
3)類似top,tail -f這種不能即刻返回明確信息的命令
4)*
3.3 利用ansible遠程執行各類腳本
3.3.1 先將腳本分發到各個機器上去
[root@m01 ~]# echo "echo '測試成功!'" >> /server/scripts/test.sh
ansible chensiqi -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts/ mode=0755 backup=yes"
注意:dest路徑的寫法,若是不存在的目錄,結尾要加斜線(/server/scripts/),否則默認不會創建目標目錄
3.3.2 遠程批量執行腳本
ansible chensiqi -m shell -a "/server/scripts/yum.sh"
示例:
[root@m01 ~]# ansible chensiqi -m command -a "sh /server/scripts/test.sh"
172.16.1.41 | SUCCESS | rc=0 >>
測試成功!
172.16.1.31 | SUCCESS | rc=0 >>
測試成功!
172.16.1.7 | SUCCESS | rc=0 >>
測試成功!
172.16.1.8 | SUCCESS | rc=0 >>
測試成功!
172.16.1.61 | SUCCESS | rc=0 >>
測試成功!
第4章 常用模塊:每個模塊就是一個功能
模塊名 | 作用 |
---|---|
command | 執行命令模塊(重要) |
copy | 文件拷貝模塊(重要) |
shell | 執行shell腳本模塊(重要) |
script | 執行shell腳本模塊(重要) |
file | 設定文件屬性模塊 |
service | 系統服務管理模塊 |
cron | 計划任務管理模塊 |
yum | yum軟件包安裝管理模塊 |
synchronize | 使用rsync同步文件模塊 |
第5章 查看Ansible的幫助
ansible-doc -l 查看所有的模塊
ansible-doc -s service 查看指定模塊用法