Ansible自动化运维工具

85 阅读7分钟

ansible概述

Ansible是一个基于Python开发的配置管理和应用部署工具,现在也在自动化管理领域大放异彩。它融合了众多老牌运维工具的优点,Pubbet和Saltstack能实现的功能,Ansible基本上都可以实现。

Ansible能批量配置、部署、管理上千台主机。比如以前需要切换到每个主机上执行的一或多个操作,使用Ansible只需在固定的一台Ansible控制节点上去完成所有主机的操作。

Ansible是基于模块工作的,它只是提供了一种运行框架,它本身没有完成任务的能力,真正执行操作的是Ansible的模块, 比如copy模块用于拷贝文件到远程主机上,service模块用于管理服务的启动、停止、重启等。

ansible的特性

特性一

Ansible其中一个比较鲜明的特性是Agentless,即无Agent的存在,它就像普通命令一样,并非C/S软件,也只需在某个作为控制节点的主机上安装一次Ansible即可,通常它基于ssh连接来控制远程主机,远程主机上不需要安装Ansible或其它额外的服务。

使用者在使用时,在服务器终端输入命令或者playbooks,会通过预定好的规则将playbook拆解为play,再组织成ansible可以识别的任务,调用模块和插件,根据主机清单通过SSH将临时文件发给远程的客户端执行并返回结果,执行结束后自动删除

特性二

Ansible的另一个比较鲜明的特性是它的绝大多数模块都具备幂等性(idempotence)。所谓幂等性,指的是多次操作或多次执行对系统资源的影响是一致的。比如执行 systemctl stop xxx 命令来停止服务,当发现要停止的目标服务已经处于停止状态, 它什么也不会做,所以多次停止的结果仍然是停止,不会改变结果,它是幂等的,而 systemctl restart xxx 是非幂等的。

Ansible的很多模块在执行时都会先判断目标节点是否要执行任务,所以,可以放心大胆地让Ansible去执行任务,重复执行某个任务绝大多数时候不会产生任何副作用。

ansible环境安装

管理端    192.168.50.100  ansible
被管理端  192.168.50.110  无需安装
被管理端  192.168.50.105  无需安装
 #1、管理端安装ansible
 yum install -y epel-release   
 #先安装epel源
 yum install -y ansible        
 #安装ansible
 yum install -y tree
 #显示树状图
 
 #ansible目录结构
 cd /etc/ansible
  tree
 .
 ├── ansible.cfg
 ├── hosts
 └── roles
 
 1 directory, 2 files
 
 
 #2、配置主机清单,修改/etc/ansible/hosts文件
 cd /etc/ansible
 vim hosts
 [webservers]    #配置组名
 192.168.41.42   #组里包含被管理的主机IP或主机名(主机名需要先修改/etc/hosts文件)
 192.168.41.43
 
 [dbservers]      #第二个组
 192.168.41.44
 
 #3、ansible默认使用ssh连接,所以管理前要设置免密登录
 #配置密钥对验证
 ssh-keygen -t rsa    #一路回车,生成密钥文件
 
 vim /etc/ssh/ssh_config      #修改ssh客户端配置文件
 StrictHostKeyChecking no     #35行,取消注释,将ask修改为no,开启免交互
 
 systemctl restart sshd       #重启sshd
 
 #使用sshpass,以免交互的方式将公钥文件传给被管理端,实现免密登录
 sshpass -p '144080' ssh-copy-id root@192.168.41.42
 sshpass -p '144080' ssh-copy-id root@192.168.41.43
 sshpass -p '144080' ssh-copy-id root@192.168.41.44
 
 #如果被管理端主机有很多台,可以通过sshpass和for循环写一个脚本,实现多台主机免密登录。

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

ansible常用的命令行模块

ansible管理命令:

 ansible <组名> -m <模块> -a <参数列表>
 
 ansible <主机IP> -m <模块> -a <参数列表>
 
 ansible <主机名> -a <参数列表>      
 #不加-m指定模块默认使用command
 
 #选项解释
 -m: 指定模块
 -a: 指定命令

查看所有ansible的模块

 ansible-doc -l   
 #列出所有已安装的模块,按q退出
 
 ansible-doc -l | wc -l   
 #统计总共有多少模块
 3387
 
 ansible-doc -s 模块   
 #查看指定模块的描述信息和操作动作

image.png

image.png

ansible模块

command

ansible 192.168.50.105 -m command -a 'date'
# 指定IP地址执行查看IP地址
ansible test -m command -a 'date'
# 地址分组执行查看IP地址
ansible all -m command -a 'date'
# all代表所有分组,执行查看IP地址
ansible all -a 'date'	
# -m command可以不写,默认运行command命令

image.png

cron模块

cron模块有两种状态(state):分别是present表示添加(可以省略),absent表示移除

ansible dbservers -m cron -a 'minute="*/1" job="/bin/echo hehe" name="test01 hehe"'
# 创建计划任务
ansible dbservers -a 'crontab -l'		
# 查看计划任务
ansible dbservers -m cron -a ‘name=" hehe" state=absent’
# 移除计划任务

image.png

image.png

image.png

user模块

user模块请求的是useradd,userdel,usermod三个指令

ansible dbservers -m user -a 'name=lisi'
# 创建用户lisi
ansible dbservers -a 'tail /etc/passwd'	
ansible dbservers -m user -a 'name="lisi" state=absent'
# 删除用户

image.png

image.png

group模块

group模块请求的是groupadd、groupdel、groupmod三个指令

ansible dbservers -m group -a 'name=mysql gid=306 system=yes'
ansible dbservers -a 'tail /etc/group'						# tail查看末尾10行
ansible dbservers -m user -a 'name=lisi uid=306 system=yes group=mysql'
ansible test01 -a 'id lisi'

image.png

copy模块

 #将本地的/etc/hosts文件,复制到远程主机的/data/目录下,并重命名为hosts.bak,文件属主设置为root、属组设置为mysql,权限设置为640。
 ansible dbservers -m copy -a 'src=/etc/hosts dest=/data/hosts.bak owner=root group=mysql mode=640'
 ansible dbservers -a 'ls -l /data'     
 #查看文件权限
 
  #将helloworld写入远程主机的/data/hello.txt文件中
 ansible dbservers -m copy -a 'content="helloworld" dest=/data/hello.txt'
 ansible dbservers -a 'cat /data/hello.txt'

image.png

image.png

file模块

 ansible-doc -s file    #查看file模块包含的动作
 
 #修改文件的属主、属组、权限等。
 ansible dbservers -m file -a 'path=/data/hosts.bak owner=test02 group=root mode=644'
 ansible dbservers -a 'ls -l /data'
 
  #设置/opt/passwd.link为/etc/passwd的链接文件。state=link表示创建软链接。
 ansible dbservers -m file -a 'path=/opt/passwd.link src=/etc/passwd state=link'
 ansible dbservers -a 'ls -l /opt'
 
 #注意:src指的是目标主机上的源文件,与copy模块不同。
 
  #创建一个文件
 ansible dbservers -m file -a 'path=/data/aa.txt state=touch'
 ansible dbservers -a 'ls /data'
 
 #删除文件/data/aa.txt
 ansible dbservers -m file -a 'path=/data/aa.txt state=absent'
 ansible dbservers -a 'ls /data'
 
 #创建一个目录
 ansible dbservers -m file -a 'path=/data/hr state=directory'
 ansible dbservers -a 'ls -l /data'

image.png

image.png

image.png (设置/opt/passwd.link为/etc/passwd的链接文件。 state=link表示创建软链接)

image.png

image.png

image.png

hostname模块

管理远程主机的主机名

 ansible dbservers -m hostname -a 'name=myhost7-4'  #修改dbservers组的主机名

image.png

ping模块

测试远程主机的连通性

 ansible all -m ping   #测试所有主机的连通性

image.png

yum模块

 ansible-doc -s yum     #查看yum模块包含的操作动作
 
 ansible dbservers -m yum -a 'name=httpd'     #安装httpd服务
 ansible dbservers -m yum -a 'name=httpd state=absent'   #卸载httpd服务
 
 #yum一次性卸载所有主机的httpd服务
 ansible all -m yum -a "name=httpd state=absent"

image.png

image.png

image.png

image.png

image.png

service/systemd模块

用于管理远程主机上的服务运行状态

 ansible-doc -s service     #查看service模块包含的操作动作
 
 #查看web服务器httpd运行状态
 ansible webservers -a 'systemctl status httpd'
 
 #启动httpd服务,并设置为开机自启
 ansible webservers -m service -a 'name=httpd state=started enabled=true'

image.png

image.png (启动httpd服务,并设置为开机自启)

image.png

script模块

实现远程批量运行本地的shell脚本 注意:script模块不具有幂等性,所以建议用剧本来执行

 ansible-doc -s script
 
 #在本地写一个脚本
 vim test.sh
 #!/bin/bash
 echo "hello ansible from script" > /data/script.txt
 
 chmod +x test.sh                              #给脚本执行权限
 ansible dbservers -m script -a 'test.sh'      #远程运行本地脚本
 ansible dbservers -a 'cat /data/script.txt'   #查看生成的文件内容是否为指导内容
 
 
 #再次运行相同脚本,状态为changed,而不是successs,因为script模块不具有幂等性
 ansible dbservers -m script -a 'test.sh'

image.png

setup模块

 ansible-doc -s setup   #查看setup模块包含的操作动作
 
 #获取dbservers组主机的facts信息
 ansible dbservers -m setup
 
 #使用filter参数可以筛选指定的facts信息
 ansible dbservers -m setup -a 'filter=*ipv4'

image.png

image.png