Ansible 運維自動化!

Ansible軟件介紹

  1. ansible是一個基於python開發的自動化運維工具!(saltstack)

  2. 其功能的實現是基礎SSH遠程連接服務的

  3. ansible可以實現批量系統配置、批量軟件部署、批量文件拷貝、批量運行命令等功能

軟件特點

  1. 不需要單獨安裝客戶端,基於ssh服務

  2. 不需要安裝服務端

  3. 需要依靠大量的模塊實現批量部署

  4. 配置文件/etc/ansible/ansible.cfg

基礎配置

安裝

yum install ansible

基本配置

/etc/ansible
#ansible。cfg是Ansible工具的配置文件
  hosts 用來配置被管理的機器
  roles 是playbook需要的一個目錄

生成祕鑰

ssh-keygen -t rea

將公鑰寫入服務器,傳入被管理機

ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 22 [email protected]"
#192.168.124.7爲被管理機器的ip

將hosts文件添加到被管理機

vim /etc/ansible/hosts
[zu]
192.168.124.7
192.168.124.9
#zu  爲管理組的名稱,可以自定義

測試Ansible

# -i                   指定 hosts 文件位置
# -u username 指定 SSH 連接的用戶名
# -k                  指定遠程用戶密碼
# -f                   指定併發數
# -s                  如需要 root 權限執行時使用 ( 連接用戶不是 root 時 )
# -K     -s 時,-K 輸入 root 密碼

ansible zu -m ping

# 操作zu中所有主機執行  ping模塊  

Ansible中 常用模塊

ansible <host-pattern>
             [-m module_name]  指定主機組或ip的地址
             [-a  args] 指定調用模塊
           [options] 傳遞給模塊的參數
ansible-doc -l 查看所有模塊
ansible-doc command 查看command模塊詳細信息
ansible-doc -s command 查看command模塊詳細用法

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章