ansible是一種集成IT系統的配置管理、應用部署、執行特定任務的開源平臺.它是基於python語言,由Paramiko和PyYAML兩個關鍵模塊構建。集合了衆多運維工具(puppet、cfengine、chef、func、fabric)的優勢,實現了批量系統配置、批量程序部署、批量運行命令等功能。ansible是基於模塊工做的,自己沒有批量部署的能力。真正具備批量部署的是ansible所運行的模塊,ansible只是提供一種框架。html
[epel] #配置的清華的epel name=Fedora EPEL baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/ gpgcheck=0
yum install ansible -y -q
#在ansible的配置文件中添加主機信息,便可與目標主機進行通訊,配置文件位置/etc/ansible/hosts,其中,[web][test]爲主機組,能夠批量控制主機組裏面的全部主機,一個主機能夠添加到多個組。 [root@centos7 ~]# /etc/ansible/hosts 172.18.153.101 172.18.153.103 [web] 172.18.153.101 172.18.153.103 [db] 172.18.153.102 172.18.153.103 "/etc/ansible/hosts" 49L, 1092C
[root@centos7 ~]# ansible test --list #查看用戶組的成員 hosts (2): 172.18.153.27 172.18.153.37 #配置之ssh等效性 [root@centos7 ~]# ssh-keygen [root@centos7 ~]# ssh-copy-id root@172.18.153.101 [root@centos7 ~]# ssh-copy-id root@172.18.153.102 [root@centos7 ~]# ssh-copy-id root@172.18.153.103 [root@centos7 ~]# ansible all -m ping #測試是否連通,出現pong則說明成功管理 172.18.153.103 | SUCCESS => { "changed": false, "ping": "pong" } 172.18.153.102 | SUCCESS => { "changed": false, "ping": "pong" } 172.18.153.101 | SUCCESS => { "changed": false, "ping": "pong" } [root@centos7 ~]# ansible all -m command -a 'useradd zhangfei' #因此主機建立用戶-m comand是使用command模塊 -a 添加參數 172.18.153.103 | CHANGED | rc=0 >> 172.18.153.101 | CHANGED | rc=0 >> 172.18.153.102 | CHANGED | rc=0 >> [root@centos7 ~]# ansible all -m command -a 'id zhangfei' #成功 172.18.153.101 | CHANGED | rc=0 >> uid=1001(zhangfei) gid=1001(zhangfei) 組=1001(zhangfei) 172.18.153.103 | CHANGED | rc=0 >> uid=1002(zhangfei) gid=1002(zhangfei) 組=1002(zhangfei) 172.18.153.102 | CHANGED | rc=0 >> uid=1001(zhangfei) gid=1001(zhangfei) 組=1001(zhangfei)
1.遠程命令模塊python
[root@centos7 ~]# ansible web -m command -a "free -m" [root@centos7 ~]# ansible web -m script -a "/root/hello.sh 12 34" [root@centos7 ~]# ansible web -m shell -a "/root/hello.sh"
2.copy模塊
實現主控制端想目標拷貝文件.相似於scpweb
#將/etc/fstab拷貝到web組目標主機/tmp/下,並更新文件屬主和權限 [root@centos7 ~]# ansible web -m copy -a "src=/etc/fstab dest=/tmp/ owner=root group=root mode=0744"
3.stat模塊
獲取遠程文件狀態信息,如atime,md5,uid等shell
[root@centos7 ~]# ansible web -m stat -a "path=/etc/fstab"
4.get_url模塊
實現遠程主機下載指定的URL到本地,支持sha256sum校驗和centos
[root@centos7 ~]# ansible web -m get_url -a "url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes"
5.yum模塊
Linux平臺軟件包管理模塊框架
[root@centos7 ~]# ansible web -m yum -a "name=curl state=latest"
6.cron模塊
遠程主機的計劃任務配置運維
[root@centos7 ~]# ansible web -m cron -a 'minute=* weekday=2,4,6 job="/usr/bin/wall FBI WARNING" name=warningcron' [root@centos7 ~]# crontab -l #去節點機查看效果 #Ansible: warningcron * * * * 2,4,6 /usr/bin/wall FBI WARNING [root@centos7 ~]# ansible all -m cron -a 'name=warningcron state=absent' #取消 [root@centos7 ~]# ansible all -m cron -a 'disabled=true job="/usr/bin/wall FBI WARNING" name=warningcron' #禁用 [root@centos7 ~]# ansible all -m cron -a 'disabled=false job="/usr/bin/wall FBI WARNING" name=warningcron'#啓用
7.mount模塊
遠程主機掛載ssh
[root@centos7 ~]# ansible web -m mount -a "name=/mnt/data dest=/dev/sd0 fstype=ext3 opts=ro state=present"
8.fetch 模塊
從受管主機拉取文件curl
root@centos7 ~]# ansible all -m fetch -a 'src=/var/log/messages dest=/root/ansible' #若是要用fetch或copy傳輸多個文件,只能先打包 root@centos7 ~]# ansible all -m shell -a 'tar Jcf /root/log.tar.xz /var/log/*.log' root@centos7 ~]# ansible all -m fetch -a 'src=/root/log.tar.xz dest=/root/ansible'
9.service模塊
遠程主機系統服務管理ide
[root@centos7 ~]# ansible web -m mount -a "name=httpd state=restart"
ansible的模塊到如今爲止一共2080個,須要本身慢慢摸索,我這裏不久多列舉了,查看模塊的方法
[root@centos7 ~]# ansible-doc -s -l #列出全部模塊 [root@centos7 ~]# ansible-doc fetch #查看詳細的模塊幫助文檔 [root@centos7 ~]# ansible-doc -s fetch #簡單查看模塊的幫助文檔