在日常企业运维工作中,工作经常遇到有多台主机需要进行管理操作,太忙并且操作的文详维轻任务还都是相同的,这样重复性的自动作效操作任务会大大增加运维人员的工作量。
因此可以通过软件工具,化运将重复性的松提升工工作任务,进行批量的率上自动化完成,从而形成高效运维的工作管理体系。
注意事项:
想要自动化一定要先标准化(所有的太忙环境、云服务器提供商软件、文详维轻目录、自动作效一致);
尽量进行分组(比如:所有web服务器是化运1组).
自动化工具选择

Ansible架构

Ansible极速使用指南
环境准备

Ansible部署
yum install -y ansible #epel源中的软件包初步配置主机清单(inventory)
[root@m01 ~]# cat /etc/ansible/hosts [oldboy] 172.16.1.7 172.16.1.31 172.16.1.41 172.16.1.51与Ansible的第1次接触
ansible oldboy -m ping

Ansible配置文件
/etc/ansible/ansible.cfg #ansible配置文件 configure [root@m01 ~]# grep -n ^host_key /etc/ansible/ansible.cfg 71:host_key_checking = False /etc/ansible/hosts #ans主机清单,默认是.Ansible主机清单
基本格式
[root@m01 ~]# cat /etc/ansible/hosts [oldboy] #[组的名字] 172.16.1.7 172.16.1.31 172.16.1.41 172.16.1.51 [web] 172.16.1.7 [db] 172.16.1.51 [nfs] 172.16.1.31 [backup] 172.16.1.41指定用户名、密码、松提升工端口

子组
我想把多个主机分组进行合并,率上比如把db,nfs,backup合并成一个组叫data组.
[web] 172.16.1.7 [db] 172.16.1.51 [nfs] 172.16.1.31 [backup] 172.16.1.41 [data:children] #data:children表示 data是创建的子组 组里面包含 db,nfs,backup3个组. db nfs backup主机清单案例
[root@m01 ~]# cat /etc/ansible/hosts [web] 172.16.1.7 [nfs] 172.16.1.31 [backup] 172.16.1.41 [db] 172.16.1.51 [data:children] db nfs backup案例01 查看与使用指定的子组 data
[root@m01 ~]# ansible data -m command -a hostname 172.16.1.31 | CHANGED | rc=0 >> nfs01 172.16.1.51 | CHANGED | rc=0 >> db01 172.16.1.41 | CHANGED | rc=0 >> backup案例02:查看指定的组db
[root@m01 ~]# ansible db -m command -a hostname 172.16.1.51 | CHANGED | rc=0 >> db01案例03:查看所有主机情况
[root@m01 ~]# ansible all -m command -a hostname 172.16.1.51 | CHANGED | rc=0 >> db01 172.16.1.41 | CHANGED | rc=0 >> backup 172.16.1.31 | CHANGED | rc=0 >> nfs01 172.16.1.7 | CHANGED | rc=0 >> web01 [root@m01 ~]#案例04:查看某一台机器
[root@m01 ~]# ansible 172.16.1.7 -m command -a hostname 172.16.1.7 | CHANGED | rc=0 >> web01 [root@m01 ~]#Ansible-命令与脚本类模块
1)command模块
仅支持简单命令,不支持特殊符号,工作管道。。免费源码下载。
这个模块是默认模块,ansible不加上模块,默认就使用这个模块
ansible all -m command -a 命令 ansible all -a hostname #相当于省略 -m command2)shell模块
与command模块类似,shell模块支持特殊符号,执行脚本。。。
[root@m01 ~]# ansible all -m command -a ip a s eth0 |sed -n 3p 172.16.1.7 | FAILED | rc=255 >> Error: either "dev" is duplicate, or "|sed" is a garbage.non-zero return code 172.16.1.51 | FAILED | rc=255 >> Error: either "dev" is duplicate, or "|sed" is a garbage.non-zero return code 172.16.1.31 | FAILED | rc=255 >> Error: either "dev" is duplicate, or "|sed" is a garbage.non-zero return code 172.16.1.41 | FAILED | rc=255 >> Error: either "dev" is duplicate, or "|sed" is a garbage.non-zero return code [root@m01 ~]# ansible all -m shell -a ip a s eth0 |sed -n 3p 172.16.1.31 | CHANGED | rc=0 >> inet 10.0.0.31/24 brd 10.0.0.255 scope global eth0 172.16.1.41 | CHANGED | rc=0 >> inet 10.0.0.41/24 brd 10.0.0.255 scope global eth0 172.16.1.7 | CHANGED | rc=0 >> inet 10.0.0.7/24 brd 10.0.0.255 scope global eth0 172.16.1.51 | CHANGED | rc=0 >> inet 10.0.0.51/24 brd 10.0.0.255 scope global eth0
3)script模块-传输脚本到被管理端并执行脚本
安装ipvsadm [root@m01 ~]# cat /server/scripts/yum.sh yum install -y ipvsadm 使用script 模块执行脚本. ansible db -m script -a /server/scripts/yum.sh4)命令与脚本模块

Ansible-文件与目录管理模块
1)file模块
管理文件或目录、软链接。

案例01-创建目录/oldboy/目录
ansible all -m file -a path=/oldboy state=directory ansible all -a ls -ld /oldboy/案例02-创建文件/oldboy/oldboy.txt文件
ansible all -m file -a path=/oldboy/oldboy.txt state=touch ansible all -a ls -l /oldboy/案例03-创建软连接 /oldboy/oldboy.txt 到/tmp/oldboy.txt.soft
ansible all -m file -a src=/oldboy/oldboy.txt path=/tmp/oldboy.txt.soft state=link [root@m01 ~]# [root@m01 ~]# [root@m01 ~]# ansible all -a ls -l /tmp/oldboy.txt.soft 172.16.1.31 | CHANGED | rc=0 >> lrwxrwxrwx 1 root root 18 Apr 12 12:01 /tmp/oldboy.txt.soft -> /oldboy/oldboy.txt 172.16.1.41 | CHANGED | rc=0 >> lrwxrwxrwx 1 root root 18 Apr 12 12:01 /tmp/oldboy.txt.soft -> /oldboy/oldboy.txt 172.16.1.51 | CHANGED | rc=0 >> lrwxrwxrwx 1 root root 18 Apr 12 12:01 /tmp/oldboy.txt.soft -> /oldboy/oldboy.txt 172.16.1.7 | CHANGED | rc=0 >> lrwxrwxrwx 1 root root 18 Apr 12 12:01 /tmp/oldboy.txt.soft -> /oldboy/oldboy.txt案例04-删除文件/目录/软连接
ansible all -m file -a path=/oldboy/oldboy.txt state=absent #删除文件 ansible all -m file -a path=/oldboy state=absent #删除目录 ansible all -m file -a path=/tmp/oldboy.txt.soft state=absent #删除软连案例05-创建文件/tmp/oldboy.txt,所有者root,用户组root,权限755
ansible all -m file -a path=/tmp/oldboy.txt owner=root group=root mode=755 state=touch ansible all -a ls -l /tmp/oldboy.txt2)copy远程传输模块

案例01-传输/etc/hosts文件到/etc/hosts
ansible all -m copy -a src=/etc/hosts dest=/etc/hosts案例02-传输/etc/hosts文件到/etc/hosts-先备份然后修改
ansible all -m copy -a src=/etc/hosts dest=/etc/hosts backup=yes [root@m01 ~]# ansible all -m shell -a ls -l /etc/hosts* 172.16.1.41 | CHANGED | rc=0 >> -rw-r--r-- 1 root root 328 Apr 12 12:24 /etc/hosts -rw-r--r-- 1 root root 327 Apr 12 12:21 /etc/hosts.81057.2022-04-12@12:24:20~ -rw-r--r--. 1 root root 370 Jun 7 2013 /etc/hosts.allow -rw-r--r--. 1 root root 460 Jun 7 2013 /etc/hosts.deny 172.16.1.7 | CHANGED | rc=0 >> -rw-r--r-- 1 root root 328 Apr 12 12:24 /etc/hosts -rw-r--r-- 1 root root 327 Apr 12 12:21 /etc/hosts.18115.2022-04-12@12:24:20~ -rw-r--r--. 1 root root 370 Jun 7 2013 /etc/hosts.allow -rw-r--r--. 1 root root 460 Jun 7 2013 /etc/hosts.deny 172.16.1.31 | CHANGED | rc=0 >> -rw-r--r-- 1 root root 328 Apr 12 12:24 /etc/hosts -rw-r--r-- 1 root root 327 Apr 12 12:21 /etc/hosts.46979.2022-04-12@12:24:20~ -rw-r--r--. 1 root root 370 Jun 7 2013 /etc/hosts.allow -rw-r--r--. 1 root root 460 Jun 7 2013 /etc/hosts.deny 172.16.1.51 | CHANGED | rc=0 >> -rw-r--r-- 1 root root 328 Apr 12 12:24 /etc/hosts -rw-r--r-- 1 root root 327 Apr 12 12:21 /etc/hosts.21455.2022-04-12@12:24:20~ -rw-r--r--. 1 root root 370 Jun 7 2013 /etc/hosts.allow -rw-r--r--. 1 root root 460 Jun 7 2013 /etc/hosts.deny3)文件管理与传输模块小结

Ansible-服务管理模块
1)systemd

案例01-关闭firewalld
ansible all -m systemd -a name=firewalld enabled=no state=stopped ansible all -a systemctl status firewalld案例02-开启sshd服务
ansible all -m systemd -a name=sshd enabled=yes state=started ansible all -a systemctl status sshd案例03-重启backup这台机器上面的rsync服务
ansible backup -m systemd -a name=rsyncd state=restarted2)service 了解

3)服务管理模块小结

Ansible-软件包管理模块
1)yum源配置管理模块

案例01-批量添加nginx-yum源
[nginx] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key ansible web -m yum_repository -a name=nginx description="nginx stable repo" baseurl="http://nginx.org/packages/centos/$releasever/$basearch/" gpgcheck=no enabled=yes #注意未来也可以通过,copy模块实现.2)yum模块

案例01-安装lrzsz
ansible all -a rpm -e lrzsz ansible all -m yum -a name=lrzsz state=installed案例02-安装sl、cowsay、aalib
ansible web -m yum -a name=sl,cowsay,aalib state=installed3)get_url模块(wget)

案例01-下载tengine源码包到/server/tools/(不存在)目录下
下载地址: https://tengine.taobao.org/download/tengine-2.3.3.tar.gz ansible web -m file -a path=/server/tools/ state=directory ansible web -m get_url -a url=https://tengine.taobao.org/download/tengine-2.3.3.tar.gz dest=/server/tools/ ansible web -a tree /server/4)软件包管理模块小结

后续内容推荐阅读:运维工作太忙?一文详解Ansible自动化运维,轻松提升工作效率(下)!
(责任编辑:数据库)