ansible 運(yùn)維使用(一)

ansible 運(yùn)維使用(二)之Playbook
官網(wǎng) https://www.ansible.com/
官網(wǎng)文檔 https://docs.ansible.com/
中文文檔 http://www.ansible.com.cn/ < Ansible中文權(quán)威指南>
安裝 pip install ansible
創(chuàng)建工作目錄 mkdir -p /opt/ansible
復(fù)制配置文件 cp /etc/ansible/ansible.cfg /opt/ansible/
修改 vim /opt/ansible/ansible.cfg
# 修改 hosts目錄
inventory      = /opt/ansible/hosts
創(chuàng)建hosts vim /opt/ansible/hosts
[docker]  # 組名
172.17.0.1  ansible_ssh_user=root ansible_ssh_pass=fenglican
172.17.0.2  ansible_ssh_user=root ansible_ssh_pass=fenglican
# 172.17.0...  可以有很多主機(jī)
# ansible_ssh_user 登陸用戶
# ansible_ssh_pass 登陸密碼
ansible命令格式

ansible <host-pattern> [-f forks] [-m module_name] [-a args]

command模塊 [執(zhí)行遠(yuǎn)程命令] (默認(rèn))

ansible testservers -m command -a 'uname -n'

script模塊 [在遠(yuǎn)程主機(jī)執(zhí)行主控端的shell/python腳本 ] (使用相對(duì)路徑)

ansible testservers -m script -a '/etc/ansible/test.sh

shell模塊 [執(zhí)行遠(yuǎn)程主機(jī)的shell/python腳本]

ansible testservers -m shell -a 'bash /root/test.sh'

raw模塊 [類似于command模塊、支持管道傳遞]

ansible testservers -m raw -a "ifconfig eth0 |sed -n 2p |awk '{print \$2}' |awk -F: '{print \$2}'"

將控制端的文件cp到被控端

ansible docker -m copy -a "src=./hosts dest=/root/Alic/"

常用模塊介紹

copy模塊:http://docs.ansible.com/ansible/copy_module.html
參數(shù):
src:本地文件的路徑,如果源是一個(gè)目錄,會(huì)將目錄中所有的文件都copy過(guò)去
dest:遠(yuǎn)程主機(jī)的絕對(duì)路徑
owner:文件屬主
group:文件屬組
mode:文件權(quán)限
命令演示:
ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab owner=root mode=0644'
fetch模塊:http://docs.ansible.com/ansible/fetch_module.html
下載文件,文件拉取模塊主要是將遠(yuǎn)程主機(jī)中的文件拷貝到本機(jī)中,和copy模塊的作用剛剛相反,
并且在保存的時(shí)候使用hostname來(lái)進(jìn)行保存,當(dāng)文件不存在的時(shí)候,會(huì)出現(xiàn)錯(cuò)誤,
除非設(shè)置了選項(xiàng)fail_on_missing為yes
參數(shù):
dest:用來(lái)存放文件的目錄,例如存放目錄為backup,源文件名稱為/etc/profile在主機(jī)pythonserver中,那么保存為/backup/pythonserver/etc/profile
fail_on_missing:當(dāng)源文件不存在的時(shí)候,標(biāo)識(shí)為失敗
flat:允許覆蓋默認(rèn)行為從hostname/path到/file的,如果dest以/結(jié)尾,它將使用源文件的基礎(chǔ)名稱
src:在遠(yuǎn)程拉取的文件,并且必須是一個(gè)file,不能是目錄
validate_checksum:當(dāng)文件fetch之后進(jìn)行md5檢查
命令演示:
ansible 192.168.10.1 -S -m fetch -a "dest=tmp_file src=/tmp/ttt.txt"

192.168.10.1 | SUCCESS => {
    "changed": true, 
    "checksum": "a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0", 
    "dest": "/opt/ansible/tmp_file/192.168.10.1/tmp/ttt.txt", 
    "md5sum": "ba1f2511fc30423bdbb183fe33f3dd0f", 
    "remote_checksum": "a8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0", 
    "remote_md5sum": null
}

#ll /opt/ansible/tmp_file/192.168.10.1/tmp/ttt.txt 
-rw-r--r-- 1 root root 4 Aug  8 16:26 /opt/ansible/tmp_file/192.168.10.1/tmp/ttt.txt
file模塊:http://docs.ansible.com/ansible/file_module.html
功能:設(shè)置文件屬性、創(chuàng)建符號(hào)鏈接、創(chuàng)建目錄等
參數(shù):
path:指明文件路徑,可以使用name或dest來(lái)代替
owner:文件屬主
group:文件屬組
mode:文件權(quán)限
創(chuàng)建文件的符號(hào)鏈接:
src:指明源文件
dest:指明符號(hào)鏈接文件路徑
命令演示:
ansible pms -m file -a 'src=/tmp/fstab dest=/srv/fstab state=link'

# ll
lrwxrwxrwx 1 root root      10 Jul  6 14:08 fstab -> /tmp/fstab
ping模塊:http://docs.ansible.com/ansible/ping_module.html
功能:測(cè)試被管理主機(jī)的連通性
命令演示:
ansible all -m ping

172.16.206.134 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
command模塊:http://docs.ansible.com/ansible/command_module.html
功能:在遠(yuǎn)程主機(jī)上執(zhí)行命令
注意:command模塊不支持管道符,這也是command模塊和shell模塊的區(qū)別。
命令演示:
ansible all -m command -a 'hostname'  

172.16.206.134 | SUCCESS | rc=0 >>
localhost.localdomain
user模塊:http://docs.ansible.com/ansible/user_module.html
功能:在遠(yuǎn)程主機(jī)上創(chuàng)建或者刪除用戶
參數(shù):
name:賬戶名
state:
          present:創(chuàng)建
          absent:刪除   
group:指定用戶的基本組
uid:指定uid
system:創(chuàng)建系統(tǒng)用戶 值為yes 或者no
命令演示:
ansible pms -m user -a 'name=test state=present uid=306 group=root  system=yes'

172.16.206.134 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 0, 
    "home": "/home/test", 
    "name": "test", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 306
}
service模塊:http://docs.ansible.com/ansible/service_module.html
功能:管理遠(yuǎn)程主機(jī)上的服務(wù)狀態(tài)
參數(shù):
enabled=:是否開機(jī)自動(dòng)啟動(dòng),取值為yes或者no。enabled=yes,表示服務(wù)開啟啟動(dòng)
name=:服務(wù)名
state=: 服務(wù)狀態(tài)
    started:?jiǎn)?dòng)
    restarted:重啟
    stopped:停止
    reloaded:重載
命令演示:
ansible pms -m service -a 'name=zabbix-agent state=started enabled=yes'

172.16.206.134 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "zabbix-agent", 
    "state": "started"
}

script模塊:http://docs.ansible.com/ansible/script_module.html
功能:在遠(yuǎn)程主機(jī)執(zhí)行主控端的shell/python腳本
ansible pms -m script -a '/tmp/echo.sh'

172.16.206.134 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "", 
    "stdout_lines": []
}

shell模塊:http://docs.ansible.com/ansible/shell_module.html
shell模塊支持管道符,這是它與commands模塊的最大區(qū)別
命令演示:
ansible pms -m shell -a 'ps -ef | grep nginx'    
yum模塊:http://docs.ansible.com/ansible/yum_module.html
功能:在遠(yuǎn)程主機(jī)上安裝軟件包
參數(shù):
name=:  包名,如果從遠(yuǎn)程服務(wù)器本地安裝某個(gè)包,則可以寫該包在遠(yuǎn)程主機(jī)上絕對(duì)的路徑,如name=/srv/jdk/jdk-8u66-linux-x64.rpm
state=:狀態(tài),值為present,absent,lastest
    present、lasted安裝
    absent:卸載
    lastest:安裝最新版的包,相當(dāng)于升級(jí)軟件包
    removed:刪除軟件包
    installed:安裝軟件包
命令演示:
ansible pms -m yum -a 'name=/srv/jdk/jdk-8u66-linux-x64.rpm  state=present'  

172.16.206.134 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": []
}

synchronize模塊:http://docs.ansible.com/ansible/synchronize_module.html
注意:ansible主機(jī)和遠(yuǎn)程主機(jī)上都需要安裝rsync
功能:將ansible主機(jī)上的源目錄下的文件同步到遠(yuǎn)程主機(jī)上
參數(shù):
src:ansible主機(jī)上的源路徑
dest:遠(yuǎn)程主機(jī)上的目標(biāo)路徑
delete:delete=yes時(shí),刪除目標(biāo)路徑下,源路徑中不存在的目錄或者文件
compress:是否開啟壓縮功能,默認(rèn)為開啟
mode:同步模式,默認(rèn)為push,設(shè)置mode=pull,改成pull模式
archive:默認(rèn)開啟了這個(gè)參數(shù),該參數(shù)默認(rèn)開啟了recursive, links, perms, times, owner,group和-D參數(shù)。如果你將該參數(shù)設(shè)置為no,那么你將停止很多參數(shù),比如會(huì)導(dǎo)致如下目的遞歸失敗,導(dǎo)致無(wú)法拉取
rsync_opts:增加rsync的額外選項(xiàng),例如
rsync_opts="--exclude=fstab" 表示同步時(shí)文件時(shí),排除fstab文件,即不同步fstab文件。如果有delete=yes選項(xiàng),而目標(biāo)路徑下有一個(gè)源路徑下不存在的文件,如文件名為fstab,那么
rsync_opts="--exclude=fstab"表示不刪除目標(biāo)路徑下的fstab文件
命令演示:
ansible pms  -m synchronize -a 'src=/tmp/test/ dest=/tmp/aaa/ delete=yes  rsync_opts="--exclude=fstab"'
#上面的命令表示將ansible主機(jī)上/tmp/test/目錄下的所有文件(除了fstab)同步到遠(yuǎn)程主機(jī)的/tmp/aaa/目錄下。并刪除/tmp/aaa/目錄下,在/tmp/test/上不存在的文件或者目錄

unarchive模塊:http://docs.ansible.com/ansible/unarchive_module.html
功能:解壓縮,這個(gè)模塊有兩種用法:
1、將ansible主機(jī)上的壓縮包在本地解壓縮后傳到遠(yuǎn)程主機(jī)上,這種情況下,copy=yes
2、將遠(yuǎn)程主機(jī)上的某個(gè)壓縮包解壓縮到指定路徑下。這種情況下,需要設(shè)置copy=no
參數(shù):
copy:默認(rèn)為yes,當(dāng)copy=yes,那么拷貝的文件是從ansible主機(jī)復(fù)制到遠(yuǎn)程主機(jī)上的,如果設(shè)置為copy=no,那么會(huì)在遠(yuǎn)程主機(jī)上尋找src源文件
src:源路徑,可以是ansible主機(jī)上的路徑,也可以是遠(yuǎn)程主機(jī)上的路徑,如果是遠(yuǎn)程主機(jī)上的路徑,則需要設(shè)置copy=no
dest:遠(yuǎn)程主機(jī)上的目標(biāo)路徑
mode:設(shè)置解壓縮后的文件權(quán)限
命令演示:
ansible pms -m unarchive -a 'src=/srv/tomcat8/apache-tomcat-8.0.29.tar.gz dest=/usr/local copy=no mode=0755'

get_url模塊:http://docs.ansible.com/ansible/get_url_module.html
功能:從http、https、ftp下載文件到遠(yuǎn)程主機(jī)
參數(shù):
url:下載地址
dest:遠(yuǎn)程主機(jī)上的目標(biāo)徑路
mode:設(shè)置下載到遠(yuǎn)程主機(jī)后的文件的權(quán)限
命令演示:
ansible pms -m get_url -a 'url=ftp://ftp.cheyaoshicorp.com/pub/臨時(shí)文件/derby.init.sh dest=/tmp'

172.16.206.134 | SUCCESS => {
    "changed": true, 
    "checksum_dest": null, 
    "checksum_src": "770a432e9847e594e0154e31c906062585d571e0", 
    "dest": "/tmp/derby.init.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "4564411c7e614859965c9ab5d76df22b", 
    "mode": "0644", 
    "msg": "OK (3934 bytes)", 
    "owner": "root", 
    "size": 3934, 
    "src": "/tmp/tmp5nqAsJ", 
    "state": "file", 
    "uid": 0, 
    "url": "ftp://ftp.cheyaoshicorp.com/pub/臨時(shí)文件/derby.init.sh"
cron模塊
用于管理計(jì)劃任務(wù)包含如下選項(xiàng): 
backup:對(duì)遠(yuǎn)程主機(jī)上的原任務(wù)計(jì)劃內(nèi)容修改之前做備份 
cron_file:如果指定該選項(xiàng),則用該文件替換遠(yuǎn)程主機(jī)上的cron.d目錄下的用戶的任務(wù)計(jì)劃 
day:日(1-31,*,*/2,……) 
hour:小時(shí)(0-23,*,*/2,……)  
minute:分鐘(0-59,*,*/2,……) 
month:月(1-12,*,*/2,……) 
weekday:周(0-7,*,……)
job:要執(zhí)行的任務(wù),依賴于state=present 
name:該任務(wù)的描述 
special_time:指定什么時(shí)候執(zhí)行,參數(shù):reboot,yearly,annually,monthly,weekly,daily,hourly 
state:確認(rèn)該任務(wù)計(jì)劃是創(chuàng)建還是刪除 
user:以哪個(gè)用戶的身份執(zhí)行
示例:
ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"'
ansible test -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="root
ansible test -m cron  -a 'backup="True" name="test" minute="0" hour="5,2" job="ls -alh > /dev/null"'
ansilbe test -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'
setup模塊
setup模塊,主要用于獲取主機(jī)信息,在playbooks里經(jīng)常會(huì)用到的一個(gè)參數(shù)gather_facts就與該模塊相關(guān)。setup模塊下經(jīng)常使用的一個(gè)參數(shù)是filter參數(shù)
具體使用示例如下:
ansible 10.212.52.252 -m setup -a 'filter=ansible_*_mb'   //查看主機(jī)內(nèi)存信息
ansible 10.212.52.252 -m setup -a 'filter=ansible_eth[0-2]'   //查看地接口為eth0-2的網(wǎng)卡信息
ansible all -m setup --tree /tmp/facts   //將所有主機(jī)的信息輸入到/tmp/facts目錄下,每臺(tái)主機(jī)的信息輸入到主機(jī)名文件中(/etc/ansible/hosts里的主機(jī)名)

使用錯(cuò)誤記錄

ansible報(bào)錯(cuò)Aborting, target uses selinux but python bindings (libselinux-python) aren't installed

yum install libselinux-python -y

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容