1.什么是ansible?
可以通過一個命令行完成一系列的操作派诬。
2.ansible 優(yōu)點 特點?
-ansible融合了眾多老牌運維工具的優(yōu)點朱沃,基本上pubbet和saltsatck能實現(xiàn)的功能,ansible都可以顯現(xiàn)塞赂。
-輕量級邑闺,無需在客戶端上安裝agent跌前,更新時,只需要在操作機上進行一次更新即可陡舅。
-ansible是一個輕量級的工具抵乓,ansible不需要啟動服務,僅僅只是一個工具靶衍,可以輕松實現(xiàn)分布式擴展灾炭。
-批量任務執(zhí)行可以寫成腳本,而且不用分發(fā)的遠程就可以執(zhí)行颅眶。
-ansible是一致性蜈出,高可靠性,安全性設計的輕量級自動化工具涛酗。
-使用python編寫铡原,維護更加簡單偷厦。
3.ansible 基礎架構(gòu)? 控制端 被控端 inventory ad-hoc playbook 連接協(xié)議?
image.png
4.ansible 配置文件 優(yōu)先級?
ANSIBLE_CONFIG ansible.cfg #當前項目目錄中 .ansible.cfg #當前執(zhí)行用戶的家目錄
1 2 3
5.ansible inventory主機清單?
/etc/ansible/ansible.cfg
[root@manager ~]# export ANSIBLE_CONFIG="/tmp/ansible.cfg" [root@manager ~]# touch /tmp/ansible.cfg
[root@manager ~]# mkdir /project1
[root@manager ~]# cd /project1/
[root@manager project1]# touch ansible.cfg
[root@manager project2]# ansible --version ansible 2.8.5 config file = /project1/ansible.cfg
[root@manager /]# mkdir /project2
[root@manager /]# cd /project2/
[root@manager project2]# touch ansible.cfg
[root@manager project1]# ansible --version ansible 2.8.5 config file = /project2/ansible.cfg
[root@manager tmp]# touch ~/.ansible.cfg
[root@manager tmp]# ansible --version ansible 2.8.5 config file = /root/.ansible.cfg
5.ansible inventory主機清單?
#1.基于IP地址+密碼的方式
[webservers]
172.16.1.7 ansible_ssh_user='root'
ansible_ssh_pass='1'
172.16.1.8 ansible_ssh_user='root'
ansible_ssh_pass='1'
2.場景二、基于密鑰連接燕刻,需要先創(chuàng)建公鑰和私鑰只泼,并下發(fā)公鑰至被控端
[root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
[root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8
方式一、主機+端口+密鑰
[root@manager ~]# cat hosts [webservers] 172.16.1.7 172.16.1.8
3.場景三酌儒、主機組使用方式
[lbservers] #定義lbservers組
172.16.1.5
172.16.1.6
[webservers] #定義webserver組
172.16.1.7
172.16.1.8
[servers:children] #定義servers組包括兩個子組
[lbservers,webserver]
lbservers
webserver
[root@manager project1]# ansible webservers --listhosts -i hosts
hosts (2):
172.16.1.7
172.16.1.8
6.Ansible Ad-Hoc 單條命令
image.png
command #執(zhí)行命令 默認 不支持管道
shell #執(zhí)行命令 支持管道
yum_reposity #yum倉庫配置
yum #yum安裝軟件
get_url #和linux的wget一致
copy #拷貝配置文件
service|systemd #啟動服務
user
group file #創(chuàng)建目錄 創(chuàng)建文件 遞歸授權(quán)
mount #掛載
cron #定時任務
firewalld #防火墻
selinux #selinuix
1.command
ansible webservers -a "ps axu|grep nginx" -i hosts #不支持管道(簡單命令)
2.shell
ansible webservers -m shell -a "ps axu|grep nginx" i hosts #支持管道
3.yum
state:
present 安裝
absent 卸載
latest 最新
enablerepo #指定使用按個倉庫
disablerepo #排除使用哪個倉庫
#1.安裝最新的httpd服務
[root@manager project1]# ansible webservers -m yum -a "name=httpd state=latest disablerepo=webtaticphp" -i hosts
#2.移除httpd服務
[root@manager project1]# ansible webservers -m yum -a "name=httpd state=absent disablerepo=webtaticphp" -i hosts
#3.安裝httpd指定從按個倉庫安裝
- name: install the latest version of Apache from the testing repo
[root@manager project1]# ansible webservers -m yum -a "name=httpd state=latest enablerepo=testing" -i hosts
#4.通過URL方式進行安裝
[root@manager project1]# ansible webservers -m yum -a "name=https://mirrors.aliyun.com/zabbix/zabbix/3.0/ rhel/7/x86_64/zabbix-agent-3.0.0-1.el7.x86_64.rpm state=present disablerepo=webtatic-php" -i hosts
- name: install nginx rpm from a local file (軟件包 必須在被控端主機)
[root@manager project1]# ansible webservers -m yum -a "name=/root/zabbix-agent-4.0.0-2.el7.x86_64.rpm state=present disablerepo=webtatic-php" -i hosts
4.copy
src #本地路徑,可以是相對,可以是絕對
dest #目標位置
owner #屬主
group #屬組
mode #權(quán)限
backup #備份
[root@manager project1]# ansible webservers -m copy -a "src=./file/ansible.oldxu.com.conf dest=/etc/nginx/conf.d/ansible.oldxu.com.conf owner=root group=root mode=644" -i hosts
[root@manager project1]# ansible webservers -m copy -a "src=./file/ansible.oldxu.com.conf dest=/etc/nginx/conf.d/ansible.oldxu.com.conf owner=root group=root mode=644 backup=yes" -i hosts
5.service|systemd
state
started #啟動
stopped #停止
restarted #重啟
reloaded #重載
enabled #是否開機自啟
yes #是
no #否
[root@manager project1]# ansible webservers -m systemd -a "name=nginx state=restarted enabled=yes" -i hosts
#創(chuàng)建 /code/ansible
path #路徑
state
touch #創(chuàng)建文件
directory #創(chuàng)建目錄
owner #屬主
group #屬組
mode #權(quán)限
#準備站點
[root@manager project1]# ansible webservers -m file -a "path=/code/ansible state=directory mode=755 owner=www group=www" -i hosts
#準備站點代碼
[root@manager project1]# ansible webservers -m copy -a "src=./file/index.html dest=/code/ansible/index.html owner=www group=www mode=644" -i hosts
7.user group
#group
整數(shù)int 小數(shù) flot dasdsa str 真|假 bool
[root@manager project1]# ansible webservers -m group -a "name=www gid=666 state=present" -i hosts
#user
name #名稱
uid #uid
group #組名或gid
create_home #是否創(chuàng)建家目錄
system #是否作為系統(tǒng)組
shell #指定登錄shell
state
present
absent
remove
groups
append
password
# 程序使用 www 666 666 /sbin/nologin /home -->無
[root@manager project1]# ansible webservers -m user -a "name=www uid=666 group=666 create_home=no shell=/sbin/nologin state=present" -i hosts
# 正常用戶 oldxu 1000 1000 /bin/bash /home/oldxu
[root@manager project1]# ansible webservers -m user -a "name=oldxu" -i hosts
# 移除oldxu用戶,并刪除家目錄所有內(nèi)容.
[root@manager project1]# ansible webservers -m user -a "name=oldxu state=absent remove=yes" -i hosts
# 創(chuàng)建 other用戶.有兩個附加組root bin,創(chuàng)建家目錄,指定登錄 shell,設定密碼123
#生成一個密碼
ansible all -i localhost, -m debug -a "msg={{ '123' | password_hash('sha512', 'mysecretsalt') }}"
[root@manager project1]# ansible webservers -m user -a 'name=other groups='root,bin' create_home=yes shell=/bin/bash
password="$6$mysecretsalt$gIIYs0Xgc7sSQkH.zKaz8/Afa MomYzR1QZYtccwmJcUt8VpLq4D055UCCX4MlwgePOP80ZRwhppv BF72RIAVi/"' -i hosts
8.mount
#提前準備好nfs服務端
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/zrlog 172.16.1.0/24
/data/zh 172.16.1.0/24
/data/edu 172.16.1.0/24
/data/blog 172.16.1.0/24
用管理端操作被控端,讓被控端掛載nfs存儲數(shù)據(jù)
present #寫入/etc/fstab
absent #卸載/etc/fstab
mounted #臨時掛載
unmounted #卸載當前掛載
#掛載過程中,如果目錄不存在,則會創(chuàng)建該目錄
[root@manager project1]# ansible webservers -m mount -a "src=172.16.1.31:/data/zrlog path=/test_zrlog fstype=nfs opts=defaults state=mounted" -i hosts
[root@manager project1]# ansible webservers -m mount -a "src=172.16.1.31:/data/zrlog path=/test_zrlog fstype=nfs opts=defaults state=unmounted" -i hosts
9.cron
minute #分
hour #時
day #日
month #月
week #周
job #
[root@manager project1]# ansible webservers -m cron -a 'name=test_job minute=00 hour=02 job="/bin/bash /server/scripts/client_to_data_server.sh &>/dev/null"' -i hosts
[root@manager project1]# ansible webservers -m cron -a 'name=test job="/bin/bash /server/scripts/test.sh &>/dev/null"' -i hosts
[root@manager project1]# ansible webservers -m cron -a 'name=test job="/bin/bash /server/scripts/test.sh &>/dev/null" state=absent' -i hosts
10.firewalld
[root@manager project1]# ansible webservers -m systemd -a "name=firewalld state=started" -i hosts
#針對服務
[root@manager project1]# ansible webservers -m firewalld -a "service=http state=enabled" -i hosts
#針對端口
[root@manager project1]# ansible webservers -m firewalld -a "port=9999/tcp state=enabled" -i hosts
11.selinux
[root@manager project1]# ansible webservers -m selinux -a "state=disabled" -i hosts