ansible批量管理服務工具。
ansible批量管理服務工具
ansible批量管理服務工具
批量管理伺服器的工具
無須部署agent,通過ssh進行管理
流行的自動化運維工具:https://github.con/ansible/ansible
三種批量管理工具
ansible (so easy) 500以下伺服器
saltstack (比較複雜) 1000到4萬伺服器
puppet (超級複雜) 只有很老企業在用
jkenkins簡介
視覺化運維(主要用在視覺化部署)
持續構建,可以和git,svn結合(存放開發程式碼的倉庫)
可結合ssh實現視覺化運維
可結合ansible實現視覺化運維
Ansible伺服器簡單的綜合安全管理策略
#禁止非root使用者檢視Ansible管理伺服器端/etc/hosts檔案
[[email protected] ~]# ll /etc/hosts
-rw-r--r--. 1 root root 180 9月 9 00:38 /etc/hosts
[[email protected] ~]# chmod 600 /etc/hosts
#禁止非root使用者檢視Ansible的主機清單配置檔案
[[email protected] ~]# ll /etc/ansible/hosts
-rw-r--r-- 1 root root 87 9月 9 21:59 /etc/ansible/hosts
[[email protected] ~]# chmod 600 /etc/ansible/hosts
ansible檢視幫助
/usr/local/python/bin/ansible-doc -l(檢視總幫助)
/usr/local/python/bin/ansible-doc -s shell(檢視shell模組的幫助)
/usr/local/python/bin/ansible-doc -s raw
安裝ansible流程
如果Centos7版本
需要安裝yum -y install net-toolsvim
關閉防火牆:systemctl stop firewalld 關閉防火牆開機啟動:systemctl disable fierwalld
關閉selinux
7.5yum安裝ansible
7.5yum倉庫全可以用,本地的需要自己手動開啟
yum -y install epel-release
yum -y install ansible (自動安裝sshpass軟體包)
安裝支援包
yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses-devel unzip zlib-devel zlib openssl-devel openssl libffi-devel
下載python原始碼包
連結:https://pan.baidu.com/s/1nE0v2fN6dq4FHfhSTbBnsg
提取碼:ud04需要雲yum
wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
原始碼編譯Python3.5
tar xf Python-3.5.2.tgz -C /usr/src
cd /usr/src/Python-3.5.2
./configure --prefix=/usr/local/python
make && make install
ln -s /usr/local/python/bin/python3 /usr/bin/python3(製作軟連結)
which python3(檢視命令是否存在)
python3 -V(查詢python版本)
靜心等待ansible安裝完畢後
ln -s /usr/local/python/bin/ansible /usr/local/bin(製作軟連結)
which ansible(檢視命令是否存在)
ansible --version(檢視ansible版本)
ansible的簡單配置
通過pip安裝的ansible是沒有配置檔案的
mkdir -p /etc/ansible(預設沒有,需要手動建立)
vim /etc/ansible/hosts(預設沒有,需要手動建立)
ansible命令使用格式
ansible -i 主機或主機組 -m 指定模組 -a 命令 (-i指定配置檔案,不寫就預設路徑下/etc/ansible/hosts,-m指定模組,-a釋出命令)
command模組釋出命令使用格式
(對方需要有python包,釋出命令)
ansible nginx -m command -a 'hostname -I'
(分發模組內容格式,nginx是模組名,-a是條件,-m command是呼叫ansible裡面的模組釋出命令用)ansible client2 -m command -a 'hostname -I'
(分發單個主機格式,client2是主機名,-a是條件,-m command是呼叫ansible裡面的模組釋出命令用)ansible client1 -m command -a 'hostname -I'
(分發單個主機格式,client1是主機名,-a是條件,-m command是呼叫ansible裡面的模組釋出命令用)ansible client1:client2 -m command -a 'hostname -I'
(分發多個主機格式,client1:client2是主機名,-a是條件,-m command是呼叫ansible裡面的模組釋出命令用)ansible all -m command -a 'hostname -I'
(all是分發所有主機格式,-a是條件,-m command是呼叫ansible裡面的模組釋出命令用)
ping模組釋出命令使用格式
(對方需要有python包)
ping模組檢查伺服器是否連線正常,ping模組不需要-a指定引數
ansible all -m ping (ansible的ping模組格式)
shell模組釋出命令使用格式
(對方需要有python包)
shell模組支援管道符格式
ansible all -m shell -a 'echo test | grep t'shell模組支援重定向格式
ansible all -m shell -a "echo bb >> /tmp/testansible"shell模組支援awk格式
ansible all -m shell -a "cat /etc/passwd | awk -F":" '{print $1}'" (如果遇到特殊符號需要加入\轉義)
raw模組使用格式僅通過ssh實現
(不依賴python包)
copy模組注意事項
yum -y install libselinux-python(傳送失敗的話說明對方沒有這個支援包)
copy模組拷貝檔案目錄使用格式
ansible all -m copy -a 'src=/root/xin dest=/tmp'
(src原始檔,dest目標位置,對方沒有目錄模組自動建立)
ansible all -m copy -a 'src=/root/xin dest=/tmp backup=yes'
(src原始檔,dest目標位置,backup=yes覆蓋同時是否備份原始檔)
ansible all -m copy -a 'src=/root/xin dest=/tmp owner=nobody group=nobody mode=0600'
(owner=屬主是誰,group=屬組是誰,mode=它許可權)
script模組批量執行指令碼使用格式
ansible all -m script -a "/service/scripts/auto_nginx.sh"
#操作示例-->遠端批量分發並自動部署nginx
#所有被管理端需要掛載光碟,並建立本地yum配置檔案
[[email protected] scripts]# pwd
/service/scripts
[[email protected] scripts]# ls | xargs -n1
auto_nginx.sh #自動安裝nginx指令碼
fenfa.sh #批量分發指令碼
nginx-1.10.2.tar.gz #nginx原始碼包
[[email protected] scripts]# cat auto_nginx.sh #nginx安裝指令碼
#!/bin/sh
#nginx install shell scripts
test -d /media/cdrom || mkdir -p /media/cdrom
mount /dev/sr0 /media/cdrom &>/dev/null
yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel &>/dev/null
test -d /service/scripts || exit 3
cd /service/scripts/
tar xf nginx-1.10.2.tar.gz -C /usr/src/
cd /usr/src/nginx-1.10.2/
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module &>/dev/null
make &>/dev/null
make install &>/dev/null
exit 0
[[email protected] scripts]# cat fenfa.sh #原始碼包和安裝指令碼的批量分發指令碼
#!/bin/sh
#批量分發指令碼
Group=$1
ansible $Group -m copy -a "src=/service/scripts/ dest=/service/scripts/"
ansible $Group -m script -a "/service/scripts/auto_nginx.sh"
[[email protected] scripts]# sh fenfa.sh all #啟用指令碼
cron定時任務模組
Ansible中的cron模組用於定義任務計劃。主要包括兩種狀態(state)
#新增定時任務計劃,在所有被管理的主機裡每十分鐘輸出hello字串,定時任務描述為test cron job
[[email protected] ~]# ansible all -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job"'
Web02 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"test cron job"
]
}
Web01 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"test cron job"
]
}
[[email protected] ~]# ansible all -m shell -a 'crontab -l'
Web01 | SUCCESS | rc=0 >>
#Ansible: test cron job
*/10 * * * * /bin/echo hello
Web02 | SUCCESS | rc=0 >>
#Ansible: test cron job
*/10 * * * * /bin/echo hello
#刪除描述為test cron job的定時任務
[[email protected] ~]# ansible all -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state=absent'
Web02 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
Web01 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
[[email protected] ~]# ansible all -m shell -a 'crontab -l'
Web02 | SUCCESS | rc=0 >>
Web01 | SUCCESS | rc=0 >>
#給Web01伺服器上的普通使用者yunjisuan新增一個定時任務
[[email protected] ~]# ansible Web01 -m shell -a 'id yunjisuan'
Web01 | SUCCESS | rc=0 >>
uid=1000(yunjisuan) gid=1000(yunjisuan) 組=1000(yunjisuan)
[[email protected] ~]# ansible Web01 -m cron -a 'minute="*/10" job="/bin/echo hello" name="yunjisuan cron job" user="yunjisuan"'
Web01 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"yunjisuan cron job"
]
}
[[email protected] ~]# ansible Web01 -m shell -a 'crontab -u yunjisuan -l'
Web01 | SUCCESS | rc=0 >>
#Ansible: yunjisuan cron job
*/10 * * * * /bin/echo hello
[[email protected] ~]# ansible Web01 -m cron -a 'minute="*/10" job="/bin/echo hello" name="yunjisuan cron job" user="yunjisuan" state="absent"'
Web01 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
[[email protected] ~]# ansible Web01 -m shell -a 'crontab -u yunjisuan -l'
Web01 | SUCCESS | rc=0 >>
yum模組批量安裝軟體包
利用yum模組安裝軟體包,雖然能被shell模組替代,但是用yum模組更顯專業一些
user模組批量建立使用者
使用者管理模組。管理使用者賬號
#在Web02上建立一個普通使用者yunjisuan,並設定使用者的密碼為123123
[[email protected] ~]# ansible Web02 -m user -a 'name=yunjisuan comment="welcom to yunjisuan" uid=1066 groups=wheel password=123123 shell=/bin/bash home=/home/yunjisuan'
Web02 | SUCCESS => {
"changed": true,
"comment": "welcom to yunjisuan",
"create_home": true,
"group": 1066,
"groups": "wheel",
"home": "/home/yunjisuan",
"name": "yunjisuan",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1066
}
[[email protected] ~]# ansible Web02 -m shell -a 'tail -1 /etc/passwd'
Web02 | SUCCESS | rc=0 >>
yunjisuan:x:1066:1066:welcom to yunjisuan:/home/yunjisuan:/bin/bash
[[email protected] ~]# ansible Web02 -m shell -a 'tail -1 /etc/shadow'
Web02 | SUCCESS | rc=0 >>
yunjisuan:123123:17783:0:99999:7::: #密碼居然是明文!!!
利用ansible的user模組狀態使用者時要注意在password引數的後邊新增密文,否則不能登陸使用者
通過Python的pip程式安裝passlib即可為密碼加密
#安裝Python2的pip工具,並通過pip工具安裝Python的加密模組來給密碼加密
[[email protected] ~]# yum -y install epel-release
[[email protected] ~]# yum -y install python2-pip
[[email protected] ~]# pip install passlib
#生成密文密碼
[[email protected] ~]# python -c "from passlib.hash import sha512_crypt;import getpass;print sha512_crypt.encrypt(getpass.getpass())"
Password: #輸入你想要加密的密碼
$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1 #加密後的密碼
#刪除之前建立的yunjisuan使用者,並刪除它的家目錄
[[email protected] ~]# ansible Web02 -m user -a 'name=yunjisuan state=absent remove=true'
Web02 | SUCCESS => {
"changed": true,
"force": false,
"name": "yunjisuan",
"remove": true,
"state": "absent"
}
#繼續在Web02上建立yunjisuan使用者
[[email protected] ~]# ansible Web02 -m user -a 'name=yunjisuan comment="welcom to yunjisuan" uid=1066 groups=wheel password=$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1 shell=/bin/bash' home=/home/yunjisuan'
[[email protected] ~]# ansible Web02 -m shell -a 'tail -1 /etc/shadow'
Web02 | SUCCESS | rc=0 >>
yunjisuan:$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1:17783:0:99999:7::: #終於密文了
setup模組批量檢視服務的所有屬性
Ansible中使用setup模組收集,檢視被管理主機的facts(facts是Ansible採集被管理主機裝置資訊的一個功能)。每個被管理主機在接收並執行管理命令之前,都會將自己的相關資訊(作業系統版本,IP地址等)傳送給控制主機
#檢視遠端主機的facts資訊
[[email protected] ~]# ansible Web01 -m setup | head
Web01 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.200.184"
],
"ansible_all_ipv6_addresses": [
"fe80::20c:29ff:fe77:16ad"
],
"ansible_apparmor": {
"status": "disabled"
ansible-playbook的初步使用
playbook可以把ansible的模組進行組合
ln -s /usr/local/python/bin/ansible-playbook /usr/local/bin (優先製作軟連結)
shell模組支援很多模式,copy模組分發檔案或目錄,register模組輸出命令執行結果,nginx_conf配置下發並檢測,vars自定義變數,setupvars內建變數,filevars可變配置檔案
vim test_shell[copy,register,nginx_conf,vars,setupvars,filevars].yaml (執行模板)
ansible-playbook test_shell[copy,register,nginx_conf,vars,setupvars,filevars].yaml (執行配置檔案)
我們可以使用ansible all -m setup | less (檢視ansible內建變數)
vim if.j2 (下發配置檔案模板樣式)
下發配置檔案裡面使用判斷語法
vim /tmp/if.j2
{% if PORT %} #if PORT存在
ip=0.0.0.0:{{ PORT }}
{% else %} #否則的話
ip=0.0.