1. 程式人生 > 實用技巧 >自動化管理工具Ansible安裝nginx

自動化管理工具Ansible安裝nginx

Ansible介紹

自動化運維工具,統一配置管理工具。

可以通過一個命令完成一系列的操作,進而能減少重複性的工作和維護成本,可以提高工作效率。

統一配置管理工具:

  • Ansible 開發語言:Python
  • SaltStack 開發語言:Python
  • puppet 開發語言:Ruby

saltstack(擴充套件)

# 1.安裝服務端和客戶端
[root@m01 ~]# yum install -y salt-master salt-minion

# 2.客戶端
[root@m01 ~]# yum install -y  salt-minion

# 3.修改客戶端的配置檔案
[root@m01 ~]# vim /etc/salt/minion
master: 10.0.0.61

# 4.啟動服務端,再啟動客戶端
[root@m01 ~]# systemctl start salt-master salt-minion
[root@m01 ~]# systemctl start salt-minion
[root@oldgirl ~]# /etc/init.d/salt-minion start

# 認證key
[root@m01 ~]# salt-key 
Accepted Keys:
Denied Keys:
Unaccepted Keys:
m01
oldgirl
web01
Rejected Keys:

### 認證1個key
[root@m01 ~]# salt-key -a web01
### 全部認證
[root@m01 ~]# salt-key -A


[root@m01 ~]# salt '*' test.ping
[root@m01 ~]# salt '*' cmd.run 'touch /root/zls123_456.txt'

test
cmd
service

環境準備

安裝Ansible

主機名 wanIP lanIP 角色
m01 10.0.0.61 172.16.1.61 Ansible控制端
web01 10.0.0.7 172.16.1.7 Ansible被控端
web02 10.0.0.8 172.16.1.8 Ansible被控端
web03 10.0.0.9 172.16.1.9 Ansible被控端
lb01 10.0.0.5 172.16.1.5 Ansible被控端
lb02 10.0.0.6 172.16.1.6 Ansible被控端
db01 10.0.0.51 172.16.1.51 Ansible被控端
backup 10.0.0.41 172.16.1.41 Ansible被控端
nfs 10.0.0.31 172.16.1.31 Ansible被控端

安裝ansible

# 安裝ansible
[root@m01 ~]# yum install -y ansible

ansible配置檔案修改

[root@m01 ~]# cat /etc/ansible/ansible.cfg 
#inventory      = /etc/ansible/hosts      #主機列表配置檔案
#library        = /usr/share/my_modules/  #庫檔案存放目錄
#remote_tmp     = ~/.ansible/tmp          #臨時py檔案存放在遠端主機目錄
#local_tmp      = ~/.ansible/tmp          #本機的臨時執行目錄
#forks          = 5                       #預設併發數
#sudo_user      = root                    #預設sudo使用者
#ask_sudo_pass = True                     #每次執行是否詢問sudo的ssh密碼
#ask_pass      = True                     #每次執行是否詢問ssh密碼
#remote_port    = 22                      #遠端主機埠
host_key_checking = False                 #跳過檢查主機指紋
log_path = /var/log/ansible.log           #ansible日誌

Ansible Inventory

主機清單的作用:讓ansible知道,我都要維護哪些機器

使用,IP+PORT+使用者名稱+密碼

vim /etc/ansible/hosts 
[zls_web]
# IP       主機埠              用哪個使用者連線      連線使用者的密碼
10.0.0.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1'
10.0.0.8 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1'

語法:
ansible 指定主機(主機清單的名字,要麼就是IP,要麼就是all) -m 指定要執行的模組

## 第一個命令
[root@m01 ~]# ansible zls_web -m ping
-m:指定模組

使用主機名+密碼的方式

# 寫法一:
[web_group]
web01  ansible_ssh_host='10.0.0.7' ansible_ssh_pass='1'
web02  ansible_ssh_host='10.0.0.8' ansible_ssh_pass='1'

# 寫法二:
[web_group]
web01  ansible_ssh_pass='1'
web02  ansible_ssh_pass='1'

[root@m01 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.7 web01
10.0.0.8 web02

# 寫法三
[web_group]
web0[1:2] ansible_ssh_pass='1'

[root@m01 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.7 web01
10.0.0.8 web02

# 寫法四
[web_group]
web0[1:2] 

[web_group:vars]
ansible_ssh_pass='1'

[root@m01 ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.7 web01
10.0.0.8 web02

主機清單推薦方式:祕鑰

# 1.建立祕鑰對
[root@m01 ~]# ssh-keygen

# 2.推送公鑰
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

# 3.配置主機清單
[web_group]
10.0.0.7:22
10.0.0.8

[backup]
10.0.0.41

##### 推薦使用終極版
[web_group]
web01 ansible_ssh_host=10.0.0.7 ansible_ssh_port=4444
web02 ansible_ssh_host=10.0.0.8

[backup_group]
backup ansible_ssh_host=10.0.0.41

[nfs_group]
nfs ansible_ssh_host=10.0.0.31

[db_group]
db01 ansible_ssh_host=10.0.0.51
db02 ansible_ssh_host=10.0.0.52

ansible主機使用

[root@m01 ~]# ansible web_group --list-host

主機的指定:
1.組名字
2.[]標籤名
3.IP
4.主機名

ansible 的 ad-hoc

ad-hoc簡而言之就是“臨時命令”,執行完即結束,並不會儲存

## ansible的選項
-m:指定模組
-a:指定動作
-i: 指定主機清單

ansible ad-hoc返回顏色

綠色:被控端,主機沒有發生更改

黃色:被控端,主機發生變更

粉色:ansible發出警告

紅色:報錯

檢視幫助手冊

[root@m01 ~]# ansible-doc shell

ad-hoc模組

command

## 作用:執行系統命令
但是,不認識管道符等一些特殊字元
使用shell取代

shell

[root@m01 ~]# ansible web01 -m shell -a 'df -h'

script

[root@m01 ~]# ansible web01 -m script -a '/root/a.sh'

yum

# yum update -y nfs-utils
ansible web_group -m yum -a 'name=nfs-utils state=latest'

# yum install -y net-tools
[root@m01 ~]# ansible web_group -m yum -a 'name=net-tools state=present'

# yum remove -y tree
[root@m01 ~]# ansible web_group -m yum -a 'name=tree state=absent'

# yum 指定網路的rpm包
yum install -y https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.4-1.el7.x86_64.rpm 
[root@m01 ~]# ansible web_group -m yum -a 'name=https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.4-1.el7.x86_64.rpm state=present'

# yum localinstall -y nginx-1.18.0-1.el7.ngx.x86_64.rpm
[root@m01 ~]# ansible backup -m yum -a 'name=/root/nginx-1.18.0-1.el7.ngx.x86_64.rpm state=present'

## 掌握的方法:
name:
	指定服務名:yum install -y tree
	指定網路包:yum install -y https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.4-1.el7.x86_64.rpm
	指定本地包:yum local install -y nginx-1.18.0-1.el7.ngx.x86_64.rpm

state:
	present:正經安裝
	absent:刪除,解除安裝
	latest:安裝最新版本

yum_repository

##新增yum倉庫
[root@m01 ~]# ansible web_group -m yum_repository -a 'name=zls description=xxx baseurl=http://www.baidu.com gpgcheck=no enabled=yes'

## 檔名跟倉庫名不一樣
[root@m01 ~]# ansible web_group -m yum_repository -a 'file=nginx name=zls description=xxx baseurl=http://www.baidu.com gpgcheck=no enabled=yes'

## 刪除yum倉庫
[root@m01 ~]# ansible web_group -m yum_repository -a 'file=nginx name=zls state=absent'

## 往同一個檔案中新增不同的倉庫
[root@m01 ~]# ansible web_group -m yum_repository -a 'file=zls name=zls2 description=ooo baseurl=http://www.google.com gpgcheck=no enabled=yes'

## 掌握的方法:
name:指定倉庫名字
state:
	present 建立
	absent 刪除
file:指定檔名字
baseurl:指定映象源
description:指定描述(repo檔案中的name)
gpgcheck:祕鑰檢查
	yes:開啟
	no:關閉
enabled:是否開啟倉庫
	yes:開啟
	no:關閉

練習:通過61機器,給所有其他機器,新增nginx官方源。安裝nginx

環境準備

伺服器名 外網IP 內網IP 安裝服務 角色
web01 10.0.0.7 172.16.1.7 ansible被控端
web02 10.0.0.8 172.16.1.8 ansible被控端
web03 10.0.0.9 172.16.1.9 ansible被控端
m01 10.0.0.61 172.16.1.61 ansible,nginx源 ansible控制端
backup 10.0.0.41 172.16.1.41 ansible被控端
nfs 10.0.0.31 172.16.1.31 ansible被控端
lb01 10.0.0.5 172.16.1.5 ansible被控端
lb02 10.0.0.6 172.16.1.6 ansible被控端
db01 10.0.0.51 172.16.1.51 ansible被控端

m01ansible控制端部署

# 安裝ansible
[root@m01 ~]# yum -y install ansible
# 編輯ansible的配置檔案開啟下面兩個註釋
[root@m01 ~]# vim /etc/ansible/ansible.cfg 

host_key_checking = False
log_path = /var/log/ansible.log
# 生成金鑰對
[root@m01 ~]# ssh-keygen
# 把公鑰發給其他伺服器
[root@m01 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@m01 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@m01 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@m01 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@m01 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@m01 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@m01 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@m01 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@m01 ~]#  ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

# 編輯 Ansible Inventory新增要管理的伺服器

[root@m01 ~]# vim /etc/ansible/hosts 
[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

[lb_group]
lb01 ansible_ssh_host=10.0.0.5
lb02 ansible_ssh_host=10.0.0.6

[nfs_group]
nfs ansible_ssh_host=10.0.0.31

[backup_group]
backup ansible_ssh_host=10.0.0.41

[database_group]
db01 ansible_ssh_host=10.0.0.51

[nginx_group:children]
web_group
lb_group
nfs_group
backup_group
database_group
# 使用 yum_repository 模組,新增 nginx 官方倉庫
[root@m01 ~]# ansible nginx_group -m yum_repository -a 'name=nginx file=nginx  description="ansible regulatory tools" baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ enabled=yes gpgcheck=no'
# 新增不同的nginx倉庫
#[root@m01 ~]# ansible nginx_group -m yum_repository -a 'name=wzh file=nginx description=wzh baseurl=http://www.google.com gpgcheck=no enabled=yes'

# 刪除倉庫
[root@m01 ~]#  ansible nginx_group -m yum_repository -a 'name=wzh file=nginx state=absent'

# 一鍵在其他伺服器安裝nginx
[root@m01 ~]# ansible nginx_group -m yum -a 'name=nginx state=present'
# 解除安裝其他機器的nginx
[root@m01 ~]# ansible nginx_group -m yum -a 'name=nignx state=absent'