ansible命令使用

ansible命令使用python

查看每一個服務器的主機名mysql

1
$ ansible multi -a  "hostname"

 

使用一個線程執行命令,至關於順序在每一個服務器上運行(默認5個線程執行)正則表達式

1
$ ansible multi -a  "hostname"  -f 1

 

查看你的環境狀況:sql

查看磁盤使用狀況shell

1
$ ansible multi -a  "df -h"

查看內存使用狀況數據庫

1
$ ansible multi -a  "free -m"

查看時間是否準確express

1
$ ansible multi -a  "date"

若是時間不一致,能夠使用ntpdate 同步一下django

$ ansoble multi -a "ntpdate cn.pool.ntp.org"centos

 

三:配置兩臺應用服務器bash

前提是安裝好epel源和centos base源(能夠使用阿里雲的鏡像源)

1
2
3
$ ansible app -m yum -a  "name=MySQL-python state=present"
$ ansible app -m yum -a  "name=python-setuptools state=present"
$ ansible app -m easy_install -a  "name=django"

 

測試django是否安裝正確

1
2
3
4
5
6
root@~ #  ansible app -a "python -c 'import django; print django.get_version()'"
10.0.0.131 | success | rc=0 >>
1.10
 
10.0.0.130 | success | rc=0 >>
1.10

 

四:配置數據庫服務器

1
2
$ ansible db -m yum -a  "name=mysql-server state=present"
$ ansible db -m service -a  "name=mysqld state=started enabled=yes"

 

配置數據庫用戶django,而且賦予權限

1
2
3
$ ansible db  -m yum -a  "name=MySQL-python state=present"
$ ansible db  -m mysql_user -a "name=django host=% password=12345 \
priv=*.*:ALL state=present

 

五:限制命令只在一個服務器上生效

1
$ ansible app  -a  "service ntpd restart"  --limit  "10.0.0.132"
1
2
3
# Limit hosts with a simple pattern (asterisk is a wildcard).
$ ansible app  -a  "service ntpd restart"  --limit  "*.4"
#以4結尾的ip地址,將會執行命令
1
2
3
# Limit hosts with a regular expression (prefix with a tilde).
$ ansible app  -a  "service ntpd restart"  --limit ~ ".*\.4"
#使用正則表達式匹配主機

 

六:管理系統用戶和組

系統添加admin組

1
$ ansible app  -m group -a  "name=admin state=present"

系統添加jwh566用戶

1
$ ansible app  -m user -a  "name=jwh5566 group=admin createhome=yes"

刪除系統用戶

1
$ ansible app  -m user -a  "name=jwh5566 state=absent remove=yes"

 

七:管理文件和目錄

獲取文件的信息,權限,全部者等

1
$ ansible multi -m stat -a  "path=/etc/environment"

複製文件到服務器

1
$ ansible multi -m copy -a  "src=/etc/hosts dest=/tmp/hosts"

從服務器接收文件(接收到控制機)

1
$ ansible multi  -m fetch -a  "src=/etc/hosts dest=/tmp"

建立目錄

1
$ ansible multi -m  file  -a  "dest=/tmp/test mode=644 state=directory"

建立符號連接

1
2
$ ansible multi -m  file  -a "src= /src/symlink  dest= /dest/symlink  \
owner=root group=root state=link"

刪除目錄和文件

1
$ ansible multi -m  file  -a  "dest=/tmp/test state=absent"

 

八:運行後臺任務

-B <seconds> 指定運行任務的最大時間

-P <seconds> 指定多久時間去一次服務器查看任務執行的狀態

 

異步更新服務器(根據系統狀況,可能須要很長時間)

1
2
3
4
5
6
7
$ ansible multi  -B 3600 -a  "yum -y update"
     background launch...
     
     10.0.0.132 | success >> {
     "ansible_job_id" "763350539037" ,
     "results_file" "/root/.ansible_async/763350539037" ,
     "started" : 1

 

若是說後臺任務還在運行,使用下面的命令查看運行狀態

1
$ ansible multi -m async_status -a  "jid=763350539037"

 

九:檢查日誌文件

1
$ ansible multi  -a  "tail /var/log/messages"

 

若是須要grep,須要使用shell模塊

1
2
3
4
5
6
7
8
9
10
root@~ #  ansible multi  -m shell -a "tail /var/log/messages | \
  grep  ansible- command  wc  -l"
10.0.0.131 | success | rc=0 >>
2
 
10.0.0.130 | success | rc=0 >>
2
 
10.0.0.141 | success | rc=0 >>
6

這個命令顯示每臺服務器分別執行了幾回ansible命令

 

十:管理crontab 任務

1
2
$ ansible multi  -m  cron  -a "name= 'daily-cron-all-servers'  \
hour=4 job= '/path/to/daily-script.sh' "

能夠使用這個配置ntp 任務

 

刪除crontab任務

1
$ ansible multi  -m  cron  -a  "name='daily-cron-all-servers' state=absent"
相關文章
相關標籤/搜索