自動化運維工具之ansible
ansible
ansible是新出現的自動化運維工具,基於Python開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。
ansible是基於模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運行的模塊,ansible只是提供一種框架。
主要包括:
(1)、連接插件connection plugins:負責和被監控端實現通信;
(2)、host inventory:指定操作的主機,是一個配置文件裏面定義監控的主機;
(3)、各種模塊核心模塊、command模塊、自定義模塊;
(4)、借助於插件完成記錄日誌郵件等功能;
(5)、playbook:劇本執行多個任務時,非必需可以讓節點一次性運行多個任務。
ansible的架構:
特性
1.no agent: 不需要在被管控主機上安裝任何軟件
2.no server: 無服務器端,使用時直接運行命令即可
3.modules in any languages:基於模塊工作,可使用任意語言開發模塊,
4.yaml,not code:使用yaml語言定制劇本playbook,
5.ssh by default:基於SSH工作
優點
(1)、輕量級,無需在客戶端安裝agent,更新時,只需在操作機上進行一次更新即可;
(2)、批量任務執行可以寫成腳本,而且不用分發到遠程就可以執行
(3)、使用python編寫,維護更簡單,ruby語法過於復雜;
=============================================================
ansible的部署以及應用
環境準備在pxe裏有
三臺機器、一臺master、兩臺node
Ansible的安裝部署:以rpm的為例
方法1:
在線安裝(EPEL源)
[[email protected] ~]# yum install -y epel-reseale
[[email protected] ~]# yum install -y ansible
方法2:
自己制作本地yum源
[[email protected] ~]# yum install -y ansible
ansible的配置文件:
[[email protected] ~]# rpm -qc ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
/etc/ansible/hosts------------主機配置文件
寫法1:
node1.ansible.com
node2.ansible.com
192.168.1.1
寫法2:以組的方式
[webserver]
192.168.10.1
192.168.10.2
[dbserver]
192.168.20.1
192.168.20.2
ansible基於SSH的方式工作
基於用戶名,密碼
192.168.10.1 ansible_ssh_user=root ansible_ssh_pass=redhat
基於密鑰
Ansible的使用流程:
1、配置基於密鑰的SSH
[[email protected] ~]# ssh-keygen -t rsa
[[email protected] ~]# ssh-copy-id -i 192.168.87.102
2、編輯/etc/ansible/hosts文件,指定被管理端的IP地址或者主機名
# vim /etc/ansible/hosts
[test]
192.168.87.102
3、使用ansible的模塊
ansible模塊
# ansible <PATTERN> -m <module_name> -a <arguments>
PATTERN的寫法:
某一個主機組的名稱 web
所有主機 all, *
寫IP地址或系列主機名
one.example.com
one.example.com:two.example.com >>>支持寫多個主機名,不同的主機名間使用冒號":"隔開
192.168.1.50
192.168.1.* >>>支持通配符
webservers:!phoenix >>>對屬於webservers組中的主機,但不屬於phoenix組的主機
webservers:&phoenix >>>對同時屬於webservers和phoenix組中的主機進行操作
正則表達式, 必須以~開頭
~(web|db).*\.example\.com
=============================================================
常用的模塊:
如何查看ansible支持的模塊
[[email protected] ~]# ansible-doc -l
查看模塊支持的參數
# ansible-doc <模塊名稱>
[[email protected] ~]# ansible-doc ping
ansible模塊的說明:
# ansible <pattern> -m <module_name> [-a <arguments>]
1、ping模塊
檢測被管理端是否在線
[[email protected] ~]# ansible test -m ping ---->test是自己在 /etc/ansible/hosts定義的
2、command 模塊
在被管理端執行命令
不支持重定向,管道
默認模塊
[[email protected] ~]# ansible test -m command -a ‘uptime‘
[[email protected] ~]# ansible test -m command -a ‘date‘
[[email protected] ~]# ansible test -m command -a ‘touch /tmp/aa.txt‘
參數:
chdir=<Directory>
[[email protected] ~]# ansible test -m command -a "chdir=/tmp ls ./"
3、shell 模塊
在被管理端執行命令
支持重定向,管道
[[email protected] ~]# ansible test -m shell -a ‘echo "hello ansible" > /tmp/bb.txt‘
4.copy模塊
拷貝ansible管理端的文件到遠程主機的指定位置
常見參數有:
dest= 指明拷貝文件的目標目錄位置,使用絕對路徑,如果源是目錄,則目標也要是目錄,如果目標文件已存在,會覆蓋原有內容
src=\‘#\‘" 指明本地路徑下的某個文件,可以使用相對路徑和絕對路徑,支持直接指定目錄,如果源是目錄,則目標也要是目錄
mode= 指明復制時,目標文件的權限
owner= 指明復制時,目標文件的屬主
group= 指明復制時,目標文件的屬組
content= 指明復制到目標主機上的內容,不能與src一起使用,相當於復制content指明的數據,到目標文件中
[[email protected] ~]# ansible test -m copy -a "src=/etc/hosts dest=/tmp"
[[email protected] ~]# ansible test -m copy -a "src=/etc/passwd dest=/tmp mode=600owner=nobody group=nobody"
5.cron模塊
管理計劃任務的模塊
常見參數有:
minute= 指明計劃任務的分鐘,支持格式:0-59,*,*/2等,與正常cron任務定義的一樣的語法,省略時,默認為*,也就是每分鐘都執行
hour= 指明計劃任務的小時,支持的語法:0-23,*,*/2等,省略時,默認為*,也就是每小時都執行
day= 指明計劃任務的天,支持的語法:1-31,*,*/2等,省略時,默認為*,也就是每天都執行
month= 指明計劃任務的月,支持的語法為:1-12,*,*/2等,省略時,默認為*,也就是每月都執行
weekday= 指明計劃任務的星期幾,支持的語法為:0-6,*等,省略時,默認為*,也就是每星期幾都執行
reboot 指明計劃任務執行的時間為每次重啟之後
name= 給該計劃任務取個名稱,必須要給明。每個任務的名稱不能一樣。
job= 執行的任務是什麽,當state=present時才有意義
state=present|absent 表示這個任務是創建還是刪除,present表示創建,absent表示刪除,默認是present
[[email protected] ~]# ansible test -m cron -a ‘minute=*/5 name=Ajob job="/usr/sbin/ntpdate 172.16.8.100 &> /dev/null" state=present‘
7.file模塊
用於設定遠程主機上的文件屬性
常見參數有:
path= 指明對哪個文件修改其屬性
src=\‘#\‘" 指明path=指明的文件是軟鏈接文件,其對應的源文件是誰,必須要在state=link時才有用
state=directory|link|absent 表示創建的文件是目錄還是軟鏈接
owner= 指明文件的屬主
group= 指明文件的屬組
mode= 指明文件的權限
創建軟鏈接的用法:
src=\‘#\‘" state=link
修改文件屬性的用法:
path= owner= mode= group=
創建目錄的用法:
path= state=directory
刪除文件:
path= state=absent
創建軟連接
[[email protected] ~]# ansible test -m file -a ‘src=/etc/passwd path=/tmp/passwd.link state=link‘
刪除文件
[[email protected] ~]# ansible test -m file -a ‘path=/tmp/cc.txt state=absent‘
修改文件屬性
[[email protected] ~]# ansible test -m file -a ‘path=/tmp/bb.txt mode=700 owner=root group=nobody‘
創建目錄
[[email protected] ~]# ansible test -m file -a ‘path=/tmp/bj state=directory‘
刪除目錄
[[email protected] ~]# ansible test -m file -a ‘path=/tmp/bj state=absent‘
8.hostname模塊
管理遠程主機上的主機名
常用參數有
name= 指明主機名
[[email protected] ~]# ansible test -m shell -a ‘hostname‘
9.yum模塊
基於yum機制,對遠程主機管理程序包
常用參數有:
name= 指明程序包的名稱,可以帶上版本號,不指明版本,就是默認最新版本
name=httpd
name=httpd-2.2.15
state=present|lastest|absent 指明對程序包執行的操作,present表示安裝程序包,latest表示安裝最新版本的程序包,absent表示卸載程序包
disablerepo= 在用yum安裝時,臨時禁用某個倉庫,倉庫的ID
enablerepo= 在用yum安裝時,臨時啟用某個倉庫,倉庫的ID
conf_file= 指明yum運行時采用哪個配置文件,而不是使用默認的配置文件
disable_gpg_check=yes|no 是否啟用gpg-check
卸載軟件包:
[[email protected] ~]# ansible test -m yum -a ‘name=httpd state=absent‘
安裝軟件包:
[[email protected] ~]# ansible test -m yum -a ‘name=httpd state=present‘
10、service模塊
用來管理遠程主機上的服務的模塊
常見參數有:
name= 被管理的服務名稱(/etc/init.d)
state=started|stopped|restarted 表示啟動或關閉或重啟
enabled=yes|no 表示要不要設定該服務開機自啟動
runlevel= 如果設定了enabled開機自動啟動,則要定義在哪些運行級別下自動啟動
[[email protected] ~]# ansible test -m service -a ‘name=nginx state=started‘
11. uri模塊
如果遠端是web服務器,可以利用ansible直接請求某個網頁
常見參數有:
url= 指明請求的url的路徑,如:http://10.1.32.68/test.jpg
user= 如果請求的url需要認證,則認證的用戶名是什麽
password= 如果請求的url需要認證,則認證的密碼是什麽
method= 指明請求的方法,如GET、POST, PUT, DELETE, HEAD
[[email protected] ~]# ansible test -m uri -a ‘url=http://192.168.87.102/index.html‘
12.group模塊
用來添加或刪除遠端主機的用戶組
常見參數有:
name= 被管理的組名
state=present|absent 是添加還是刪除,不指名默認為添加
gid= 指明GID
system=yes|no 是否為系統組
[[email protected] ~]# ansible test -m group -a ‘name=hr gid=2000 state=present‘
13.user模塊
管理遠程主機上的用戶的賬號
常見參數有:
name= 指明要管理的賬號名稱
state=present|absent 指明是創建賬號還是刪除賬號,present表示創建,absent表示刪除
system=yes|no 指明是否為系統賬號
uid= 指明用戶UID
group= 指明用戶的基本組
groups= 指明用戶的附加組
shell= 指明默認的shell
home= 指明用戶的家目錄
move_home=yes|no 當home設定了家目錄,如果要創建的家目錄已存在,是否將已存在的家目錄進行移動
password= 指明用戶的密碼,最好使用加密好的字符串 opssl passwd -l -salt ‘123‘ ‘redhat‘ 生成加密密碼
comment= 指明用戶的註釋信息
remove=yes|no 當state=absent時,也就是刪除用戶時,是否要刪除用戶的而家目錄
[[email protected] ~]# ansible test -m user -a ‘name=martin group=hr groups=shichang uid=500 shell=/bin/bash home=/home/martin comment="martin user"‘
14.script模塊
將管理端的某個腳本,移動到遠端主機(不需要指明傳遞到遠端主機的哪個路徑下,系統會自動移動,然後執行),
一般是自動移動到遠端主機的/root/.ansible/tmp目錄下,然後自動給予其權限,然後再開個子shell然後運行腳本,運行完成後刪除腳本
測試腳本
[[email protected] ~]# ansible test -m script -a ‘/root/1.sh‘
15.setup模塊
可收集遠程主機的facts變量的信息,相當於收集了目標主機的相關信息(如內核版本、操作系統信息、cpu、…),保存在ansible的內置變量中,之後我們有需要用到時,直接調用變量即可
[[email protected] ~]# ansible test -m setup
!@#$%^&*其他的通過上面的方式查看!@#$%^&*
本文出自 “lyw666” 博客,請務必保留此出處http://lyw666.blog.51cto.com/12823216/1957654
自動化運維工具之ansible