1、传统运维&自动化运维概念
1)传统的运维概念(硬件、软件、系统、网络)
- 手工安装系统、机房建设;
- 软件服务配置、部署通过手工的操作;
- 没有自动化脚本、流程;
- 依靠大量的运维人员完成任务;
2)自动化运维概念(硬件、软件、系统、网络)
- 自动安装系统、机房建设;
- 软件服务配置、部署通过自动化脚本、工具;
- 编写自动化脚本、定制流程;
- 小量运维人员完成复杂的工作任务;
2、自动化运维工具-Ansible实战
1)概述
Ansible是基于Python语言开发的一款自动化运维工具,主要是帮助运维人员去管理Linux服务器,帮助运维人员实现轻松的对操作系统、软件服务、配置文件等管理,能够使用自动化的方式去维护和操作。
2)Ansible自动化运维工具特点:
- 轻量级、所有操作在服务端完成即可;
- 服务端跟客户端通信使用SSH协议(密码、秘钥);
- 客户端无需安装Ansible客户端程序;
- 基于Python语言编写,代码简洁、维护方便;
- 支持Root、普通用户、Sudo用户的操作;
3)Ansible自动化运维工具原理:
Ansible服务端会读取本地hosts文件列表(客户端IP列表),调用本地各种插件和模块,基于SSH协议(用户名|密码|秘钥文件)远程客户端IP主机,让客户端主机执行命令、修改配置、执行任务(Ansible服务端下达的)等,客户端执行任务完成会反馈给服务端或者屏幕终端执行的结果。
4) Ansible自动化运维工具安装与配置:
【1】安装:
[root@localhost ansible]# yum -y remove ansible Loaded plugins: fastestmirror, langpacks Resolving Dependencies --> Running transaction check ---> Package ansible.noarch 0:2.4.2.0-2.el7 will be erased --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================= Package Arch Version Repository Size ========================================================================================================= Removing: ansible noarch 2.4.2.0-2.el7 @extras 38 M Transaction Summary ========================================================================================================= Remove 1 Package Installed size: 38 M Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Erasing : ansible-2.4.2.0-2.el7.noarch 1/1 warning: /etc/ansible/hosts saved as /etc/ansible/hosts.rpmsave Verifying : ansible-2.4.2.0-2.el7.noarch 1/1 Removed: ansible.noarch 0:2.4.2.0-2.el7 Complete! [root@localhost ansible]# ls hosts_bak hosts.rpmsave [root@localhost ansible]# cd / [root@localhost /]# cd [root@localhost ~]# yum -y install ansible Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ---> Package ansible.noarch 0:2.4.2.0-2.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================= Package Arch Version Repository Size ========================================================================================================= Installing: ansible noarch 2.4.2.0-2.el7 extras 7.6 M Transaction Summary ========================================================================================================= Install 1 Package Total download size: 7.6 M Installed size: 38 M Downloading packages: ansible-2.4.2.0-2.el7.noarch.rpm | 7.6 MB 00:00:01 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : ansible-2.4.2.0-2.el7.noarch 1/1 Verifying : ansible-2.4.2.0-2.el7.noarch 1/1 Installed: ansible.noarch 0:2.4.2.0-2.el7 Complete!
【2】查看/etc/ansible/目录下的文件
[root@localhost ~]# ll /etc/ansible/ total 32 -rw-r--r-- 1 root root 19179 Jan 30 2018 ansible.cfg -rw-r--r-- 1 root root 1016 Jan 30 2018 hosts -rw-r--r-- 1 root root 57 Jul 31 21:55 hosts.rpmsave drwxr-xr-x 2 root root 6 Jan 30 2018 roles
【3】修改hosts文件(不是本地DNS文件)
[root@localhost ansible]# cp hosts hosts_bak cp: overwrite ‘hosts_bak’? y [root@localhost ansible]# vim hosts [root@localhost ansible]# cat hosts [localhost] 192.168.249.135 [Nginx1] 192.168.249.132
【4】参数了解
- -m:要执行的模块,默认为command
- -a:模块的参数
- -u:ssh连接的用户名,默认用root,ansible.cfg中可以配置
- -k:提示输入ssh登录密码,当使用密码验证的时候用
- -s:sudo运行
- -U:sudo到哪个用户,默认为root
- -K:提示输入sudo密码,当不是NOPASSWD模式时使用
- -C:只是测试一下会改变什么内容,不会真正去执行
- -c:连接类型(default=smart)
- -f:fork多少进程并发处理,默认为5个
- -i:指定hosts文件路径,默认default=/etc/ansible/hosts
- -I:指定pattern,对已匹配的主机中再过滤一次
- --list-host:只打印有哪些主机会执行这个命令,不会实际执行
- -M:要执行的模块路径,默认为/usr/share/ansible
- -o:压缩输出,摘要输出
- --private-key:私钥路径
- -T:ssh连接超时时间,默认是10秒
- -t:日志输出到该目录,日志文件名以主机命名
- -v:显示详细日志
【5】常用模块
只需要熟练使用 shell 模块和 copy 模块即可,其余的file、yum、command模块等都可以使用shell模块代替
【6】实战操作
1.远程执行“df -h”命令
[root@localhost ansible]# ansible 192.168.249.132 -m command -a "df -h" The authenticity of host '192.168.249.132 (192.168.249.132)' can't be established. ECDSA key fingerprint is SHA256:6t+bT9udQAxIkfD6ncqowUKRgNalEatjusRkAw8pbWo. ECDSA key fingerprint is MD5:46:d3:59:fc:d6:44:03:36:6e:d0:51:c6:66:55:a5:e5. Are you sure you want to continue connecting (yes/no)? yes 192.168.249.132 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.249.132' (ECDSA) to the list of known hosts. Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). ", "unreachable": true }
我们发现让我们输入密码,我们还没输入呢,就已经报错了,所以,我们需要加一个-k参数来输入密码
[root@localhost ansible]# ansible -k 192.168.249.132 -m command -a "df -h" SSH password: 192.168.249.132 | SUCCESS | rc=0 >> Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 17G 3.4G 14G 20% / devtmpfs 475M 0 475M 0% /dev tmpfs 487M 0 487M 0% /dev/shm tmpfs 487M 7.6M 479M 2% /run tmpfs 487M 0 487M 0% /sys/fs/cgroup /dev/sda1 1014M 192M 823M 19% /boot tmpfs 98M 0 98M 0% /run/user/0
2.使用ssh进行此操作
[root@localhost /]# ssh -l root 192.168.249.132 "ip ad" SSH password: 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:61:2d:0a brd ff:ff:ff:ff:ff:ff inet 192.168.249.132/24 brd 192.168.249.255 scope global noprefixroute dynamic ens32 valid_lft 1037sec preferred_lft 1037sec inet6 fe80::dfe1:38b4:8e7d:8765/64 scope link noprefixroute valid_lft forever preferred_lft forever
我们同样可以实现啊,所以我们为什么要是使用ansible呢?我们继续往下做实验
3.同时对2台主机进行操作,执行“df -h”命令
[root@localhost ansible]# ansible all -m shell -a "df -h" #这里的all是指对hosts文件内所有的主机进行操作 192.168.249.135 | SUCCESS | rc=0 >> Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 17G 1.7G 16G 10% / devtmpfs 475M 0 475M 0% /dev tmpfs 487M 144K 487M 1% /dev/shm tmpfs 487M 7.6M 479M 2% /run tmpfs 487M 0 487M 0% /sys/fs/cgroup /dev/sda1 1014M 179M 836M 18% /boot tmpfs 98M 0 98M 0% /run/user/0 192.168.249.132 | SUCCESS | rc=0 >> Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 17G 3.4G 14G 20% / devtmpfs 475M 0 475M 0% /dev tmpfs 487M 0 487M 0% /dev/shm tmpfs 487M 7.6M 479M 2% /run tmpfs 487M 0 487M 0% /sys/fs/cgroup /dev/sda1 1014M 192M 823M 19% /boot tmpfs 98M 0 98M 0% /run/user/0
4.ssh实现不了,但是!我可以使用while循环语句啊:
[root@localhost ansible]# for i in `seq 132 135`;do echo -e "