1、Linux运维发展史
早期,服务器比较少。
后来,随着网民的增加,服务器的数量节节攀升。
centos 6
centos 7
centos 8
ubuntu 12
ubuntu 14
ubuntu 16
使用Linux自动化运维工具,来完成一次执行多条命令,同时解决服务器版本不同。
Ansible 基于SSH协议的自动化运维工具,不需要客户端,不能够并发执行
saltstack 是C/S结构的工具,支持并发
2、Ansible
Ansible是一个自动化统一配置管理工具,自动化主要体现在Ansible集成了丰富模块以及功能组件,可以通过一个命令完成一系列的操作,进而能减少重复性的工作和维护成本,可以提高工作效率。
3、安装Ansible
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum install ansible
4、ansible的组成
1、Ansible的hosts主机文件
[root@localhost ~]# cat /etc/ansible/hosts
[web] # 分组
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
IP 用户名 端口 密码
2、核心模块
ping
[root@localhost ~]# ansible web -m ping
3.自定义模块custom modules根据自己的需求编写具体的模块
4.插件plugins完成模块功能的补充
5.剧本playbookansible的配置文件,将多个任务定义在剧本中,由ansible自动执行
6.主机清单inventor定义ansible需要操作主机的范围
最重要的一点是 ansible是模块化的 它所有的操作都依赖于模块
5、ansible的具体使用
1、查看ansible版本
[root@localhost ~]# ansible --version
2、执行的详情
[root@localhost ~]# ansible web -v -m command -a 'ls'
3、主机清单路径
[root@localhost ~]# ansible web -i ./hosts -m ping
4、输入SSH密码
[root@localhost ~]# ansible web -k -i ./hosts -m ping
5、测试执行的步骤是否正确
[root@localhost ~]# ansible web -m command -a 'mkdir /ss/dd/aa' -C
练习:
1、使用ansible部署Nginx
[root@localhost ~]# ansible web -m command -a 'yum install nginx -y'
6、ansible配置文件
ansible的配置文件是:/etc/ansible/ansible.cfg
[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日志
7、主机清单
1、基于密码的方式
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
ansible_ssh_user :用户名
ansible_ssh_port :端口
ansible_ssh_pass :密码
2、基于变量密码的方式
[web01]
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22
[web01:vars]
ansible_ssh_pass='1'
3、一个分组配置多主机
[root@localhost ~]# cat /etc/ansible/hosts
[web01]
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22
192.168.15.7 ansible_ssh_user=root ansible_ssh_port=22
4、基于密钥的方式登录
生成密钥
ssh-keygen
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.15.7
[root@localhost ~]# cat /etc/ansible/hosts
[web01]
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22
192.168.15.7 ansible_ssh_user=root ansible_ssh_port=22
5、分组组合
[root@localhost ~]# cat /etc/ansible/hosts
[web01]
192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22
[web02]
192.168.15.7 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
[web:children]
web01
wen02
练习:
使用分组的方式,在多台主机上安装Nginx
使用分组的方式,在多台主机上安装Nginx
[root@m01 ~]# cat hosts
[web01]
1992.168.15.100
[web02]
192.168.15.7
[web:children]
web01
web02
[root@m01 ~]# ansible -i hosts web -m command -a 'yum install -y nginx'