Ansible命令使用node
Ansible語法使用ansible <pattern_goes_here> -m <module_name> -a <arguments>python
-m NAME,–module-name=NAME:指定執行使用的模塊nginx
-u USERNAME,–user=USERNAME:指定遠程主機以USERNAME運行命令web
-s,–sudo:至關於Linux系統下的sudo命令shell
-USUDO_USERNAME,–sudo-user=SUDO_USERNAME:使用sudo,至關於Linux下的sudo命令vim
-C -check只檢查不實際執行bash
-e EXTRA_VARS,引用外部參數架構
-i INVENTORY,指定倉庫列表,默認/etc/ansible/hostsdom
–list-hosts,列出執行主機列ssh
==========================================================================================
實驗架構:
Master:
Ansible 172.16.250.149
Slave:
node1 172.16.252.245
node2 172.16.251.163
node3 172.16.250.217
==========================================================================================
Ansible的安裝部署
Ansible在epel的yum中有提供,因此配置好epel源,直接使用yum命令安裝便可
yum install ansible
安裝目錄
配置文件目錄:/etc/ansible/
執行文件目錄:/usr/bin/
Lib庫依賴目錄:/usr/lib/pythonX.X/site-packages/ansible/
Help文檔目錄:/usr/share/doc/ansible-X.X.X/
Man文檔目錄:/usr/share/man/man1/
#yum install ansible -y
配置和被管理的主機直接創建基於ssh的密鑰認證
[root@Ansible~]#ssh-keygen #生成密碼 Generatingpublic/privatersakeypair. Enterfileinwhichtosavethekey(/root/.ssh/id_rsa): Createddirectory'/root/.ssh'. Enterpassphrase(emptyfornopassphrase): Entersamepassphraseagain: Youridentificationhasbeensavedin/root/.ssh/id_rsa. Yourpublickeyhasbeensavedin/root/.ssh/id_rsa.pub. Thekeyfingerprintis: 2c:b0:df:16:26:8e:c7:e6:b4:c6:6a:22:e1:18:89:e9root@Ansible Thekey'srandomartimageis: +--[RSA2048]----+ || || |.| |o.| |.o.oS| |*==.| |+o..Bo| |oE..=oo| |.o.oo| +-----------------+
==========================================================================================
添加認證
[root@Ansible~]#ssh-copy-id root@172.16.250.149 ssh-copy-id root@172.16.252.245 ssh-copy-id root@172.16.251.163 ssh-copy-id root@172.16.250.217 [root@Ansible~]#sshroot@172.16.250.149 #驗證 sshroot@172.16.252.245 sshroot@172.16.251.163 sshroot@172.16.250.217
==========================================================================================
定義主機,將全部被管理的主機加入到/etc/ansible/hosts中,不然沒法管理
[root@Ansible~]#vim /etc/ansible/hosts [web] 172.16.250.149 172.16.252.245 172.16.251.163 172.16.250.217
==========================================================================================
#執行ping存活檢測
[root@Ansible~]#ansible web -m ping 172.16.250.217|SUCCESS=>{ "changed":false, "ping":"pong" } 172.16.251.163|SUCCESS=>{ "changed":false, "ping":"pong" } 172.16.250.149|SUCCESS=>{ "changed":false, "ping":"pong" } 172.16.252.245|SUCCESS=>{ "changed":false, "ping":"pong" }
#列出執行主機列表
[root@Ansible /etc/ansible]#ansible web --list-hosts hosts (4): 172.16.250.149 172.16.252.245 172.16.251.163 172.16.250.217
==========================================================================================
[root@Ansible /etc/ansible]#vim ansible.cfg host_key_checking = False [root@Ansible /etc/ansible]#useradd locy #新建用戶 [locy@Ansible ~]$ ssh-keygen [root@node1 ~]#echo "******" | passwd --stdin locy [locy@Ansible ~]$ ssh locy@172.16.252.245 [locy@node1 ~]$ [root@Ansible ~]#su locy [locy@Ansible ~]$ ssh locy@172.16.252.245 Last login: Sat Jul 8 10:29:22 2017 from 172.16.250.149 [locy@node1 ~]$ logout Connection to 172.16.252.245 closed. [locy@Ansible ~]$ ansible 172.16.252.245 -m ping 172.16.252.245 | SUCCESS => { "changed": false, "ping": "pong" }
==========================================================================================
#作免密sodu
[root@node1 ~]#visudo %wheel ALL=(ALL) NOPASSWD: ALL [root@node1 ~]#usermod -G wheel locy
==========================================================================================
對172.16.252.245作ping操做,鏈接用戶locy,以sodu方式運行
[locy@Ansible ~]$ ansible 172.16.252.245 -m ping -u locy -b 172.16.252.245 | SUCCESS => { "changed": false, "ping": "pong" } 或使用root方式 (不建議) [locy@Ansible ~]$ ansible 172.16.252.245 -m ping -u locy -b --become-user=root
==========================================================================================
Ansible經常使用模塊詳解
ansible <host-pattern> [-m module_name] [-a args] [options] #ansible命令格式
指定主機組或ip地址 指定調用模塊 傳遞給模塊的參數
ansible-doc -l 查看全部模塊
ansible-doc command 查看command模塊詳細信息
ansible-doc -s command 查看command模塊詳細用法
==========================================================================================
Command
命令模塊接受命令名稱,後面是空格分隔的列表參數。給定的命令將在全部選定的節點上執行。它不會經過shell進行處理,好比$HOME和操做如」小於」<「,」>」, 「|」, 「;」,」&」‘ 工做(須要使用(shell)模塊實現這些功能)。
action: command
chdir # 在執行命令以前,先切換到該目錄
creates # 一個文件名,當這個文件存在,則該命令不執行,能夠用來作判斷
executable # 切換shell來執行命令,須要使用命令的絕對路徑
free_form # 要執行的Linux指令,通常使用Ansible的-a參數代替。
removes # 一個文件名,這個文件不存在,則該命令不執行,與creates相反的判斷
==========================================================================================
對全部機器使用pwd命令
#-m 指定使用的模塊command -a 傳遞給模塊的參數
[root@Ansible ~]#ansible web -m command -a 'pwd' 172.16.250.217 | SUCCESS | rc=0 >> /root 172.16.252.245 | SUCCESS | rc=0 >> /root 172.16.251.163 | SUCCESS | rc=0 >> /root 172.16.250.149 | SUCCESS | rc=0 >> /root
==========================================================================================
查看磁盤使用狀況並將內容傳輸到/tmp/df.txt中
[root@Ansible ~]#ansible web -m shell -a 'df -h > /tmp/df.txt'
對/tmp/df.txt進行查看
[root@Ansible ~]#ansible web -m command -a 'cat /tmp/df.txt'
批量添加用戶
[root@Ansible ~]#ansible web -m command -a 'useradd Tom'
==========================================================================================
shell
執行的命令中有管道或者變量,就須要使用shell
action: shell
chdir # 執行以前,先cd到指定目錄在執行命令
creates # 一個文件名,當這個文件存在,則該命令不執行
executable # 切換shell來執行命令,須要使用命令的絕對路徑
free_form # 執行的命令
removes # 一個文件名,這個文件不存在,則該命令不執行
==========================================================================================
對/tmp/df.txt進行查看
[root@Ansible ~]#ansible web -m shell -a 'cat /tmp/df.txt'
給上步添加的用戶設定密碼
[root@Ansible ~]#ansible web -m shell -a 'echo rookie | passwd --stdin Tom'
==========================================================================================
copy
複製模塊,將文件複製到被管理主機
action: copy
backup # 建立一個備份文件包括時間戳信息,若是以某種方式重創錯了,還能夠拿回原始文件
content # 取代src=,表示直接用此處指定的信息生成爲目標文件內容
dest # 遠程節點存放文件的路徑,必須是絕對路徑
directory_mode # 遞歸複製設置目錄權限,默認爲系統默認權限
force # 若是目標主機包含該文件,但內容不一樣,若是設置爲yes,則強制覆蓋,若是設置爲no,則只有當目標主機的目標位置不存在該文件時,才複製。默認爲yes
group # 複製到遠程主機後,指定文件或目錄的屬組
mode # 複製到遠程主機後,指定文件或目錄權限,相似與chmod指明如 0644
owner # 複製到遠程主機後,指定文件或目錄屬主
src # 要複製到遠程主機的文件在本地的地址,能夠是絕對路徑,也能夠是相對路徑。若是路徑是一個目錄,它將遞歸複製。在這種狀況下,若是路徑使用」/」來結尾,則只複製目錄裏的內容,若是沒有使用」/」來結尾,則包含目錄在內的整個內容所有複製,相似於rsync。
==========================================================================================
將本地的/etc/fatab文件複製到目標主機的/tmp/ansible.log,屬主爲roo,屬組爲locy,權限爲640,並備份
[root@Ansible ~]#ansible web -m copy -a 'src=/etc/fstab dest=/tmp/ansible.log owner=root group=locy mode=640 backup=yes'
對上一步的操做結果進行查看
[root@Ansible ~]#ansible web -m shell -a 'ls -l /tmp/ansible.log' 172.16.250.217 | SUCCESS | rc=0 >> -rw-r-----. 1 root locy 541 7月 9 20:10 /tmp/ansible.log 172.16.250.149 | SUCCESS | rc=0 >> -rw-r-----. 1 root locy 541 7月 9 20:10 /tmp/ansible.log 172.16.252.245 | SUCCESS | rc=0 >> -rw-r----- 1 root locy 541 7月 9 08:10 /tmp/ansible.log 172.16.251.163 | SUCCESS | rc=0 >> -rw-r----- 1 root locy 541 7月 9 20:10 /tmp/ansible.log
==========================================================================================
cron
定時任務模塊,設置管理節點生成定時任務
action: cron
backup # 若是設置,建立一個crontab備份
cron_file # 若是指定, 使用這個文件cron.d,而不是單個用戶crontab
day # 日應該運行的工做( 1-31, *, */2, etc )
hour # 小時( 0-23, *, */2, etc )
job # 指明運行的命令是什麼
minute # 分鐘( 0-59, *, */2, etc )
month # 月( 1-12, *, */2, etc )
name # 定時任務描述
reboot # 任務在重啓時運行,不建議使用,建議使用special_time
special_time # 特殊的時間範圍,參數:reboot(重啓時),annually(每一年),monthly(每個月),weekly(每週),daily(天天),hourly(每小時)
state # 指定狀態,prsent表示添加定時任務,也是默認設置,absent表示刪除定時任務
user # 以哪一個用戶的身份執行
weekday # 周( 0-6 for Sunday-Saturday, *, etc )
==========================================================================================
天天凌晨三點、四點、五點、六點將磁盤使用狀況保存在/tmp/df.log
[root@Ansible ~]#ansible web -m cron -a 'name="harddrive check" minute="15" hour="3,4,5,6" job="df -lh >> /tmp/df.log"'
每十分鐘將磁盤使用狀況保存在/tmp/df.log
[root@Ansible ~]#ansible web -m cron -a 'name="harddrive check2" minute="*/10" job="df -lh >> /tmp/df.log"' [root@Ansible ~]#crontab -l #Ansible: harddrive check 15 3,4,5,6 * * * df -lh >> /tmp/df.log #Ansible: harddrive check2 */10 * * * * df -lh >> /tmp/df.log
將harddrive check刪除
[root@Ansible ~]#ansible web -m cron -a 'name="harddrive check" state=absent'
======================================================================================
fetch
遠程文件複製到本地
dest #保存文件的目錄
fail_on_missing #當設置爲yes時,若是源文件丟失,任務將會失敗
flat #容許覆蓋將主機名/路徑/文件/文件附加到目的地的默認行爲
src #獲取遠程系統上的文件。這必須是一個文件,而不是一個文件目錄
validate_checksum #在獲取文件以後驗證源和目標校驗和
==========================================================================================
將遠程文件/tmp/df.txt複製到本地/root/下
[root@Ansible ~]#ansible web -m fetch -a 'src=/tmp/df.txt dest=/root/'
==========================================================================================
file
文件操做模塊,設置文件屬性
action: file
force # 須要在兩種狀況下強制建立軟鏈接,一種是源文件不存在但以後會創建的狀況下;另外一種是目標鏈接已存在,須要先取消以前的軟鏈接,有兩個選項:yes|no
group # 設置文件或目錄的屬組
mode # 設置文件或目錄的權限
owner # 設置文件或目錄的屬主
path # 必選項,定義文件或目錄的路徑
recurse # 遞歸設置文件的屬性,只對目錄有效
src # 要被連接到的路徑,只應用與state=link的狀況
state # directory:若是目錄不存在,建立目錄
==========================================================================================
查看web組下的全部主機的/tmp/df.txt
[root@Ansible ~]#ansible web -m shell -a 'ls -l /tmp/df.txt' 172.16.250.217 | SUCCESS | rc=0 >> -rw-r--r--. 1 root root 562 7月 9 19:18 /tmp/df.txt 172.16.250.149 | SUCCESS | rc=0 >> -rw-r--r--. 1 root root 535 7月 9 19:18 /tmp/df.txt 172.16.251.163 | SUCCESS | rc=0 >> -rw-r--r-- 1 root root 615 7月 9 19:18 /tmp/df.txt 172.16.252.245 | SUCCESS | rc=0 >> -rw-r--r-- 1 root root 535 7月 9 07:18 /tmp/df.txt
將web組下的全部主機的/tmp/df.txt權限改成600屬主屬組爲locy
[root@Ansible ~]#ansible web -m file -a 'path=/tmp/df.txt state=touch mode="600" owner=locy group=locy' 172.16.250.217 | SUCCESS | rc=0 >> -rw-------. 1 locy locy 562 7月 9 21:41 /tmp/df.txt 172.16.250.149 | SUCCESS | rc=0 >> -rw-------. 1 locy locy 535 7月 9 21:41 /tmp/df.txt 172.16.252.245 | SUCCESS | rc=0 >> -rw------- 1 locy locy 535 7月 9 09:41 /tmp/df.txt 172.16.251.163 | SUCCESS | rc=0 >> -rw------- 1 locy locy 615 7月 9 21:41 /tmp/df.txt
==========================================================================================
在root下建立file目錄
[root@Ansible ~]#ansible web -m file -a 'path=/root/file state=directory' [root@Ansible ~]#ls file
==========================================================================================
hostname
設置系統的主機名
將172.16.250.149主機名改成master
[root@Ansible ~]#ansible 172.16.250.149 -m hostname -a 'name=master' [root@Ansible ~]#hostname master
==========================================================================================
yum
基於yum源安裝程序
action: yum
conf_file # yum的配置文件
disable_gpg_check # 關閉gpg_check
disablerepo # 不啓用某個源
enablerepo # 啓用某個源
name= # 指定要安裝的包,若是有多個版本須要指定版本,不然安裝最新的包
state # 安裝(present),安裝最新版(latest),卸載程序包(absent)
==========================================================================================
爲web組全部主機安裝nginx 且爲最新版本
[root@Ansible ~]#ansible web -m yum -a 'name=nginx state=latest'
==========================================================================================
service
服務管理模塊
action: service
arguments # 向服務傳遞的命令行參數
enabled # 設置服務開機自動啓動,參數爲yes|no
name # 控制服務的名稱
pattern # 定義一個模式,若是經過status指令來查看服務的狀態時,沒有響應,就會經過ps指令在進程中根據該模式進行查找,若是匹配到,則認爲該服務依然在運行
runlevel # 設置服務自啓動級別
sleep # 若是執行了restarted,則在stop和start之間沉睡幾秒鐘
state # 啓動started 關閉stopped 從新啓動restarted 重載reloaded
==========================================================================================
web組全部主機啓動nginx
[root@Ansible ~]#ansible web -m service -a 'name=nginx state=started'
web組全部主機關閉nginx
[root@Ansible ~]#ansible web -m service -a 'name=nginx state=stopped'
web組全部主機重啓nginx
[root@Ansible ~]#ansible web -m service -a 'name=nginx state=restarted'
web組全部主機重載nginx配置文件
[root@Ansible ~]#ansible web -m service -a 'name=nginx state=reloaded'
web組全部主機啓動nginx,並開機啓動/不啓動
[root@Ansible ~]#ansible web -m service -a 'name=nginx state=started enabled=yes/no'
==========================================================================================
group
用戶組模塊,添加或刪除組
action: group
gid # 設置組的GID號
name= # 管理組的名稱
state # 指定組狀態,默認爲建立,設置值爲absent爲刪除
system # 設置值爲yes,表示爲建立系統組
==========================================================================================
建立名爲tom的組
[root@Ansible ~]#ansible web -m group -a 'name=tom state=present'
==========================================================================================
user
用戶模塊,管理用戶賬號
action: user
comment # 用戶的描述信息
createhome # 是否建立家目錄
force # 在使用state=absent是, 行爲與userdel –force一致.
group # 指定基本組
groups # 指定附加組,若是指定爲(groups=)表示刪除全部組
home # 指定用戶家目錄
login_class # 能夠設置用戶的登陸類 FreeBSD, OpenBSD and NetBSD系統.
move_home # 若是設置爲home=時, 試圖將用戶主目錄移動到指定的目錄
name # 指定用戶名
non_unique # 該選項容許改變非惟一的用戶ID值
password # 指定用戶密碼
remove # 在使用state=absent時, 行爲是與userdel –remove一致
shell # 指定默認shell
state # 設置賬號狀態,不指定爲建立,指定值爲absent表示刪除
system # 當建立一個用戶,設置這個用戶是系統用戶。這個設置不能更改現有用戶
uid # 指定用戶的uid
update_password # 更新用戶密碼
==========================================================================================
建立用戶tom,用戶信息爲tom is tom,uid爲1066,基本組爲tom,附加組爲wheel,shell類型爲zshell,用戶家目錄爲/home/tomhome
[root@Ansible ~]#ansible web -m user -a 'name=tom comment="tom is tom" uid=1066 group=tom groups=wheel shell=/bin/zshell home=/home/tomhome' [root@Ansible ~]#getent passwd tom tom:x:1066:1002:tom is tom:/home/tomhome:/bin/zshell
==========================================================================================
script
在指定節點運行服務端的腳本
[root@Ansible ~]#vim test.sh #/bin/bash touch /tmp/test.sh.log #建立/tmp/test.sh.log echo "hello" >> /tmp/test.sh.log #將date命令結果輸出到/tmp/test.sh.log 在web組中全部主機執行/root/test.sh腳本 [root@Ansible ~]#ansible web -m script -a '/root/test.sh' [root@Ansible ~]#cat /tmp/test.sh.log hello [root@node1 ~]#cat /tmp/test.sh.log hello 查看172.16.251.163主機下的/tmp/test.sh.log [root@Ansible ~]#ansible 172.16.251.163 -m shell -a ‘cat /tmp/test.sh.log’ 172.16.251.163 | SUCCESS | rc=0 >> hello
==========================================================================================