ansible 和saltstack對比
錯誤回答:ansible比較輕量級,不需要安裝客戶端
SaltStack 底層有一個消息隊列:Zero-MQ(message queue)
1.ansible是基於ssh協議來工作的,但是saltstack也有支持ssh協議的模式
2.但是一般來說,選擇saltstack就是為了使用客戶端和服務端的方式(速度快)
3.因為saltstack底層有一個zmq做消息隊列
4.ansible基於ssh,如果ssh服務掛了,ansible也不能工作,saltstack可以,會起兩個端口4505,4506
ansible的模塊
command
shell
script
軟件管理模塊
- yum
name: 服務名
state:
present:安裝
absent:卸載
latest:安裝最新版本
- yum_repository
name:指定倉庫名字,如果沒有配置file,則文件名和倉庫名一致
file:指定文件名
baseurl:指定源
gpgcheck:指定是否檢查秘鑰
yes:檢查
no:不檢查
description:指定對倉庫的描述,也就是repo文件中的name
enabled:是否開啟倉庫
yes:開啟
no:不開啟
state:
present:添加倉庫
absent:刪除倉庫
ansible文件管理模塊
copy
[root@m01 ~]# ansible backup -m copy -a 'src=/root/zls_xxx.conf dest=/etc/rsyncd.conf owner=root group=root mode=0644'
src:指定源文件位置(管理機上的文件)
dest:指定你要推送到主機的目標位置
owner:指定屬主
group:指定屬組
mode:指定權限
backup:是否備份,第一次推,沒有每份,對端機器存在該文件,並且內容不一致,才會做備份
yes:推送之前,先備份目標主機的源文件
no:不備份
remote_src:源文件是否在遠端的機器上
yes:是
no:否
content:往指定目標文件中寫入內容
file
作用:
-
授權
-
創建目錄
-
創建文件
-
創建軟連接
-
刪除目錄,文件,軟連接
[root@m01 ~]# ansible all -m file -a 'path=/opt/test/zls owner=www group=www mode=0722 state=directory'
[root@m01 ~]# ansible all -m file -a 'path=/code owner=www group=www recurse=yes'
path:指定文件或目錄的路徑
owner:指定屬主
group:指定數組
mode:指定權限
src:做 軟/硬 鏈接的時候使用,指定源文件
recurse:是否遞歸授權
yes:遞歸授權
no:僅授權當前目錄
state:
directory:創建目錄
touch:創建文件
link:做軟鏈接
hard:做硬鏈接
absent:刪除
file:配合 modification_time access_time 修改文件的屬性,stat
get_url
類似於:wget
[root@m01 ~]# ansible backup -m get_url -a 'url=http://test.driverzeng.com/Nginx_File/nginx.txt dest=/root checksum=md5:8f8dd0f79bc6ef2148ca3494070a86a1'
url:指定下載文件的地址
dest:指定下載的路徑
checksum:指定加密的算法
sha256
md5
ansible服務管理模塊
service、systemd
[root@m01 ~]# ansible 'c6,backup' -m service -a 'name=crond state=stopped'
name:指定服務名稱
state:
started:啟動服務
stopped:停止服務
restarted:重啟服務
reloaded:重新加載服務
ansible用戶管理模塊
user
[root@m01 ~]# ansible all -m user -a 'name=zlsqqq uid=10201 group=root shell=/sbin/nologin create_home=false'
name:指定用戶名
uid:指定uid -u
group:只能指定組名,不能指定gid -g
shell:指定登錄的方式 -s
create_home:是否創建家目錄
true,yes:創建
false,no:不創建
comment:指定注釋 -c
groups:指定附加組(配合append,如果不加append覆蓋) -G
append:創建附加組的時候,追加 -a
remove:刪除用戶的時候,是否同時刪除家目錄和郵件文件
true,yes:刪除
fasle,no:不刪除
state
present:創建
absent:刪除
generate_ssh_key:是否創建秘鑰對
yes:創建
no:不創建
ssh_key_bits:指定秘鑰對加密長度
ssh_key_file:指定私鑰文件的位置
system:是否是系統用戶 -r
yes:是系統用戶
no:不是系統用戶
group
[root@m01 ~]# ansible all -m group -a 'name=xxxx gid=10010 state=present'
name:指定組名
gid:指定組id
state:
present:創建
absent:刪除
ansible定時任務模塊
cron
#### 創建
[root@m01 ~]# ansible all -m cron -a "name='sync time' minute=*/5 job='ntpdate time1.aliyun.com &>/dev/null'"
#### 刪除(刪除是根據注釋來刪除的 name)
[root@m01 ~]# ansible all -m cron -a "name='time' state=absent"
name:指定定時任務的名字(添加一個備注)
state:
present:創建定時任務
absent:刪除定時任務
minute:分 (0-59) */5 10-20 10,20
hour:時(0-23)
day:日(1-31)
month:月(1-12)
weekday:周(0-6)
ansible磁盤掛載模塊
mount
[root@m01 ~]# ansible web_group -m mount -a 'path=/mnt src=10.0.0.31:/web_data fstype=nfs state=mounted'
path:掛載到本地的目錄
src:對端目錄
fstype:文件系統類型
nfs
ext4
ext3
state:
present:只寫入開機自動掛載的文件中,不掛載
mounted:既寫入文件,又掛載
absent:卸載設備,並且清理開機自動掛載文件
unmounted:只卸載不清理文件
推薦:
- 掛載的時候:mounted
- 卸載的時候:absent
ansible關閉selinux模塊
[root@m01 ~]# ansible all -m selinux -a 'state=disabled'
state:
enforcing
permisive
disabled
ansible防火牆模塊
[root@m01 ~]# ansible all -m firewalld -a 'port=443/tcp permanent=no state=enabled'
[root@m01 ~]# ansible all -m firewalld -a 'service=http permanent=no state=enabled'
web01 web02 backup
安裝rsync
打開防火牆
打開873端口
關閉selinux
安裝nfs
並且web01 02 掛載到nfs
統一使用www用戶uid gid 666
環境准備
服務器 | 內網IP | 外網IP | 安裝服務 | 角色 |
---|---|---|---|---|
web01 | 172.16.1.7 | 10.0.0.7 | 被控端 | |
web02 | 172.16.1.8 | 10.0.0.8 | 被控端 | |
nfs | 172.16.1.31 | 10.0.0.31 | 被控端 | |
backup | 172.16.1.41 | 10.0.0.41 | 被控端 | |
m01 | 172.16.1.61 | 10.0.0.61 | ansible | 控制端 |
m01部署
# 下載ansible
[root@m01 ~]# yum install -y ansible
# 修改ansible配置文件
[root@m01 ~]# vim /etc/ansible/ansible.cfg
# 開啟ansible的日志
log_path = /var/log/ansible.log
# 默認模塊由command改成shell
module_name = shell
# 檢查對應服務器的主機密鑰,打開注釋
host_key_checking = False
# 添加主機清單
[root@m01 ~]# !v
vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
web03 ansible_ssh_host=10.0.0.9
[backup_group]
backup ansible_ssh_host=10.0.0.41
[nfs_group]
nfs ansible_ssh_host=10.0.0.31
[rsync:children]
backup_group
nfs_group
~
# 生成密鑰
[root@m01 ~]# ssh-keygen
# 把公鑰發送給被控端服務器
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.31
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.41
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.9
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.8
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.7
# 創建www用戶組制定gid666
[root@m01 ~]# ansible all -m group -a 'name=www gid=666 state=present'
# 創建www用戶指定uid666 gid666 ,不讓登陸,不創建家目錄
[root@m01 ~]# ansible all -m user -a "name=www uid=666 group=666 shell=/sbin/nologin create_home=no"
# 編輯backuprsync的配置文件
[root@m01 ~]# vim /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = nfs_bak
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[nfs]
comment = welcome to oldboyedu backup!
path = /backup
# 把配置文件傳送到backup服務器
[root@m01 ~]# ansible backup -m copy -a 'src=/etc/rsyncd.conf dest=/etc/rsyncd.conf owner=root group=root mode=0644'
# 編輯密碼文件
[root@m01 ~]# vim /etc/rsync.passwd
nfs_bak:123
# 把密碼文件傳送到backup
[root@m01 ~]# ansible backup -m copy -a 'src=/etc/rsync.passwd dest=/etc/rsync.pwaawd owner=root group=root mode=0600'
# 創建備份目錄
[root@m01 ~]# ansible backup -m file -a 'path=/backup owner=www group=www mode=0755 state=directory'
# 配置yum倉庫
[root@m01 ~]# ansible all -m yum_repository -a 'name=rsync file=rsync description=rsync baseurl=http://http://mirrors.aliyun.com/epel/7/$basearch gpgcheck=no enabled=yes'
# 安裝rsync
[root@m01 ~]# ansible backup -m yum -a 'name=rsync state=present'
#安裝nfs
[root@m01 ~]# ansible nfs -m yum -a 'name=nfs-utils state=present'
# 啟動rsync
[root@m01 ~]# ansible backup -m service -a 'name=rsyncd state=started'
# 打開防火牆
[root@m01 ~]# ansible all -m service -a 'name=firewalld state=started'
# 打開873端口
[root@m01 ~]# ansible all -m firewalld -a "port=873/tcp permanent=no state=enabled"
# 關閉selinux
[root@m01 ~]# ansible all -m selinux -a 'state=disabled'
# 編輯nfs掛載文件
[root@m01 ~]# ansible nfs -m copy -a 'content="/test_data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)\n" dest=/etc/exports'
# 創建nfs掛載目錄
[root@m01 ~]# ansible nfs -m file -a 'path=/test_data owner=www group=www mode=0755 state=directory'
# 啟動nfs服務
[root@m01 ~]# ansible nfs -m service -a 'name=nfs-server state=started'
# 關閉防火牆
[root@m01 ~]# ansible all -m service -a 'name=firewalld state=stopped'
# 掛載目錄
[root@m01 ~]# ansible web_group -m mount -a 'path=/mnt src=172.16.1.31:/test_data fstype=nfs state=mounted'
# 查看掛載
[root@m01 ~]# ansible web_group -m shell -a 'df -h'
web02 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use%
172.16.1.31:/test_data 19G 1.3G 18G 8% /mnt
web03 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
172.16.1.31:/test_data 19G 1.3G 18G 8% /mnt
1.web01 web02 安裝nginx
2.自己寫一個前端頁面(xxx_web01_page)
3.安裝nfs
4.web01和web02隨便掛載目錄到nfs
5.nfs將共享目錄的數據,推送到backup
6.rsync
環境准備
服務器名稱 | 外網IP | 內網IP | 角色 |
---|---|---|---|
web01 | 10.0.0.7 | 172.16.1.7 | 被控端 |
web02 | 10.0.0.8 | 172.16.1.8 | 被控端 |
nfs | 10.0.0.31 | 172.16.1.31 | 被控端 |
backup | 10.0.0.41 | 172.16.1.41 | 被控端 |
m01 | 10.0.0.61 | 172.16.1.61 | 控制端 |
作業規划
服務器准備
配置m01主機清單
編輯playbook
m01部署
# 下載ansible
[root@m01 ~]# yum install -y ansible
# ansible配置文件優化
[root@m01 ~]# vim /etc/ansible/ansible.cfg
host_key_checking = False
log_path = /var/log/ansible.log
module_name = shell
# 生成密鑰對
[root@m01 ~]# ssh-keygen
# 把公鑰發送給要管理的機器
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.41
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.31
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.9
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.8
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.7
# 配置主機清單
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
# web03 ansible_ssh_host=10.0.0.9
[backup_group]
backup ansible_ssh_host=10.0.0.41
[nfs_group]
nfs ansible_ssh_host=10.0.0.31
[rsync:children]
backup_group
nfs_group
# 編寫ansible-playbook劇本
ansible-playbook
[root@m01 ~]# cat nginx_rsync.yml
- hosts: web_group
tasks:
- name: install nginx server
yum:
name: nginx
state: present
- name: nginx_page
copy:
src: /etc/ansible/nginx.conf
dest: /etc/nginx/conf.d/
owner: root
group: root
mode: 0755
- name: create zhandian yemian
file:
path: /code/
owner: root
group: root
mode: 0755
state: directory
- name: bianjipage
copy:
src: /root/a.html
dest: /code/
owner: root
group: root
mode: 0644
- name: start nginx
service:
name: nginx
state: restarted
enabled: yes
- hosts: nfs
tasks:
- name: install nfs-server
yum:
name: nfs-utils
state: present
- name: bianjipeizhi
copy:
content: /data 172.16.1.0/24(rw,sync,all_squash,anonuid=0,anongid=0)
dest: /etc/exports
owner: root
group: root
mode: 0644
- name: create data
file:
path: /data/
owner: root
group: root
mode: 0755
state: directory
- name: start server
service:
name: nfs-server
state: restarted
enabled: yes
- hosts: web_group
tasks:
- name: mount nfs
mount:
path: /mnt
src: 172.16.1.31:/data
fstype: nfs
state: mounted
- hosts: backup
tasks:
- name: install rsync
yum:
name: rsync
state: present
- name: rsync_page
copy:
src: /root/rsync
dest: /etc/rsyncd.conf
- name: pass_page
copy:
content: nfs_bak:123
dest: /etc/rsync.passwd
- name: create dir
file:
path: /backup
owner: root
group: root
mode: 0755
state: directory
- name: start rsync
service:
name: rsyncd
state: restarted
enabled: yes
- hosts: nfs
tasks:
- name: install rsync
yum:
name: rsync
state: present
- name: start rsync
service:
name: rsyncd
state: restarted
enabled: yes
- name: rsync beifei
copy:
src: /root/bf.sh
dest: /root/bf.sh
- name: cron backup
cron:
minute: "*/1"
job: "/bin/sh /root/bf.sh &>/dev/null"
備份腳本
[root@nfs ~]# cat bf.sh
export RSYNC_PASSWORD=123
#設置普通變量
D=$(date +%F)
IP=$(ifconfig eth1|awk 'NR==2 {print $2}')
H=$(hostname)
bk_dir=/backup
local_dir=/data
#判斷目錄存在如果不存在則創建
if [ ! -d $local_dir ] ; then
mkdir -p $local_dir
fi
#客戶端本地打包備份至上面創建的目錄下
tar zcPf ${local_dir}/${D}_data.tar.gz /data
#將備份的數據推送到本地服務器目錄
rsync -avz $local_dir nfs_bak@10.0.0.41::nfs
#只保留七天內的備份數據其他刪除
cd / && \
find /backup/ -type d ! -mtime -7 ! -name 'backup' |xargs rm -rf
nginx配置文件
root@web03 code]# cd /etc/nginx/conf.d/
[root@web03 conf.d]# ll
total 4
-rwxr-xr-x 1 root root 80 Jun 10 22:32 nginx.conf
[root@web03 conf.d]# cat nginx.conf
server {
listen 80;
server_name test.nginx.com;
root /code;
index a.html;
}
自定義站點頁面
[root@web03 ~]# cd /code
[root@web03 code]# cat a.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>my website</title>
</head>
<body>
<article>
<header>
<h1>被釣魚網站</h1>
<p>創建時間:<time pubdate="pubdate">2020/6/4</time></p>
</header>
<p>
<b>標題:</b>啥也不是
</p>
<footer>
<p><small>太難了</small></p>
</footer>
</article>
</body>
</html>
每分鍾備份一次data目錄
ansible
ansible是新出現的自動化運維工具,基於Python開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。
Options:
-a MODULE_ARGS, --args=MODULE_ARGS
#module arguments
#指定執行模塊使用的參數
--ask-vault-pass
#ask for vault password
#加密playbook文件時提示輸入密碼
-B SECONDS, --background=SECONDS
#run asynchronously, failing after X seconds(default=N/A)
#后台運行超時時間,異步運行,X秒之后失敗
-C, --check
#don't make any changes; instead, try to predict some of the changes that may occur
#模擬執行,不會真正在機器上執行(查看執行會產生什么變化)
-D, --diff
#when changing (small) files and templates, show the differences in those files; works great with --check
#當更新的文件數及內容較少時,該選項可顯示這些文件不同的地方,該選項結合-C用會有較好的效果
-e EXTRA_VARS, --extra-vars=EXTRA_VARS
#set additional variables as key=value or YAML/JSON
#執行命令時添加額外參數變量
-f FORKS, --forks=FORKS
#specify number of parallel processes to use(default=5)
#並行任務數。FORKS被指定為一個整數,默認是5
-h, --help
#show this help message and exit
#打開幫助文檔API
-i INVENTORY, --inventory-file=INVENTORY
#specify inventory host path(default=/etc/ansible/hosts) or comma separated host list.
#指定要讀取的Inventory文件
-l SUBSET, --limit=SUBSET
#further limit selected hosts to an additional pattern
#限定執行的主機范圍
--list-hosts
#outputs a list of matching hosts; does not execute anything else
#列出執行匹配到的主機,但並不會執行
-m MODULE_NAME, --module-name=MODULE_NAME
#module name to execute (default=command)
#指定執行使用的模塊,默認使用 command 模塊
-M MODULE_PATH, --module-path=MODULE_PATH
#specify path(s) to module library (default=None)
#要執行的模塊的路徑
--new-vault-password-file=NEW_VAULT_PASSWORD_FILE
#new vault password file for rekey
-o, --one-line
#condense output
#壓縮輸出,摘要輸出.嘗試一切都在一行上輸出
--output=OUTPUT_FILE
#output file name for encrypt or decrypt; use - for stdout
#
-P POLL_INTERVAL, --poll=POLL_INTERVAL
#set the poll interval if using -B (default=15)
#設置輪詢間隔,每隔數秒。需要- B
--syntax-check
#perform a syntax check on the playbook, but do not execute it
#檢查Playbook中的語法書寫
-t TREE, --tree=TREE
#log output to this directory
#將日志內容保存在該輸出目錄,結果保存在一個文件中在每台主機上
--vault-password-file=VAULT_PASSWORD_FILE
#vault password file
#
-v, --verbose
#verbose mode (-vvv for more, -vvvv to enable connection debugging)
#執行詳細輸出
--version
#show program's version number and exit
#顯示版本
Connection Options:
control as whom and how to connect to hosts
-k, --ask-pass
ask for connection password
--private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
use this file to authenticate the connection
-u REMOTE_USER, --user=REMOTE_USER
connect as this user (default=None)
指定遠程主機以USERNAME運行命令
-c CONNECTION, --connection=CONNECTION
connection type to use (default=smart)
指定連接方式,可用選項paramiko (SSH)、ssh、local,local方式常用於crontab和kickstarts
-T TIMEOUT, --timeout=TIMEOUT
override the connection timeout in seconds(default=10)
SSH連接超時時間設定,默認10s
--ssh-common-args=SSH_COMMON_ARGS
specify common arguments to pass to sftp/scp/ssh (e.g.ProxyCommand)
--sftp-extra-args=SFTP_EXTRA_ARGS
specify extra arguments to pass to sftp only (e.g. -f, -l)
--scp-extra-args=SCP_EXTRA_ARGS
specify extra arguments to pass to scp only (e.g. -l)
--ssh-extra-args=SSH_EXTRA_ARGS
specify extra arguments to pass to ssh only (e.g. -R)
Privilege Escalation Options:
control how and which user you become as on target hosts
-s, --sudo
run operations with sudo (nopasswd) (deprecated, use become)
相當於Linux系統下的sudo命令
-U SUDO_USER, --sudo-user=SUDO_USER
desired sudo user (default=root) (deprecated, use become)
使用sudo,相當於Linux下的sudo命令
-S, --su
run operations with su (deprecated, use become)
-R SU_USER, --su-user=SU_USER
run operations with su as this user (default=root) (deprecated, use become)
-b, --become
run operations with become (does not imply password prompting)
--become-method=BECOME_METHOD
privilege escalation method to use (default=sudo),valid choices: [ sudo | su | pbrun | pfexec | doas |dzdo | ksu | runas ]
--become-user=BECOME_USER
run operations as this user (default=root)
--ask-sudo-pass
ask for sudo password (deprecated, use become)
--ask-su-pass
ask for su password (deprecated, use become)
-K, --ask-become-pass
ask for privilege escalation password
Ansible是一個配置管理系統configuration management system,你只需要可以使用ssh訪問你的服務器或設備就行。
1.安裝軟件
2.配置服務
Ansible能做什么
ansible可以幫助我們完成一些批量任務,或者完成一些需要經常重復的工作。
比如:同時在100台服務器上安裝nfs服務,並在安裝后啟動服務。
比如:將某個文件一次性拷貝到100台服務器上。
比如:每當有新服務器加入工作環境時,你都要為新服務器部署某個服務,也就是說你需要經常重復的完成相同的工作。
這些場景中我們都可以使用到ansible。
Cobbler
Ansible
1.購買機器->2.配置環境->3.部署代碼->4測試->5.加入集群
Ansible/Saltstack(master->minion)
Ansible軟件特點
1.ansible不需要單獨安裝客戶端,SSH相當於ansible客戶端。
2.ansible不需要啟動任何服務,僅需安裝對應工具即可。
3.ansible依賴大量的python模塊來實現批量管理。
4.ansible配置文件/etc/ansible/ansible.cfg
實現從管理機m01到其他機器的密鑰認證
ansible借助公鑰批量管理
利用非交換式工具實現批量分發公鑰與批量管理服務器
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
1.安裝ansible
[root@m01 ~]# yum install ansible -y
2.配置ansible
[root@m01 ~]# vim /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.41
3.驗證ansible
ansible是通過ssh端口探測通信
[root@m01 ~]# ansible oldboy -m ping
172.16.1.7 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.16.1.31 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.16.1.41 | SUCCESS => {
"changed": false,
"ping": "pong"
}
批量執行命令
[root@m01 ~]# ansible oldboy -m command -a "df -h"
如果沒有給對應的主機下發公鑰,可以使用密碼的方式進行添加
172.16.1.41 ansible_ssh_user='root' ansible_ssh_pass='1' ansible_ssh_port='22'
定義主機清單
[web]
172.16.1.7
[nfs]
172.16.1.31
[backup]
172.16.1.41
[oldboy:children]
web
nfs
backup
[root@m01 ~]# ansible web --list-hosts #web
hosts (1):
172.16.1.7
[root@m01 ~]# ansible nfs --list-hosts #nfs
hosts (1):
172.16.1.31
[root@m01 ~]# ansible backup --list-hosts #rsync
hosts (1):
172.16.1.41
[root@m01 ~]# ansible oldboy --list-hosts #集中所有的小組,用於執行一些基礎的配置
hosts (3):
172.16.1.31
172.16.1.41
172.16.1.7
1、命令 -> 文件 = 腳本
2、模塊 -> 文件 = 劇本
安裝 配置 啟動
1.command 執行命令
2.shell 執行命令
3.yum 安裝軟件模塊
4.copy 配置模塊
5.service 啟動服務模塊
6.user 用戶管理
7.file 創建目錄,創建文件,往文件寫內容
8.cron 定時任務
9.mount 掛載
.command命令模塊:默認模塊, 執行命令
root@m01 ~]# ansible oldboy -a "hostname"
如果需要一些管道操作,則使用shell
[root@m01 ~]# ansible oldboy -m shell -a "ifconfig|grep eth0" -f 50
-f =forks /etc/ansible/ansible.cfg #結果返回的數量
yum安裝模塊
推送腳本文件至遠程,遠程執行腳本文件
[root@m01 ~]# ansible oldboy -m yum -a "name=httpd state=installed"
name ---指定要安裝的軟件包名稱
state ---指定使用yum的方法
installed,present ---安裝軟件包
removed,absent ---移除軟件包
latest ---安裝最新軟件包
copy模塊推送文件模塊
[root@m01 ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/test.txt owner=www group=www mode=0600"
在推送覆蓋遠程端文件前,對遠端已有文件進行備份,按照時間信息備份
[root@m01 ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/test.txt backup=yes"
直接向遠端文件內寫入數據信息,並且會覆蓋遠端文件內原有數據信息
[root@m01 ~]# ansible oldboy -m copy -a "content='bgx' dest=/tmp/oldboy"
src --- 推送數據的源文件信息
dest --- 推送數據的目標路徑
backup --- 對推送傳輸過去的文件,進行備份
content --- 直接批量在被管理端文件中添加內容
group --- 將本地文件推送到遠端,指定文件屬組信息
owner --- 將本地文件推送到遠端,指定文件屬主信息
mode --- 將本地文件推送到遠端,指定文件權限信息
4.service服務模塊
[root@m01 ~]# ansible oldboy -m service -a "name=crond state=stopped enabled=yes"
name --- 定義要啟動服務的名稱
state --- 指定服務狀態是停止或是運行
started --- 啟動
stopped --- 停止
restarted --- 重啟
reloaded --- 重載
enabled --- 是否讓服務開啟自啟動
1.安裝
[root@m01 ~]# ansible web -m yum -a "name=httpd state=installed"
2.配置
[root@m01 ~]# ansible web -m copy -a "content='This is Ansible' dest=/var/www/html/index.html"
3.啟動
[root@m01 ~]# ansible web -m service -a "name=httpd state=started"
yum copy service mount cron user file
1.機器還原快照(firewalld、selinux、配置好倉庫)
2.推送你的公鑰
3.指定backup安裝rsync、配置、啟動、創建目錄、創建用戶、准備密碼文件、權限
4.指定nfs安裝nfs、配置、啟動
5.web掛載nfs
6.web執行腳本推送數據至bakcup,加入定時任務
3.script模塊
編寫腳本
[root@m01 ~]# mkdir -p /server/scripts
[root@m01 ~]# cat /server/scripts/yum.sh
!/usr/bin/bash
yum install -y iftop
在本地運行模塊,等同於在遠程執行,不需要將腳本文件進行推送目標主機執行
[root@m01 ~]# ansible oldboy -m script -a "/server/scripts/yum.sh"
5.file配置模塊
創建目錄
[root@m01 ~]# ansible oldboy -m file -a "path=/tmp/oldboy state=diretory"
創建文件
[root@m01 ~]# ansible oldboy -m file -a "path=/tmp/tt state=touch mode=555 owner=root group=root"
[root@m01 ~]# ansible oldboy -m file -a "src=/tmp/tt path=/tmp/tt_link state=link"
path --- 指定遠程主機目錄或文件信息
recurse --- 遞歸授權
state ---
directory --- 在遠端創建目錄
touch --- 在遠端創建文件
link --- link或hard表示創建鏈接文件
absent --- 表示刪除文件或目錄
mode --- 設置文件或目錄權限
owner --- 設置文件或目錄屬主信息
group --- 設置文件或目錄屬組信息
7.group模塊
name --- 指定創建的組名
gid --- 指定組的gid
state
absent --- 移除遠端主機的組
present --- 創建遠端主機的組(默認)
創建組,指定gid
[root@m01 ~]# ansible oldboy -m group -a "name=oldgirl gid=888"
8.user模塊
uid --- 指定用戶的uid
group --- 指定用戶組名稱
groups --- 指定附加組名稱
password --- 給用戶添加密碼
shell --- 指定用戶登錄shell
create_home --- 是否創建家目錄
創建oldgirl,設定uid為888,並加入gid為888
[root@m01 ~]# ansible oldboy -m user -a "name=oldgirl uid=888 group=888 shell=/sbin/nologin create_home=no"
隨機生成加密字符串(-1使用MD5進行加密 -stdin 非交互式 -salt 加密參數)
[root@m01 ~]# echo "bgx" | openssl passwd -1 -stdin
固定加密字符串
[root@m01 ~]# echo "123"| openssl passwd -1 -stdin -salt 'salt
創建普通用戶,並配置對應的用戶密碼
[root@m01 ~]# echo "bgx" | openssl passwd -1 -stdin
$1\(1KmeCnsK\)HGnBE86F/XkXufL.n6sEb.
[root@m01 ~]# ansible oldboy -m user -a 'name=xlw password="$1\(765yDGau\)diDKPRoCIPMU6KEVEaPTZ0"'
crond模塊
使用ansible添加一條定時任務
[root@m01 ~]# ansible oldboy -m cron -a "minute=* hour=* day=* month=* weekday=* job='/bin/sh /server/scripts/test.sh'"
[root@m01 ~]# ansible oldboy -m cron -a "job='/bin/sh /server/scripts/test.sh'"
設置定時任務注釋信息,防止重復,name設定
[root@m01 ~]# ansible oldboy -m cron -a "name='cron01' job='/bin/sh /server/scripts/test.sh'"
刪除相應定時任務
[root@m01 ~]# ansible oldboy -m cron -a "name='ansible cron02' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' state=absent"
注釋相應定時任務,使定時任務失效
[root@m01 scripts]# ansible oldboy -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' disabled=yes"
mount模塊
present ---開機掛載,僅將掛載配置寫入/etc/fstab
mounted ---掛載設備,並將配置寫入/etc/fstab
unmounted ---卸載設備,不會清除/etc/fstab寫入的配置
absent ---卸載設備,會清理/etc/fstab寫入的配置
僅將掛載的配置寫入/etc/fstab,並不會執行掛載操作
[root@m01 ~]# ansible oldboy -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=present"
臨時掛載設備,並將掛載信息寫入/etc/fstab
[root@m01 ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted"
臨時卸載,不會清理/etc/fstab
[root@m01 ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=unmounted"
卸載,不僅臨時卸載,同時會清理/etc/fstab
[root@m01 ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=absent"
yum copy service mount cron user file
1.機器還原快照(firewalld、selinux、配置好倉庫)
選擇虛擬機-》快照-》恢復
2.推送你的公鑰
[root@m01 ~]# sshpass -p1 ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41
3.配置Ansible的主機清單
[root@m01 ~]# cat /etc/ansible/hosts
[web]
172.16.1.7
172.16.1.8
[nfs]
172.16.1.31
[backup]
172.16.1.41
檢查主機是否都ok
[root@m01 ~]# ansible all -m ping
epel、firewalld、selinux、ww
1.基礎環境:
1.所有的主機都需要安裝rsync和nfs-utils
2.所有的主機都需要准備對應的rsync客戶端的密碼文件/etc/rsync.pass
3.所有的主機都需要創建一個uid和gid為666的www用戶
4.所有的主機都需要全網備份的腳本,並配置好定時任務
1.安裝rsync和nfs-utils
[root@m01 ~]# ansible all -m yum -a "name=rsync,nfs-utils state=installed"
2.准備rsync的客戶端密碼文件
[root@m01 ~]# ansible all -m copy -a "content='1' dest=/etc/rsync.pass owner=root group=root mode=600"
3.准備對應的www用戶,uid和gid都為666
[root@m01 ~]# ansible all -m group -a "name=www gid=666"
[root@m01 ~]# ansible all -m user -a "name=www uid=666 group=666 create_home=no shell=/sbin/nologin"
4.從管理上拷貝對應的腳本文件,然后將其加入定時任務
[root@m01 ~]# ansible all -m copy -a "src=./scripts/rsync_backup_md5.sh dest=/server/scripts/ mode=755"
[root@m01 ~]# ansible all -m cron -a "name='Rsync Bakcup Scripts' hour=01 minute=00 job='/bin/bash /server/scripts/rsync_backup_md5.sh &>/dev/null'"
[root@m01 ~]# pwd
/root
[root@m01 ~]# mkdir scripts
[root@m01 ~]# cat scripts/rsync_backup_md5.sh
!/usr/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
1.定義變量
Host=\((hostname) Addr=\)(ifconfig eth1|awk 'NR==2{print \(2}') Date=\)(date +%F)
Dest=\({Host}_\){Addr}_${Date}
Path=/backup
2.創建備份目錄
[ -d \(Path/\)Dest ] || mkdir -p \(Path/\)Dest
3.備份對應的文件
cd / &&
[ -f \(Path/\)Dest/system.tar.gz ] || tar czf \(Path/\)Dest/system.tar.gz etc/fstab etc/rsyncd.conf &&
[ -f \(Path/\)Dest/log.tar.gz ] || tar czf \(Path/\)Dest/log.tar.gz var/log/messages var/log/secure && \
4.攜帶md5驗證信息
[ -f \(Path/\)Dest/${Date}.flag ] || md5sum \(Path/\)Dest/*.tar.gz >\(Path/\)Dest/${Date}.flag
4.推送本地數據至備份服務器
export RSYNC_PASSWORD=1
rsync -avz $Path/ rsync_backup@172.16.1.41::backup
5.本地保留最近7天的數據
find $Path/ -type d -mtime +7|xargs rm -rf
應用環境:(配置rsync服務->Backup服務器)
1.安裝rsync
2.配置rsync,/etc/rsyncd.conf
3.創建目錄,創建虛擬用戶文件,變更權限
4.啟動服務,加入開機自啟動
5.配置郵箱,准備對應的腳本
1.安裝rsync
[root@m01 ~]# ansible backup -m yum -a "name=rsync state=installed"
2.配置rsync,/etc/rsyncd.conf
[root@m01 ~]# ansible backup -m copy -a "src=./conf/rsyncd.conf dest=/etc/rsyncd.conf"
[root@m01 ~]# pwd
/root
[root@m01 ~]# mkdir conf
[root@m01 ~]# cat conf/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.password
log file = /var/log/rsyncd.log
#####################################
[backup]
path = /backup
[data]
path = /data
.創建目錄,變更權限,創建虛擬用戶文件
[root@m01 ~]# ansible backup -m file -a "path=/backup state=directory mode=755 owner=www group=www"
[root@m01 ~]# ansible backup -m file -a "path=/data state=directory mode=755 owner=www group=www"
[root@m01 ~]# ansible backup -m copy -a "content='rsync_backup:1' dest=/etc/rsync.password mode=600 owner=root group=root"
.啟動服務,加入開機自啟動
[root@m01 ~]# ansible backup -m service -a "name=rsyncd state=started enabled=yes"
5.配置郵箱,准備對應的腳本
應用環境:(配置nfs服務)
1.安裝nfs-utils
2.配置nfs-utils
3.創建對應的共享目錄,並修改權限
4.啟動nfs
1.安裝nfs-utils
[root@m01 ~]# ansible nfs -m yum -a "name=nfs-utils state=installed"
2.配置nfs-utils
[root@m01 ~]# ansible nfs -m copy -a "content='/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)' dest=/etc/exports"
3.創建對應的共享目錄,並遞歸修改權限
[root@m01 ~]# ansible nfs -m file -a "path=/data state=directory recurse=yes owner=www group=www mode=755"
4.啟動nfs
[root@m01 ~]# ansible nfs -m service -a "name=nfs-server state=started enabled=yes"
4.應用環境:(配置web服務,掛載操作)
[root@m01 ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted"
playbook是由一個或多個模塊組成的,使用多個不同的模塊,完成一件事情。
yum
copy
service
安裝一個服務,配置,並啟動。
1.找誰來拍。
2.大概的任務。
3.具體怎么做。
安裝httpd服務->playbook
1.安裝
2.配置
3.啟動
[root@m01 ~]# cat httpd_install.yaml
這是一個ansible的playbook
-空格hosts: web
tasks: #一個tasks是分配任務,
-空格name: Install Httpd Server
yum:
name:空格httpd,httpdtools
state:空格installed
-空格name: Configure Httpd Server
copy:
src:空格/httpd.conf
dest:格/etc/httpd/conf/httpd.conf
-空格name: Start Httpd Server
service:
name:httpd
state:空格started
enabled:空格yes
.修改本地拷貝好的httpd.conf文件
3.執行ansible-playbook httpd_install.yaml 推送
1.環境規划
角色 外網IP(NAT) 內網IP(LAN) 部署軟件
m01 eth0:10.0.0.61 eth1:172.16.1.61 ansible
backup eth0:10.0.0.41 eth1:172.16.1.41 rsync
nfs eth0:10.0.0.31 eth1:172.16.1.31 nfs、Sersync
web01 eth0:10.0.0.7 eth1:172.16.1.7 httpd
1.全網備份
2.實時備份
0.目錄規划
[root@m01 ~]# mkdir /etc/ansible/ansible_playbook/{file,conf,scripts} -p
[root@m01 ~]# tree /etc/ansible/ansible_playbook/
/etc/ansible/ansible_playbook/
├── conf
└── file
└── scripts
##########
1.基礎環境:所有機器統一的配置
1.需要關閉firewalld以及selinux、epel倉庫、ssh端口、優化基礎配置
2.需要安裝rsync和nfs-utils
3.准備www用戶
4.需要准備/etc/rsync.pass密碼文件
5.需要准備全網備份腳本
基礎的playbook劇本
[root@m01 ansible_playbook]# cat base.yaml
2.應用環境:Rsync
1.安裝rsync
2.配置rsync(配置變更,一定要進行重載操作)
3.創建虛擬用戶,權限調整
4.創建目錄/data/ /backup
5.啟動rsync
6.配置郵箱->郵箱的發件人->校驗的腳本
.應用環境:NFS
1.安裝nfs-utils
2.配置nfs (當修改了配置,觸發重載操作)
3.創建目錄,授權
4.啟動
應用環境:Sersync
1.下載sersync
2.解壓,改名,配置
3.啟動
[root@m01 ansible_playbook]# cat sersync.yaml
掛載nfs共享的目錄
關於palybook的網站
https://galaxy.ansible.com/
Ansiblele書寫格式
--- #注釋這里的---代表yaml語法與!/bin/bash一樣 |
---|
--- 注釋這里的---代表yaml語法與!/bin/bash一樣
remote_user: root #注釋冒號后面有空格再參數。這里是指定客戶機root身份執行任務
tasks: #注釋這里就是動作了
-空name: install nginx #注釋這個是干什么用的 Shell: cat /var/log/cron ##注釋 模塊名字后面跟參數
--- hosts: all 一個空格 remote_user: root 2個空格 tasks: 2個空格 - name: cat 3個空格 service: name=mysqld state=stopped 3個空格 - name: xiezai mysql yum: name=mysql state=absent - name: anzhuang mysql yum: name=mysql-server,mysql - name: kaiqimysql service: name=mysqld state=restarted enabled=yes - name: reboot command: reboot~開始我也納悶為什么我的會報錯,原理通俗易懂點就是開始的命令不是一個級別的,同一級別的前面空格必須一樣多不過這樣確實鍛煉寫作水平,不管命令多么長只要字母對齊就可以了 以后shell也能些好看點。~
- host: all 選擇組
Remote_user: root 選擇執行的用戶
tasks: ####記住回車下一行 這里是動作的標題