- Ansible簡(jiǎn)介
Ansible是新出現(xiàn)的自動(dòng)化運(yùn)維工具,基于Python開發(fā),集合了眾多運(yùn)維工具(puppet、cfengine、chef、func、fabric)的優(yōu)點(diǎn),實(shí)現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運(yùn)行命令等功能。
Ansible是基于模塊工作的,本身沒(méi)有批量部署的能力。真正具有批量部署的是ansible所運(yùn)行的模塊,ansible只是提供一種框架。
主要包括:
(1)連接插件connection plugins:負(fù)責(zé)和被監(jiān)控端實(shí)現(xiàn)通信;
(2)host inventory:指定操作的主機(jī),是一個(gè)配置文件里面定義監(jiān)控的主機(jī);
(3)各種模塊核心模塊、command模塊、自定義模塊;
(4)借助于插件完成記錄日志郵件等功能;
(5)playbook:劇本執(zhí)行多個(gè)任務(wù)時(shí),非必需可以讓節(jié)點(diǎn)一次性運(yùn)行多個(gè)任務(wù)。
- 搭建環(huán)境
管理端:CentOS7-1 192.168.177.145
被管理端:centos7-2 192.168.177.135
被管理端:centos7-3 192.168.177.132
Ansible安裝
192.168.177.145:
# systemctl stop firewalld.service //關(guān)閉防火墻 # setenforce 0 # yum install -y epel-release //安裝epel源 # yum install ansible -y //安裝Ansible # vim /etc/ansible/hosts [abc] 192.168.177.135 [mysql] 192.168.177.132 # ssh-keygen -t rsa //設(shè)置密鑰對(duì) # ssh-copy-id root@192.168.177.135 # ssh-copy-id root@192.168.177.132 //配置密鑰對(duì) # ssh-agent bash //免交互代理 # ssh-add
192.168.177.135(另一臺(tái)也一樣):
# systemctl stop firewalld.service //關(guān)閉防火墻 # setenforce 0 # cd ~/.ssh
Ansible命令行模塊
command模塊
命令格式:ansible [主機(jī)] [-m 模塊] [-a args]
# ansible 192.168.177.135 -m command -a 'date' //指定ip執(zhí)行date # ansible mysql -a 'date' //指定分類執(zhí)行date
cron模塊
用于定義任務(wù)計(jì)劃
兩種狀態(tài)(state):present表示添加(可以省略),absent表示移除。
# ansible-doc -s cron //查看cron模塊信息 # ansible abc -m cron -a 'minute="*/1" job="/usr/bin/echo nihao" name="test nihao"' //添加周期性計(jì)劃任務(wù) # ansible abc -a 'crontab -l' # ansible abc -m cron -a 'name="test nihao" state=absent' //移除計(jì)劃任務(wù),假如該計(jì)劃任務(wù)沒(méi)有取名字,name=None即可
user模塊
用于創(chuàng)建新用戶和更改刪除已存在的用戶
user模塊是請(qǐng)求的是useradd, userdel, usermod三個(gè)指令
# ansible-doc -s user # ansible mysql -m user -a 'name=zhangsan' //創(chuàng)建zhangsan # ansible mysql -m user -a 'name=zhangsan state=absent' //刪除zhangsan
group模塊
對(duì)用戶組進(jìn)行管理
group模塊請(qǐng)求的是groupadd, groupdel, groupmod 三個(gè)指令
# ansible mysql -m group -a 'name=test gid=306 system=yes' //創(chuàng)建test組 # ansible mysql -m user -a 'name=wang' //創(chuàng)建用戶wang # ansible mysql -m group -a 'name=test1 gid=506 system=yes' //創(chuàng)建test1組 # ansible mysql -m user -a 'name=wang uid=506 group=test1 system=yes' //將wang添加到test1組
copy模塊
用于實(shí)現(xiàn)文件復(fù)制和批量下發(fā)文件
# ansible-doc -s copy # ansible abc -m copy -a 'src=/etc/fstab dest=/opt/fstab.bk owner=root mode=644' //將/etc/fstab復(fù)制到被管理端/opt下 # ansible abc -a 'cat /opt/fstab.bk' //查看
file模塊
用于設(shè)置文件屬性
# ansible mysql -m file -a 'path=/opt/test.txt state=touch' //創(chuàng)建空文件 # ansible mysql -m file -a 'path=/opt/test.txt owner=wang group=test1 mode=666' //設(shè)置文件的屬主,屬組和權(quán)限 # ansible mysql -m file -a 'src=/opt/test.txt path=/opt/test.txt.link state=link' //創(chuàng)建鏈接性文件 # ansible mysql -m copy -a 'content="hello" dest=/opt/test.txt' //在test.txt中寫入內(nèi)容
ping模塊
用于測(cè)試指定主機(jī)的連通性
# ansible all -m ping
yum模塊
# ansible abc -m yum -a 'name=httpd' //yum安裝httpd服務(wù)
service模塊
用來(lái)控制管理服務(wù)的運(yùn)行狀態(tài)
# ansible abc -m service -a 'name=httpd enabled=true state=started' //開機(jī)自啟動(dòng)
shell模塊
在被管理端運(yùn)行命令
# ansible mysql -m shell -a 'echo "abc123" | passwd --stdin wang' //創(chuàng)建密碼
script模塊
將本地腳本復(fù)制到被管理端運(yùn)行
# ansible-doc -s script # vi /opt/test.sh #!/bin/bash echo "hello ansible from script"> /opt/script.txt # chmod +x /opt/test.sh # ansible mysql -m script -a '/opt/test.sh'
setup模塊
# ansible mysql -m setup //獲取mysql組主機(jī)的facts信息