ansible簡介
ansible是新出現的自動化運維工具,基於python開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程式部署、批量執行命令等功能。ansible是基於模組工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所執行的模組,ansible只是提供一種框架。主要包括:
(1)、連線外掛程式connection plugins:負責和被監控端實現通訊;
(2)、host inventory:指定操作的主機,是乙個配置檔案裡面定義監控的主機;
(3)、各種模組核心模組、command模組、自定義模組;
(4)、借助於外掛程式完成記錄日誌郵件等功能;
(5)、playbook:劇本執行多個任務時,非必需可以讓節點一次性執行多個任務。
windows下ansible工作模式
ansible 從1.7+版本開始支援windows,但前提是管理機必須為linux系統,遠端主機的通訊方式也由ssh變更為powershell,同時管理機必須預安裝python的winrm模組,方可和遠端windows主機正常通訊,但powershell需3.0+版本且management framework 3.0+版本,實測windows 7 sp1和windows server 2008 r2及以上版本系統經簡單配置可正常與ansible通訊。簡單總結如下:
(1) 管理機必須為linux系統且需預安裝python winrm模組
(2) 底層通訊基於powershell,版本為3.0+,management framework版本為3.0+
(3) 遠端主機開啟winrm服務
ansible管理機部署安裝
1). 對管理主機的要求
目前,只要機器上安裝了 python 2.6 或 python 2.7 (windows系統不可以做控制主機),都可以執行ansible.
主機的系統可以是 red hat, debian, centos, os x, bsd的各種版本,等等.
這個節點就是安裝ansible的節點。
shell(nmc)># yum -y install epel-release
shell(nmc)># yum -y install ansible.noarch
配置管理主機:vim /etc/ansible/hosts
免密登陸設定:
49 ssh-keygen -t rsa -p
52 find / -name ssh-keygen
53 cd /usr/bin/
57 ansible sdyy02 -m ping
59 ansible 192.168.7.7 -m ping
60 ssh-keygen -t rsa
61 cd $home/.ssh
63 ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
64 ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
65 vi /etc/ansible/ansible.cfg
66 ansible all -m ping
2)控制機安裝python winrm模組
tar zxvf pywinrm-0.2.2.tar.gz
211 tar -zxvf isodate-0.6.0.tar.gz
212 tar -zxvf xmlwitch-0.3.tar.gz
213 cd pywinrm-0.2.2/
214 ls
215 python setup.py install
216 python
217 ansible windows -m win_ping
ansible使用命令
群組名稱:sdyy
批量傳輸檔案並備份: ansible sdyy -s -m copy -a 'src=/home/master/upload/list.jsp dest=目的路徑 owner=webadmin group=webadmin backup=yes'
批量刪除 :ansible sdyy -m shell -a "rm -rf /opt/cs.txt"
批量啟動tomcat: ansible sdyy -m shell -a " nohup /opt/tomcat1.sh start owner=webadmin group=webadmin"
批量關閉tomcat: ansible sdyy -m shell -a "/opt/tomcat1.sh stop owner=webadmin group=webadmin"
windows:
ping :ansible windows -m win_ping
拷貝:ansible windows -m win_copy -a 'src=/opt/cs.txt dest=d:\'
刪除:ansible windows -m win_file -a "path=d:\cs.txt state=absent"
windows配置:
和linux發版版稍有區別,遠端主機系統如為windows需預先如下配置:
•安裝framework 3.0+
•更改powershell策略為remotesigned
•公升級powershell至3.0+
•設定windows遠端管理,英文全稱ws-management(winrm)
(1)安裝framework 3.0+
(2)更改powershell策略為remotesigned
set-executionpolicy remotesigned
(3)公升級powershell至3.0+
window 7和windows server 2008 r2預設安裝的有powershell,但版本號一般為2.0版本,所以我們需公升級至3.0+,如下圖中數字1部分表示powershell版本過低需3.0+版本,數字2部分表示當前powershell版本為2.0。
(4)設定windows遠端管理(ws-management,winrm)
winrm service 預設都是未啟用的狀態,先檢視狀態;如無返回資訊,則是沒有啟動;
winrm enumerate winrm/config/listener
針對winrm service 進行基礎配置:
winrm quickconfig
檢視winrm service listener:
winrm e winrm/config/listener
為winrm service 配置auth:
winrm set winrm/config/service/auth @
為winrm service 配置加密方式為允許非加密:
winrm set winrm/config/service @
ansible windows -m win_ping
錯誤說明
pip list
229 ansible windows -m win_ping
230 sudo pip uninstall request
231 sudo pip uninstall requests
232 pip install requests
233 pip uninstall docopt
234 sudo pip uninstall docopt
235 sudo pip install docopt
236 ansible windows -m win_ping
修改ansible的windows連線方式
vi /etc/ansible/hosts
[sdyy]
192.168.7.7
192.168.7.8
[windows]
192.168.7.11 ansible_ssh_user="administrator" ansible_ssh_pass="sdyykj123" ansible_ssh_port=5985 ansible_connection=winrm
ansible環境搭建
ansible的環境搭建 1 我們需要有ntp的時間同步 yum install ntp 管理機與客戶機都要安裝 vim etc ntp.conf 在客戶機上做 192.168.197.40位管理機的ip 2 ssh 無 agent 的方式,最好去配置證書認證 ssh keygen t rsa p ...
ansible安裝及配置
ansible安裝及準備 yum install epel release y 安裝依賴工具 yum install git python python pip y yum y install ansible 配置ansible ssh金鑰登陸 ssh keygen t rsa b 2048 回車 ...
ansible 安裝配置
安裝 yum install y ansible 關閉selinux 建立乙個ssh 放置主機資訊 mkdir p etc ansible ssh cd 建立hosts centos7修改主機名 hostnamectl set hostname baidu 然後reboot 把私鑰上傳到 etc a...