Ansible是新出現的自動化運維工具,基於Python研發。糅合了眾多老牌運維工具的優點實現了批量操作系統配置、批量程序的部署、批量運行命令等功能。僅需在管理工作站上安裝ansible程序配置被管控主機的IP信息,被管控的主機無客戶端。ansible應用程序存在於epel(第三方社區)源,依賴於很多python組件。主要包括:
(1)、連接插件connection plugins:負責和被監控端實現通信;
(2)、host inventory:指定操作的主機,是一個配置文件里面定義監控的主機;
(3)、各種模塊核心模塊、command模塊、自定義模塊;
(4)、借助於插件完成記錄日志郵件等功能;
(5)、playbook:劇本執行多個任務時,非必需可以讓節點一次性運行多個任務。
下面來看下ansible的使用:
一:首先安裝ansible模塊:apt install ansible
二:配置Ansible以及測試。
(1)第一步是修改主機與組配置。文件在/etc/ansible/hosts. 格式位ini。添加1台主機同時定義IP到werbserver組。
#green.example.com
#blue.example.com
192.168.0.9
# Ex 2: A collection of hosts belonging to the 'webservers' group
#[webservers]
#alpha.example.org
#beta.example.org
192.168.0.9
(二)通過ping模塊測試主機的連通性。分別對單主機進行ping操作。出現如下結果表明測試成功。
root@zhf-linux:/home/zhf# ansible 192.168.0.9 -m ping
192.168.0.9 | SUCCESS => {
"changed": false,
"ping": "pong"
}
root@zhf-linux:/home/zhf# ansible webservers -m ping
192.168.0.9 | SUCCESS => {
"changed": false,
"ping": "pong"
}
Ansible是依賴於SSH通信,如果機器之間沒有配置SSH證書訪問的話,則需要用登錄密碼訪問。命令需改為ansible 192.168.0.9 -m ping -k.
三:Ansible功能模塊:
(1) 遠程命令模塊:
模塊包括command,script,shell都可以實現遠程shell命令運行。command作為ansible的默認模板,可以運行遠程權限范圍所有的shell命令,script是在遠程主機執行主控端存儲的shell腳本文件。相當於SCP+SHELL組合,shell功能是執行遠程主機的shell腳本文件。
root@zhf-linux:/home/zhf# ansible webservers -m command -a "ls -al"
192.168.0.9 | SUCCESS | rc=0 >>
total 72
drwx------ 12 root root 4096 Nov 12 14:45 .
drwxr-xr-x 22 root root 4096 Jul 9 16:59 ..
drwxr-xr-x 3 root root 4096 Nov 12 14:45 .ansible
-rw------- 1 root root 5408 Nov 12 14:46 .bash_history
-rw-r--r-- 1 root root 3106 Feb 20 2014 .bashrc
drwx------ 5 root root 4096 Aug 31 21:47 .cache
drwx------ 4 root root 4096 Jul 26 10:47 .config
drwx------ 3 root root 4096 Jul 9 15:59 .dbus
drwx------ 2 root root 4096 Jul 9 16:23 .gvfs
drwxr-xr-x 3 root root 4096 Jul 26 11:23 .local
drwxr-xr-x 2 root root 4096 Jul 26 11:33 .pip
-rw-r--r-- 1 root root 140 Feb 20 2014 .profile
drwxr-xr-x 3 root root 4096 Jul 26 11:41 .python-eggs
drwxr-xr-x 2 root root 4096 Oct 30 21:18 .rpmdb
drwx------ 2 root root 4096 Oct 27 23:11 .ssh
-rw-r--r-- 1 root root 0 Oct 29 10:02 test.txt
-rw------- 1 root root 5439 Oct 31 22:07 .viminfo
在主機上建立一個sh文件,內容很簡單,就是echo “hello ansible”。通過遠程端也可以調用。
root@zhf-linux:/home/zhf# ansible webservers -m shell -a "/home/zhf/zhf/shell_prj/test1.sh"
192.168.0.9 | SUCCESS | rc=0 >>
hello ansible!
拷貝文件:
在上傳文件的同時還可以設置上傳文件的屬主以及權限。格式為: owner=xxx group=xxx mode=0744
root@zhf-linux:/home/zhf/zhf# ansible webservers -m copy -a "src=/home/zhf/zhf/test2.txt dest=/home/zhf owner=root group=root mode=0744"
192.168.0.9 | SUCCESS => {
"changed": true,
"checksum": "07c0752c54e3883358ab0c8c6008004929954217",
"dest": "/home/zhf/test2.txt",
"gid": 0,
"group": "root",
"md5sum": "d2c01e6badaa08464b9e0cd578a5de8b",
"mode": "0744",
"owner": "root",
"size": 30,
"src": "/root/.ansible/tmp/ansible-tmp-1510471651.28-269106495340296/source",
"state": "file",
"uid": 0
}
stat模塊:
獲取遠程文件的狀態信息,包括ctime,mtime,atime,uid,gid等信息。我們用剛才上傳的文件為例。
root@zhf-linux:/home/zhf/zhf# ansible webservers -m stat -a "path=/home/zhf/test2.txt"
192.168.0.9 | SUCCESS => {
"changed": false,
"stat": {
"atime": 1510471652.086876,
"checksum": "07c0752c54e3883358ab0c8c6008004929954217",
"ctime": 1510471652.130877,
"dev": 2054,
"exists": true,
"gid": 0,
"gr_name": "root",
"inode": 130813,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"md5": "d2c01e6badaa08464b9e0cd578a5de8b",
"mode": "0744",
"mtime": 1510471651.438875,
"nlink": 1,
"path": "/home/zhf/test2.txt",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 30,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": true
}
}
遠程安裝軟件:將會遠程主機安裝上ansible模塊。
root@zhf-linux:/home/zhf/zhf# ansible webservers -m apt -a "pkg=ansible state=latest"
cron模塊:
root@zhf-linux:/home/zhf/zhf# ansible webservers -m cron -a "name=check dirs hour='5,2' job='ls -al'"
下一節講介紹playbook模塊