集中化管理平臺Ansible詳解(一)

什麼是ansible?

ansible是一種集成IT系統的配置管理、應用部署、執行特定任務的開源平臺.它是基於python語言,由Paramiko和PyYAML兩個關鍵模塊構建。集合了衆多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。ansible是基於模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運行的模塊,ansible只是提供一種框架。


ansible的優勢

  • 部署簡單,只需要在主控端部署Ansible環境,被控端無需做任何操作;
  • 默認使用SSH(Secure SHell)協議對設備進行管理;
  • 主從集中化管理;
  • 配置簡單、功能強大、擴展性強;
  • 支持API及自定義模塊,可通過Python輕鬆擴展;
  • 通過Playbooks來定製強大的配置、狀態管理;
  • 對雲計算平臺、大數據都有很好的支持;
  • 提供一個功能強大、操作性強的Web管理界面和REST API接口——AWX平臺;
  • 冪等性:一種操作重複多次結果相同。

ansible的安裝和測試

  1. epel源配置
    [epel]  #配置的清華的epel
    name=Fedora EPEL
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/
    gpgcheck=0
  2. yum安裝
    yum install ansible -y -q
  3. ansible 配置
    #在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  
  4. 測試
    [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 [email protected]
    [root@centos7 ~]# ssh-copy-id [email protected]
    [root@centos7 ~]# ssh-copy-id [email protected]
    [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)

ansible的模塊使用

1.遠程命令模塊

  • command :默認的模塊,可以運行遠程權限範圍所有的shell命令
  • script:在遠處主機上執行主控制端儲存的shell腳本文件,相當於scp+shell組合
  • shell:執行遠程主機的shell腳本問文件
[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模塊
實現主控制端想目標拷貝文件.類似於scp

#將/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等

[root@centos7 ~]# ansible web -m stat -a "path=/etc/fstab"

4.get_url模塊
實現遠程主機下載指定的URL到本地,支持sha256sum校驗和

[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模塊
遠程主機掛載

[root@centos7 ~]# ansible web -m mount -a "name=/mnt/data dest=/dev/sd0 fstype=ext3 opts=ro state=present"

8.fetch 模塊
從受管主機拉取文件

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模塊
遠程主機系統服務管理

[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   #簡單查看模塊的幫助文檔
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章