1.ansible基本概述
2.ansible的實(shí)現(xiàn)
3.ansible ad-hoc 單模塊的操作
一精拟、Ansible基礎(chǔ)概述
1.什么是Ansible
Ansible是一個(gè)自動(dòng)化的配置管理工具枉长,通過一個(gè)命令完成一系列的操作斧散,進(jìn)而減少重復(fù)行的工作早抠,提高工作的效率
2.功能
①批量執(zhí)行遠(yuǎn)程命令
②批量配置軟件服務(wù)
③實(shí)現(xiàn)軟件開發(fā)功能
④編程高級(jí)的IT任務(wù),playbook用來描繪一套IT架構(gòu)(劇本)
3.特點(diǎn)
易學(xué)泡态、操作靈活胜宇、簡(jiǎn)單應(yīng)用、安全可靠作瞄、移植性高
4.架構(gòu)
控制端茶宵、被控端、ssh宗挥、inventory乌庶、ad-hoc、playbook
二契耿、ansible的實(shí)現(xiàn)
1.安裝配置環(huán)境
1.安裝
[root@manager ~]# yum install ansible -y
2.配置ansible.cfg瞒大,指定,主機(jī)清單的目錄
[root@manager ansible_adhoc]# cat ansible.cfg
[defaults]
inventory = ./hosts
3.定義主機(jī)清單
[root@manager ~]# cat /etc/ansible/hosts
[webservers]
172.16.1.7
172.16.1.8
4.推送公鑰給各個(gè)節(jié)點(diǎn)
5.執(zhí)行ansible ad-hoc 測(cè)試是否能與該清單節(jié)點(diǎn)通訊
[root@manager ~]# ansible webservers -m ping
2.配置文件查找順序
1.$ANSIBLE_CONFIG
2.當(dāng)前目錄下的ansible.cfg
3.當(dāng)前用戶家目錄下的ansible.cfg
4.查找/etc/ansible/ansible.cfg
總結(jié):一般我們會(huì)創(chuàng)建一個(gè)目錄搪桂,然后切換到該目錄下透敌,創(chuàng)建ansible.cfg和hosts文件,然后進(jìn)行ansible ad-hoc操作
3..主機(jī)清單的寫法
1.基于密碼的方式
[webservers]
172.16.1.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='密碼'
2.基于秘鑰的方式
[webservers]
172.16.1.31
#172.16.1.31 hostname=nfs
#nfs ansible_ssh_host=172.16.1.31 #顯示主機(jī)名
三、ansible ad-hoc單個(gè)模塊的操作
臨時(shí)命令酗电,執(zhí)行完即結(jié)束淌山,不會(huì)保存
語法:
ansible [主機(jī)或主機(jī)組名] -m [模塊] -a ['動(dòng)作']
提示:
綠色(未更改)
黃色(發(fā)生了更改)
紅色(報(bào)錯(cuò))
主要模塊
模塊名稱 | 具體 |
---|---|
命令 | command、shell |
安裝 | yum顾瞻、yum_repository |
配置 | copy泼疑、file |
啟動(dòng) | systemd、service |
掛載 | mount |
定時(shí) | cron |
用戶 | user荷荤、group |
防火墻 | selinux退渗、firewalld |
1.command 和 shell 模塊
區(qū)別:shell支持通道,而command不支持
[root@manager ansible_adhoc]# ansible webserver -m command -a 'df -h'
[root@manager ansible_adhoc]# ansible webservers -m shell -a 'ps -ef |grep nginx'
2.yum 模塊
[root@manager ansible_adhoc]# ansible webserver -m yum -a 'name=nginx state=present'
name 軟件包名稱蕴纳,或下載地址
state:
present 安裝(默認(rèn))
absent 卸載
latest 更新
enablerepo 指定哪個(gè)倉庫下載
disablerepo 禁止哪個(gè)倉庫下載
3.copy 模塊
[root@manager ansible_adhoc]# ansible webservers -m copy -a 'src=./exports.j2 dest=/etc/exports owner=root group=root mode=644'
src 源文件
dest 目標(biāo)位置
owner 設(shè)定文件屬主
group 設(shè)定文件屬組
mode 設(shè)定文件權(quán)限
backup 備份(只在文件有變化時(shí)執(zhí)行)
content 往目標(biāo)主機(jī)文件中增加內(nèi)容(覆蓋重定向)
4.group 和 user 模塊
[root@manager ansible_adhoc]# ansible webservers -m group -a 'name=www gid=666'
[root@manager ansible_adhoc]# ansible webservers -m user -a 'name=www uid=666 group=www create_home=no shell=/sbin/nologin'
groups 指定附加組
create_home 創(chuàng)不創(chuàng)建家目錄
shell 指定登錄shell
state:
present 創(chuàng)建(默認(rèn))
absent 刪除
remove=yes 移除用戶相關(guān)的文件(例如家目錄)
#創(chuàng)建jsm用戶会油,為其添加123作為登錄密碼,并且創(chuàng)建家目錄
[root@manager ansible_adhoc]# ansible localhost -m debug -a "msg={{ '123' | password_hash('sha512', 'salt') }}"
[root@manager ansible_adhoc]# ansible webservers -m user -a 'name=jsm password="$6$salt$jkHSO0tOjmLW0S1NFlw5veSIDRAVsiQQMTrkOKy4xdCCLPNIsHhZkIRlzfzIvKyXeGdOfCBoW1wJZPLyQ9Qx/1" create_home=yes'
5file 模塊
[root@manager ansible_adhoc]# ansible webservers -m file -a 'path=/data state=directory owner=www group=www mode=755 recurse=yes'
path 指定被控端的路徑
state:
directory 創(chuàng)建目錄
touch 創(chuàng)建文件
recurse 遞歸授權(quán)
6.systemd 和 service 模塊(一模一樣)
[root@manager ansible_adhoc]# ansible webservers -m systemd -a 'name=nginx state=started enabled=yes'
state:
started 啟動(dòng)
restarted 重啟
reloaded 重載
stopped 停止
enabled=yes 開機(jī)自啟
7.mount 模塊
[root@manager ansible_adhoc]# ansible client -m mount -a 'src=172.16.1.7:/data path=/opt fstype=nfs opts=defaults state=mounted'
src 設(shè)備古毛、nfs翻翩、磁盤、光盤等
path 被控端要掛載的目錄
fstype:文件系統(tǒng)類型
iso9660 光盤
nfs
xfs
opts 掛載參數(shù)
state:
mounted 掛載設(shè)備稻薇,并加入開機(jī)自啟
present 寫入fstab嫂冻,不掛載
absent 取消掛載,并清除fstab中記錄
unmounted 取消掛載塞椎,不清楚fstab記錄
remounted 重新掛載
8.get_url 模塊
[root@manager ansible_adhoc]# ansible webservers -m get_url -a 'url=https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.0-1.el7.x86_64.rpm dest=/tmp'
url 指定下載地址
dest 指定下載目錄
9.cron 模塊
[root@manager ansible_adhoc]# ansible webservers -m cron -a 'name=backup minute=0 hour=5 job=/server/script/client_data_server.sh &>/dev/null'
name 定時(shí)任務(wù)描述
job 具體的任務(wù)
minute 分
hour 時(shí)
day 日
month 月
weekday 周
disabled=no 關(guān)閉定時(shí)任務(wù)
disabled=yes 重新啟動(dòng)定時(shí)任務(wù)
10.selinux 和 firewalld 模塊
[root@manager ansible_adhoc]# ansible webservers -m selinux -a 'state=disabled'
state=disabled 關(guān)閉防火墻