冠縣網(wǎng)站建設做百度推廣需要什么條件
自動化運維
ansible----自動化運維工具
特點:
部署簡單,使用ssh管理
管理端與被管理端不需要啟動服務
配置簡單、功能強大,擴展性強
一、ansible環(huán)境搭建
準備四臺機器
安裝步驟
mo服務器:
#下載epel
[root@mo ~]# yum -y install epel-release.noarch?
#安裝ansible
[root@mo ~]# yum -y install ansible
#查看ansible版本
[root@mo ~]# ansible --version
進行免密登錄
[root@mo ~]# ssh-copy-id -i 192.168.1.25
[root@mo ~]# ssh-copy-id -i 192.168.1.26
編輯配置文件
[root@mo ~]# vim /etc/ansible/hosts?
[group01]
192.168.1.25
192.168.1.26[group02]
192.168.1.25
192.168.1.26
192.168.1.27
測試連接性
[root@mo ~]# ?ansible 192.168.1.25 -m ping
192.168.1.25 | SUCCESS => {
? ? "ansible_facts": {
? ? ? ? "discovered_interpreter_python": "/usr/bin/python"
? ? },?
? ? "changed": false,?
? ? "ping": "pong"
}
設置別名分組
[root@mo ~]# vim /etc/ansible/hosts?
[group01]
192.168.1.25
192.168.1.26other ansible_ssh_host=192.168.1.27 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1
[group02]
192.168.1.25
192.168.1.26
other
再次測試連通性
group01:
[root@mo ~]# ?ansible group01 -m ping
192.168.1.26 | SUCCESS => {
? ? "ansible_facts": {
? ? ? ? "discovered_interpreter_python": "/usr/bin/python"
? ? },?
? ? "changed": false,?
? ? "ping": "pong"
}
192.168.1.25 | SUCCESS => {
? ? "ansible_facts": {
? ? ? ? "discovered_interpreter_python": "/usr/bin/python"
? ? },?
? ? "changed": false,?
? ? "ping": "pong"
}
other | SUCCESS => {
? ? "ansible_facts": {
? ? ? ? "discovered_interpreter_python": "/usr/bin/python"
? ? },?
? ? "changed": false,?
? ? "ping": "pong"
}
group02:
[root@mo ~]# ansible group02 -m ping
192.168.1.26 | SUCCESS => {
? ? "ansible_facts": {
? ? ? ? "discovered_interpreter_python": "/usr/bin/python"
? ? },?
? ? "changed": false,?
? ? "ping": "pong"
}
192.168.1.25 | SUCCESS => {
? ? "ansible_facts": {
? ? ? ? "discovered_interpreter_python": "/usr/bin/python"
? ? },?
? ? "changed": false,?
? ? "ping": "pong"
}
other | SUCCESS => {
? ? "ansible_facts": {
? ? ? ? "discovered_interpreter_python": "/usr/bin/python"
? ? },?
? ? "changed": false,?
? ? "ping": "pong"
}
other:
[root@mo ~]# ansible other -m ping
other | SUCCESS => {
? ? "ansible_facts": {
? ? ? ? "discovered_interpreter_python": "/usr/bin/python"
? ? },?
? ? "changed": false,?
? ? "ping": "pong"
}
更改group02的名字
[root@mo ~]# ansible group02 -m hostname -a 'name=dd'
二、file模塊
file模塊?于對?件相關(guān)的操作(創(chuàng)建, 刪除, 軟硬鏈接等)
1.創(chuàng)建目錄
[root@mo ~]# ansible group01 -m file -a 'path=/tmp/abc state=directory'
查看
2.創(chuàng)建文件
[root@mo ~]# ansible group02 -m file -a 'path=/tmp/abc/def state=touch'
查看
3.遞歸修改
[root@mo ~]# ansible group02 -m file -a 'path=/tmp/abc recurse=yes owner=bin group=daemon mode=1777'
查看
[root@s0 ~]# ll /tmp/abc/
總用量 0-rwxrwxrwt 1 bin daemon 0 8月 16 14:08 def
4.修改文件
[root@mo ~]# ansible group02 -m file -a 'path=/tmp/abc state=absent'
[root@mo ~]# ansible group02 -m file -a 'path=/tmp/aaaa state=touch owner=bin group=de=1777'
查看
[root@s0 ~]# ls -l /tmp/
總用量 0-rwxrwxrwt 1 bin daemon 0 8月 16 14:21 aaaa
5.刪除文件
[root@mo ~]# ansible group02 -m file -a 'path=/tmp/aaaa state=absent'
6.創(chuàng)建鏈接文件
創(chuàng)建軟連接
[root@mo ~]# ansible group02 -m file -a 'src=/etc/fstab path=/tmp/xxx state=link'
創(chuàng)建硬鏈接
[root@mo ~]# ansible group02 -m file -a 'src=/etc/fstab path=/tmp/xxx2 state=hard'
查看
[root@s0 ~]# ll /tmp/
lrwxrwxrwx 1 root root 10 8月 16 14:31 xxx -> /etc/fstab
-rw-r--r--. 2 root root 502 5月 25 18:31 xxx2
7.獲取文件統(tǒng)計信息
[root@mo ~]# ansible group02 -m stat -a 'path=/etc/fstab'
三、copy模塊
copy模塊?于對?件的遠程拷?操作(如把本地的?件拷?到遠程 的機器上)
使?content參數(shù)直接往遠程?件?寫內(nèi)容(會覆蓋原內(nèi)容)
使?force參數(shù)控制是否強制覆蓋
使?backup參數(shù)控制是否備份?件
copy模塊拷?時要注意拷??錄后?是否帶"/"符號
沒有"/"把表示拷貝目錄,有"/"表示拷貝目錄下的文件
用于遠程拷貝文件
給虛擬機傳mysql包
[root@mo ~]# rz -E
rz waiting to receive.
查看
[root@mo ~]# ls
anaconda-ks.cfg ?mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
改名
[root@mo ~]# mv mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz mysql57.tar.gz
復制
將當前目錄下的mysql57.tar.gz文件復制到group02組內(nèi)所有機器的用戶主目錄中?
[root@mo ~]# ansible group02 -m copy -a 'src=./mysql57.tar.gz dest=~'
查看
[root@s0 ~]# ls -lh
總用量 663M
-rw-------. 1 root root 1.4K 5月 ?25 16:47 anaconda-ks.cfg
-rw-r--r-- ?1 root root 663M 8月 ?16 15:07 mysql57.tar.gz[root@s1 ~]# ls -lh
總用量 663M
-rw-------. 1 root root 1.4K 5月 ?25 16:47 anaconda-ks.cfg
-rw-r--r-- ?1 root root 663M 8月 ?16 15:07 mysql57.tar.gz[root@s2 ~]# ls -lh
總用量 663M
-rw-------. 1 root root 1.4K 5月 ?25 16:47 anaconda-ks.cfg
-rw-r--r-- ?1 root root 663M 8月 ?16 15:07 mysql57.tar.gz
寫入內(nèi)容
[root@mo ~]# ansible group02 -m copy -a 'content="wo shi dd chao ji wu di yu zhou bao long zhan shen" dest=~/tst'
查看
[root@s0 ~]# ls
anaconda-ks.cfg ?mysql57.tar.gz ?tst
[root@s0 ~]# cat tst
wo shi dd chao ji wu di yu zhou bao long zhan shen
?
四、user模塊
user模塊?于管理?戶賬號和?戶屬性。
創(chuàng)建aaa?戶,默認為普通?戶,創(chuàng)建家?錄
ansible group1 -m user -a ‘name=aaa state=present’
創(chuàng)建bbb系統(tǒng)?戶,并且登錄shell環(huán)境為/sbin/nologin
ansible group1 -m user -a ‘name=bbb state=present system=yes shell="/sbin/nologin"’
創(chuàng)建ccc?戶, 使?uid參數(shù)指定uid, 使?password參數(shù)傳密碼
echo 123456 | openssl passwd -1 -stdin
$1$DpcyhW2G$Kb/y1f.lyLI4MpRlHU9oq0
下?句命令注意?下格式,密碼要?雙引號引起來,單引號的話驗證時會密碼不正確
ansible group1 -m user -a 'name=ccc uid=2000 state=present password="$1$DpcyhW2G$Kb/y1f.lyLI4MpRlHU9oq0"'
創(chuàng)建?個普通?戶叫hadoop,并產(chǎn)?空密碼密鑰對
ansible group1 -m user -a 'name=hadoop generate_ssh_key=yes'
刪除aaa?戶,但家?錄默認沒有刪除
ansible group1 -m user -a 'name=aaa state=absent'
刪除bbb?戶,使?remove=yes參數(shù)讓其刪除?戶的同時也刪除家?錄
ansible group1 -m user -a 'name=bbb state=absent remove=yes'
五、group模塊
group模塊?于管理?戶組和?戶組屬性。
創(chuàng)建組
ansible group1 -m group -a 'name=groupa gid=3000 state=present'
刪除組(如果有?戶的gid為此組,則刪除不了)
ansible group1 -m group -a 'name=groupa state=absent'
六、cron模塊
cron模塊?于管理周期性時間任務。
創(chuàng)建?個cron任務,不指定user的話,默認就是root
如果minute,hour,day,month,week不指定的話,默認都為*
ansible group1 -m cron -a 'name="test cron1" user=root job="touch /tmp/111" minute=*/2'
刪除cron任務
ansible group1 -m cron -a 'name="test cron1" state=absent'
七、yum模塊
yum模塊?于使?yum命令來實現(xiàn)軟件包的安裝與卸載。
使?yum安裝?個軟件(前提:group1的機器上的yum配置都已經(jīng)OK)
ansible group1 -m yum -a 'name=vsftpd state=present'
使?yum安裝httpd,httpd-devel軟件,state=latest表示安裝最新版本
ansible group1 -m yum -a 'name=httpd,httpd-devel state=latest'
使?yum卸載httpd,httpd-devel軟件
ansible group1 -m yum -a 'name=httpd,httpddevel state=absent'
八、service模塊
service模塊?于控制服務的啟動,關(guān)閉,開機?啟動等。
啟動vsftpd服務,并設為開機?動啟動
ansible group1 -m service -a 'name=vsftpd state=started enabled=on'
關(guān)閉vsftpd服務,并設為開機不?動啟動
ansible group1 -m service -a 'name=vsftpd state=stopped enabled=false'
九、其他模塊
hostname模塊
hostname模塊?于修改主機名(注意: 它不能修改/etc/hosts?件)
stat模塊
stat模塊類似linux的stat命令,?于獲取?件的狀態(tài)信息。
template模塊
template模塊?先使?變量渲染jinja2模板?件成普通?件,然后再復制過去.?copy模塊不?持.(jinja2是?個基于python的模板引擎)
template模塊不能拷??錄
fetch模塊
fetch模塊與copy模塊類似,但作?相反。?于把遠程機器的?件拷?到本地。
注意: fetch模塊不能從遠程拷??錄到本地
yum_repository模塊
yum_repository模塊?于配置yum倉庫。
增加?個/etc/yum.repos.d/local.repo配置?件
ansible group1 -m yum_repository -a "name=local description=localyum baseurl=file:///mnt/ enabled=yes gpgcheck=no"
注意:此模塊只幫助配置yum倉庫,但如果倉庫?沒有軟件包,安裝? 樣會失敗。所以可以?動去掛載光驅(qū)到/mnt?錄
# mount /dev/cdrom /mnt
刪除/etc/yum.repos.d/local.repo配置?件
ansible group1 -m yum_repository -a "name=local state=absent"
script模塊
script模塊?于在遠程機器上執(zhí)?本地腳本。
在master上準備?個腳本
?vim /tmp/1.sh
#!/bin/bash
mkdir /tmp/haha touch /tmp/haha/{1..10}
在group1的遠程機器?都執(zhí)?master上的/tmp/1.sh腳本
(此腳本 不?給執(zhí)?權(quán)限)
ansible group1 -m script -a '/tmp/1.sh'
command與shell模塊
兩個模塊都是?于執(zhí)?linux命令的,這對于命令熟悉的?程師來說,?起來?常high。
shell模塊與command模塊差不多
(command模塊不能執(zhí)??些類似$HOME,>,等符號,但shell可以)
注意: shell模塊并不是百分之百任何命令都可以,?如vim或ll別名就不可以。