Blog.088 Ansible 自动化运维基础
本章目录
1. Ansible
1.1 应用场景
1.2 特性和原理
2. Ansible 部署
3. Ansible 命令行模块
Ansible是一个基于 Python 开发的配置管理和应用部署工具,现在也在自动化管理领域大放异彩。
它融合了众多老牌运维工具的优点,Pubbet 和 Saltstack 能实现的功能,Ansible 基本上都可以实现。

Ansible 能 批量配置、部署、管理上千台主机。比如以前需要切换到每个主机上执行的一或多个操作,使用 Ansible 只需在固定的一台 Ansible 控制节点上去完成所有主机的操作。
Ansible 是 基于模块工作 的,它只是提供了一种运行框架,它本身没有完成任务的能力,真正执行操作的是 Ansible 的模块 ,比如 copy 模块用于拷贝文件到远程主机上,service 模块用于管理服务的启动、停止、重启等。
Ansible 其中一个比较鲜明的特性是 Agentless ,即无 Agent 的存在,它就像普通命令一样,并非 C/S 软件,也只需在某个作为控制节点的主机上安装一次 Ansible 即可, 通常它基于 ssh 连接 来控制远程主机,远程主机上不需要安装 Ansible 或其它额外的服务。
使用者在使用时,在服务器终端输入命令或者 playbooks,会通过预定好的规则将 playbook 拆解为play,再组织成 ansible 可以识别的任务调用模块和插件,根据主机清单通过 SSH 将临时文件发给远程的客户端执行并返回结果,执行结束后自动删除。
Ansible 的另一个比较鲜明的特性是它的绝大多数模块都具备幂等性(idempotence)。
所谓幂等性,指的是多次操作或多次执行对系统资源的影响是一致的。
比如执行 systemctl stop xxx 命令来停止服务,当发现要停止的目标服务已经处于停止状态,它什么也不会做,所以多次停止的结果仍然是停止,不会改变结果,它是幂等的,而 systemctl restart xxx 是非幂等的。
Ansible 的很多模块在执行时都会先判断目标节点是否要执行任务,所以,可以放心大胆地让 Ansible 去执行任务,重复执行某个任务绝大多数时候不会产生任何副作用。
(1)环境准备
- Ansible 管理服务器:CentOS7(64 位):192.168.30.10:Ansible
- 被管理客户端:CentOS7(64 位):192.168.30.20
- 被管理客户端:CentOS7(64 位):192.168.30.30
(2)所有服务器关闭防火墙和 SElinux
1 systemctl stop firewalld 2 setenforce 0
(3)管理端安装 ansible 服务
cd /etc/yum.repos.d/ mv repo.bak/* ./ yum install -y epel-release yum install -y ansible

(4)配置主机清单
1 vim /etc/ansible/hosts 2 [webservers] #配置组名 3 192.168.30.20 #组里包含的被管理主机IP地址或主机名(主机名需要先修改/etc/hosts文件) 4 5 [dbservers] 6 192.168.30.30

(5)配置密钥对验证
1 ssh-keygen -t rsa #生成密钥对 2 abc123 #设置密码 3 4 ssh-copy-id root@192.168.30.20 5 ssh-copy-id root@192.168.30.30


(6)设置免密登录
1 ssh-agent bash 2 ssh-add 3 abc123

(7)查询被控制端的当前日期测试
1 ansible webservers -m command -a 'date' 2 ansible dbservers -m command -a 'date'

1 ansible-doc -l #查询所有已安装的模块,按q退出
- command 模块
在远程主机执行命令,不支持管道,重定向等 shell 的特性。
command:ansible 的默认模块,不指定 -m 参数的时候,使用的就是 command 模块;
常见的命令都可以使用,但命令的执行不是通过shell来执行的,所以 < > | and & z 这些操作都不可以,不支持管道,没法批量执行命令。
1 ansible-doc -s command #-s 列出指定模块的描述信息和操作动作 2 3 ansible 192.168.30.10 -m command -a 'date' #指定ip执行 date 命令 4 ansible webservers -m command -a 'free' #指定组执行 date 命令 5 ansible dbservers -m command -a 'free' 6 ansible all -m command -a 'date' #all代表所有 hosts 主机 7 ansible all -a 'date' #如省略 -m 模块,则代表为默认的 command 模块 8 9 ###常用用参数 10 chdir:在远程主机上运行命令前提前进入目录 11 creates:判断指定文件是否存在,如果存在,不执行后面的操作 12 removes:判断指定文件是否存在,如果存在,执行后而的操作.

常用的参数:
- chdir:在远程主机上运行命令前提前进入目录
- creates:判断指定文件是否存在,如果存在,不执行后面的操作
- removes:判断指定文件是否存在,如果存在,执行后面的操作
- shell 模块
在远程主机执行命令,相当于调用远程主机的 shell 进程,然后在该 shell 下打开一个子 shell 运行命令(支持管道符号等功能)
1 ansible-doc -s shell 2 ansible webservers -m shell -a 'cat /etc/passwd | wc -l' ##查看passwd文档有多少行

- cron 模块
在远程主机定义任务计划,其中有两种状态(state):present 表示添加(可以省略),absent 表示移除。
1 ansible-doc -s cron #查看相关说明,按q退出 2 3 常用参数: 4 minute/hour/day/month/weekday:分/时/日/月 /周 5 job:任务计划要执行的命令 6 name :任务计划的名称 7 8 ansible webservers -m cron -a 'minute="*/1" job="/bin/echo helloworld" name="test crontab"' 9 ansible webservers -m command -a 'crontab -l' 10 11 ##移除计划性任务,加入该计划没有名字,name=None即可 12 ansible webservers -m cron -a 'name="test crontab" state=absent'


- user 模块
用户管理模块
1 ansible-doc -s user 2 3 常用的参数: 4 name :用户名,必选参数 5 state=present | absent:创建账号或者删除账号,present表示创建,absent 表示删除 6 system=yes | no:是否为系统账号 7 uid: 用户uid 8 group:用户基本组 9 shell:默认使用的shell 10 move_home=yse | no:如果设置的家日录已经存在,是否将已经存在的家日录进行移动 11 password:用户的密码,建议使用加密后的字符串 12 comment:用户的注释信息 13 remove=yes | no:当state=absent时, 是否删除用户的家目录 14 15 ansible webservers -m user -a 'name="test001"' #创建 16 ansible webservers -m command -a 'tail -1 /etc/passwd' #查看确认 17 ansible webservers -m user -a 'name="test001" state=absent' #删除 18 ansible webservers -m command -a 'tail -1 /etc/passwd' #查看确认



- group 模块
用户组管理的模块
1 ansible-doc -s group #查看相关文档 2 3 ansible dbservers -m group -a 'name=mysql gid=300 system=yes' 4 ansible dbservers -m command -a 'tail -1 /etc/group' 5 ansible dbservers -m user -a 'name="test002" uid=300 system=yes group=mysql' 6 ansible dbservers -m command -a 'tail -2 /etc/passwd' 7 ansible dbservers -a 'id test002'


- copy 模块
用于复制指定主机文件到远程主机上
1 ansible-doc -s copy #查看相关文档 2 3 ##常用参数 4 dest:指出复制文件的日标及位置,使用绝对路径,如果是源目录,指目标也要是目录,如果目标文件已经存在会覆盖原有的内容 5 src:指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是目录 6 mode:指出复制时,目标文件的权限 7 owner:指出复制时,日标文件的属主 8 group:指出复制时,目标文件的属组 9 content:指出复制到日标主机上的内容,不能与src一起使用 10 11 ##测试创建文件并修改权限 12 ansible dbservers -a 'mkdir /test' 13 ansible dbservers -m copy -a 'src=/etc/passwd dest=/test/passwd.bak owner=root mode=640' 14 ansible dbservers -a 'ls -l /test' 15 16 ##测试创建文件并写入内容 17 ansible dbservers -m copy -a 'content="this is test txt" dest=/test/test.txt' 18 ansible dbservers -a 'ls -l /test' 19 ansible dbservers -a 'cat /test/test.txt'




- file 模块
设置文件属性
1 ansible-doc -s file 2 3 #修改文件的属主属组权限等 4 ansible dbservers -m file -a 'owner=zhangsan group=mysql mode=777 path=/opt/123.txt' 5 ansible dbservers -a 'ls -l /opt' 6 7 ##设置/opt/123.txt.bak 为 /opt/123.txt 的链接文件 8 ansible dbservers -m file -a 'path=/opt/123.txt.link src=/opt/123.txt state=link' 9 10 ansible dbservers -m file -a 'path=/opt/abc.txt state=touch' #创建一个文件 11 ansible dbservers -m file -a 'path=/opt/abc.txt state=absent' #删除一个文件




- hostname 模块
用于管理远程主机上的主机名
1 ansible dbservers -m hostname -a 'name=testhost'

- ping 模块
检测远程主机的连通性
1 ansible all -m ping

- yum 模块
在远程主机上安装与卸载软件包
1 ansible-doc -s yum 2 3 ansible webservers -m yum -a 'name=httpd' #安装服务 4 ansible webservers -m yum -a 'name=httpd state=absent' #卸载服务


- service/systemd 模块
用于在远程主机上管理服务的运行状态
1 ansible-doc -s service 2 3 ##常用的参数 4 name:被管理的服务名称。 5 state=started | stopped | restarted:动作包含启动关闭或者重启。 6 enabled=yes | no:表示是否设置该服务开机自启。 7 runlevel:如果设定了enabled开机自启去,则要定义在哪些运行目标下自启动。 8 9 ansible webservers -m service -a 'name=httpd enabled=true state=started' #安装服务并设为开机自启 10 systemctl is-enabled httpd.service #被控制端查看是否设为开机自启


- script 模块
实现远程批量运行本地 shell 脚本
1 ansible-doc -s script 2 3 vim test.sh #编写一个脚本 4 #!/bin/bash 5 echo "hello ansible from script" > /opt/script.txt #在script.txt中写入指定内容 6 7 chmod +x test.sh #赋予权限 8 ansible dbservers -m script -a 'test.sh' #实现远程运行本地的脚本 9 ansible dbservers -a 'cat /opt/script.txt' #查看生成的文档内容


- setup 模块
facts 组件是用来收集被管理节点信息的,使用 setup 模块可以获取这些信息
1 ansible-doc -s setup 2 3 ansible webservers -m setup #获取mysql组主机的facts信息 4 ansible dbservers -m setup -a ‘filter=*ipv4’ #使用filter可以筛选指定的facts信息


-
浙公网安备 33010602011771号