自动化管理工具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 root@10.0.0.5
# 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 root@10.0.0.61
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.5
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.6
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.7
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.8
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.9
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.31
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.41
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.51
# 编辑 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'