python學習-ansible簡單使用1
Ansible 一種集成 IT 系統的配置管理、應用部署、執行特定任務的開源平臺,是 AnsibleWorks 公司名下的項目,該公司由 Cobbler 及 Func 的作者於 2012 年創建成立。
Ansible 基於 Python 語言實現,由 Paramiko 和 PyYAML 兩個關鍵模塊構建。
Ansible 特點:
>> 部署簡單,只需在主控端部署 Ansible 環境,被控端無需做任何操作。
>> 默認使用 SSH(Secure Shell)協議對設備進行管理。
>> 主從集中化管理。
>> 配置簡單、功能強大、擴展性強。
>> 支持 API 及自定義模塊,可通過 Python 輕松擴展。
>> 對雲計算平臺、大數據都有很好的支持。
二、Ansible的安裝
Ansible只需在管理端部署環境即可,默認通過yum安裝即可。
yum install ansible -y
2.1 Ansible配置及測試
第一步是修改主機與組配置,文件位置/etc/ansible/hosts,格式為ini,添加兩臺主機ip,同時定義兩個IP到webservers組,更新的內容如下:
【/etc/ansible/hosts】 [webservers] #組名 websevers ## alpha.example.org ## beta.example.org 172.31.101.51 #主機1 172.31.101.52 #主機2
通過ping模塊測試主機的連通性,分別對單主機及組進行ping操作
ansible 172.31.101.52 -m ping -k #單個主機 ansible webservers -m ping -k #單個組
測試主機連通性如下圖所示
由於主控端與被控端主機未配置ssh證書信任,需要執行ansible命令添加-k參數,要求提供root(默認)帳號密碼,即提示“SSH password:”時輸入
ping模塊參數說明
# -i 指定 hosts 文件位置 # -u username 指定 SSH 連接的用戶名 # -k 指定遠程用戶密碼 # -f 指定並發數 # -s 如需要 root 權限執行時使用 ( 連接用戶不是 root 時 ) # -K -s 時,-K 輸入 root 密碼
2.2 配置linux主機ssh無密碼訪問
為了避免Ansible下發指令時輸入目標主機密碼,可以通過證書簽名達到ssh無密碼訪問。
在主控端創建密鑰,執行ssh-keygen -t rsa
[root@rbtnode1 ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): (回車) Enter passphrase (empty for no passphrase): (回車) Enter same passphrase again: (回車) Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 8b:8a:91:2e:04:2e:dd:4d:99:c0:e9:f0:5f:f9:85:bb root@rbtnode1 The key's randomart image is: +--[ RSA 2048]----+ | . . | | . + | | + . o . . | |. o + o . . | |o. . + .S. o | |.o... o. .o | |o o . . . | |.. o . E | | .o . | +-----------------+
接下來同步公鑰文件id_rsa.pub到目標主機,使用ssh-copy-id公鑰拷貝工具,命令格式為/usr/bin/ssh-copy-id [-i[identity-file]][user@]machine
ssh-copy-id -i /root/.ssh//id_rsa.pub [email protected] ssh-copy-id -i /root/.ssh//id_rsa.pub [email protected]
2.3 定義主機與組規則
Ansible通過定義好的主機與組規則(Inventory)對匹配的目標主機進行遠程操作,配置規則文件默認是/etc/ansible/hosts,以下為舉例說明:
www.abc.com # 定義域名 192.168.1.100 # 定義 IP 192.168.1.150:37268 # 指定端口號 [WebServer] # 定義分組 192.168.1.10 192.168.1.20 192.168.1.30 [DBServer] # 定義多個分組 192.168.1.50 192.168.1.60 Monitor ansible_ssh_port=12378 ansible_ssh_host=192.168.1.200 # 定義別名 # ansible_ssh_host 連接目標主機的地址 # ansible_ssh_port 連接目標主機的端口,默認 22 時無需指定 # ansible_ssh_user 連接目標主機默認用戶 # ansible_ssh_pass 連接目標主機默認用戶密碼 # ansible_ssh_connection 目標主機連接類型,可以是 local 、ssh 或 paramiko # ansible_ssh_private_key_file 連接目標主機的 ssh 私鑰 # ansible_*_interpreter 指定采用非 Python 的其他腳本語言,如 Ruby 、Perl 或其他類似 ansible_python_interpreter 解釋器 [webservers] # 主機名支持正則描述 www[01:50].example.com [dbservers] db-[a:f].example.com
2.4 目標匹配
目標匹配,格式為ansible <pattern_goes_here> -m <module_name> -a <arguments> 舉例說明:重啟webservers組的所有Apache服務
ansible webservers -m service -a "name=httpd state=restarted"
規則 | 含義 |
192.198.1.2或one.example.com | 匹配目標Ip地址或者主機名,多個ip或主機名使用":"號分隔 |
webservers | 匹配目標組為webserver,多個組使用":"號分隔 |
ALL 或 ‘*’ | 匹配目標所有主機 |
~(web|db).*\.example\.com 或 192.168.1.* | 支持正則表達式匹配所有主機或ip地址 |
webservers:!192.168.1.22 | 匹配websevers組且排除192.168.1.22主機ip |
webservers:&dbservers | 匹配webservers與dbservers兩個群組的交集 |
webservers:!{{excluded}}:&{{required}} | 支持變量匹配的方式 |
2.5查詢支持模塊及模塊說明
ansible-doc -l # 列出 Ansible 支持的模塊 ansible-doc ping # 查看該模塊幫助信息
三、常用模塊及api
3.1遠程命令模塊
模塊包括command、script、shell都可以實現遠程shell命令運行。command作為Ansible的默認模塊,可以運行遠程權限範圍內所有的shell命令;script功能是在遠程主機上執行主控端存儲shell腳本文件,相當於scp+shell組合;shell功能是執行遠程主機的shell腳本文件
ansible webservers -m command -a "free -m" ansible webservers -m script -a "/home/test.sh 12 34" ansible webservers -m shell -a "/home/test.sh"
3.2copy模塊
實現主控端向目標主機拷貝文件,類似於scp的功能。以下示例實現拷貝/root/pip-10.0.1.tar.gz 文件到webserver組目標主機/tmp/目錄下,並更新文件屬主及權限
# ansible webservers -m copy -a "src=/root/pip-10.0.1.tar.gz dest=/tmp/ owner=root group=root mode=0755"
3.3 stat模塊
獲取遠程文件的狀態信息,包括atime ,ctime ,md5等信息
ansible webservers -m stat -a "path=/tmp/pip-10.0.1.tar.gz"
4.4 get_url模塊
實現在遠程主機下載指定URL到本地,支持sha256sum文件校驗
ansible webservers -m get_url -a "url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes"
4.5 yum模塊
linux平臺軟件包管理操作,常見有yum,apt管理方式
ansible webservers -m yum -a "name=wget state=latest
4.6 cron模塊
遠程主機crontab配置
ansible webservers -m cron -a "name='check dirs' hour='5,2' job='ls -alh > /dev/null'"
在遠程主機查看計劃任務
4.7 mount模塊
遠程主機的分區掛載
ansible webservers -m mount -a "name=/mnt/date src=fstype=exts opts=ro state=present"
4.8 service模塊
遠程主機系統服務管理
ansible webservers -m service -a "name=firewalld state=stopped" ansible webservers -m service -a "name=firewalld state=started" ansible webservers -m service -a "name=firewalld state="restartd" ansible webservers -m service -a "name=firewalld state=reloded"
4.8 user服務模塊
遠程主機系統用戶管理
ansible webservers -m user -a "name=yangchao comment='yangchao'" #增加用戶 ansible webservers -m user -a "name=yangchao state=absent remove=yes" #刪除用戶
關於ansible其他模塊及詳細用法,請參照
http://www.ansible.com.cn/docs/modules_intro.html
python學習-ansible簡單使用1