Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)

前言

常用的自动化工具有:

  • ansible
  • saltstack
  • puppet

这三款都是自动化工具,可以用来提高运维管理效率,在这三款运维工具中目前主流的是ansible和saltstack。ansible和saltstack的区别在于ansible无需安装客户端,这也成为了ansible的一大优势;而saltstack则需要安装客户端,也可以不用安装,他们的适用场景也不一样,ansible适用于小型企业,管理较少的服务器时适用,saltstack则用于中大型企业,因为ansible无法并行执行saltstack则可以并行执行。但是这三款运维工具并不存在优劣,只是适用的场景的不同。

一、Ansible概述

1.1 Ansible介绍

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

1.2 Ansible能做什么

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

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

1.3 为什么选择Ansible

Ansible自2012年发布以来,没多久便在美国开始流行。快速被IT人员接受的原因较大方面得益于Michael DeHaan在美国IT圈的名气和影响力。随后它逐步在各国流行。而笔者选择Ansible的原因主要有如下几个方面:

  • Ansible完全基于Python开发,而DevOps在国内已然是一种趋势,Python被逐步普及,运维人员自己开发工具的门槛逐步降低,得益于此,方便对Ansible二次开发;
  • Ansible丰富的内置模块,甚至还有专门为商业平台开发的功能模块,近600个模块完全可以满足日常功能所需;
  • 在Ansible去中心化概念下,一个简单的复制操作即可完成管理配置中心的迁移;
  • Agentless(无客户端),客户端无需任何配置,由管理端配置好后即可使用,这点非常诱人。在第10章会介绍如何部署配置Windows系统的主机端,用后会有深切的感受。
  • 自Ansible发布后,也陆续被AWS、Google CloudPlatform、Microsoft Azure、Cisco、HP、VMware、Twitter等大公司接纳并投入使用。

1.4 两大特性

  1. agentless:不需要额外安装客户端软件,只要在一台主机上安装anaible即可通过ash控制远程主机ansib1c是通过模块来执行操作的
  2. 幂等性;ansible很多模块在执行时会判断远程主机是否已执行过这个任务,如果已执行且操作没有发生任何改变,则不会再去执行改变结果

1.5 Ansible架构

Ansible 管理节点和远程主机节点间通过SSH协议进行通信。所以配置Ansible的时候,只需保证从Ansible 管理节点通过SSH协议能够连接到被管理的远程节点即可。注意,SSH必须配置为公钥认证登录方式,而非密码认证。

Ansible可以同时管理Red Hat系的Linux、Debian系的Linux以及Windows 主机。Ansible的工作原理如图所示。

在这里插入图片描述

二、部署Ansible

服务器类型 IP地址 需要安装的软件
Ansible管理服务器 192.168.109.138 Ansible
被管理客户端 192.168.109.131 ------
被管理客户端 192.168.109.132 ------

2.1 安装ansible服务

[root@localhost ~]#yum install -y epel-release
[root@localhost ~]#yum install -y ansible

[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# ls
ansible.cfg  hosts  roles
----------------------------------------------------------------
ansible.cfg		# ansible的配置文件,一般无需修改
hosts			# ansible的主机清单,用于存储需要管理的远程主机的相关信息
roles			# 公共角色目录

在这里插入图片描述

2.2 配置主机清单

vim /etc/ansible/hosts				
[webservers]		#配置组名
192.168.109.131		#组里包含的被管理主机IP地址或主机名(主机名需要先修改/etc/hosts文件)
[dbservers]
192.168.109.132

在这里插入图片描述

2.3 配置密钥对验证

#生成密钥对(一路回车)
ssh-keygen -t rsa		
#导入对方主机
ssh-copy-id [email protected]
ssh-copy-id [email protected]

在这里插入图片描述

2.4 设置免交互免密登录

vim /etc/ssh/ssh_config
35    StrictHostKeyChecking no

systemctl restart sshd

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三、Ansible命令模块

命令格式: ansible <组名> -m <模块> -a <参数列表>

ansible-doc -l			#查询所有已安装的模块,按q退出

在这里插入图片描述

有三千多个模块,我们只需要学习常用的

3.1 command模块

在远程主机执行命令,不支持管道,重定向等shell的特性。

ansible-doc -s command					#-s 列出指定模块的描述信息和操作动作

ansible 192.168.109.131 -m command -a 'ifconfig'	#指定ip执行命令
ansible webservers -m command -a 'free'				#指定组执行命令
ansible dbservers -m command -a 'free'
ansible all -m command -a 'date'					#all代表所有 hosts 主机
ansible all -a 'date'								#如省略 -m 模块,则代表为默认的 command 模块

###常用用参数
chdir:在远程主机上运行命令前提前进入目录
creates:判断指定文件是否存在,如果存在,不执行后面的操作
removes:判断指定文件是否存在,如果存在,执行后而的操作

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.1.1 示例:chdir

[root@localhost opt]# ansible dbservers -m command -a 'chdir=/opt ls ./'
192.168.109.132 | CHANGED | rc=0 >>
rh

在这里插入图片描述

3.1.2 示例:creates

#判断指定文件是否存在,如果存在,不执行后面的操作
[root@localhost opt]# ansible dbservers -m command -a 'creates=/opt/123.txt echo helloworld >/opt/123.txt '
192.168.109.132 | CHANGED | rc=0 >>
helloworld >/opt/123.txt

#切换132机子查看
[root@localhost opt]# ls
123.txt  rh

在这里插入图片描述
在这里插入图片描述

3.1.3 示例:removes

#判断指定文件是否存在,如果存在,执行后而的操作
[root@localhost opt]# ansible dbservers -m command -a 'removes=/opt/123.txt touch /opt/123.txt'
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.109.132 | CHANGED | rc=0 >>

[root@localhost opt]# ansible dbservers -m command -a 'removes=/opt/123.txt rm -f /opt/123.txt'
[WARNING]: Consider using the file module with state=absent rather than running 'rm'.  If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.109.132 | CHANGED | rc=0 >>

[root@localhost opt]# ansible dbservers -m command -a 'removes=/opt/123.txt touch /opt/123.txt'
192.168.109.132 | SUCCESS | rc=0 >>
skipped, since /opt/123.txt does not exist

在这里插入图片描述

3.2 shell模块

在远程主机执行命令,相当于调用远程主机的shell进程,然后在该shell 下打开一个子shell运行命令(支持管道符号等功能)

ansible-doc -s shell
#写入helloworld到123.txt
[root@localhost opt]# ansible dbservers -m shell -a 'echo helloworld >/opt/123.txt '
192.168.109.132 | CHANGED | rc=0 >>
#过滤IP地址
[root@localhost opt]# ansible dbservers -m shell -a 'ifconfig ens33|awk "NR==2 {print \$2}"'
192.168.109.132 | CHANGED | rc=0 >>
192.168.109.132

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.3 cron模块

在远程主机定义任务计划,其中有两种状态(state):present表示添加(可以省略),absent表示移除。

ansible-doc -s cron					#查看相关说明,按q退出

常用参数:
minute/hour/day/month/weekday:分/时/日/月 /周
job:任务计划要执行的命令
name :任务计划的名称

#每两个月的10号的早上和晚上十点的第十分钟执行一次复制系统内核日志到/opt/
linux:10 10,22 10 */2 * /usr/bin/cp  /var/log/messages /opt
ansible:
[root@localhost opt]# ansible dbservers -m cron -a 'minute="10" hour="10,20" day="10" month="*/2" job="/usr/bin/cp  /var/log/messages /opt" name="test crontab"'
192.168.109.132 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test crontab"
    ]
}
#查看任务列表
[root@localhost opt]# ansible dbservers -a 'crontab -l'
192.168.109.132 | CHANGED | rc=0 >>
#Ansible: test crontab
10 10,20 10 */2 * /usr/bin/cp  /var/log/messages /opt
[root@localhost opt]# 
#切换到132机子上传查看
[root@localhost opt]# crontab -l
#Ansible: test crontab
10 10,20 10 */2 * /usr/bin/cp  /var/log/messages /opt

在这里插入图片描述
在这里插入图片描述

3.4 user模块

用户管理模块

ansible-doc -s user

常用的参数:
name :用户名,必选参数
state=present|absent:创建账号或者删除账号,present表示创建,absent 表示删除
system=yes|no:是否为系统账号
uid: 用户uid
group:用户基本组
groups:附加组
shell:默认使用的shell
move_home=yse|no:如果设置的家日录已经存在,是否将已经存在的家日录进行移动
password:用户的密码,建议使用加密后的字符串
comment:用户的注释信息
remove=yes|no:当state=absent时, 是否删除用户的家目录

ansible webservers -m user -a 'name="test001"'						#创建
ansible webservers -m command -a 'tail -1 /etc/passwd'				#查看确认
ansible webservers -m user -a 'name="test001" state=absent'			#删除
ansible webservers -m command -a 'tail -1 /etc/passwd'				#查看确认

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.5 group模块

用户组管理的模块

ansible-doc -s group			#查看相关文档

ansible dbservers -m group -a 'name=mysql gid=300 system=yes'
ansible dbservers -m command -a 'tail -1 /etc/group'
ansible dbservers -m user -a 'name="test002" uid=300 system=yes group=mysql'
ansible dbservers -m command -a 'tail -2 /etc/passwd'
ansible dbservers -a 'id test002'

在这里插入图片描述
在这里插入图片描述

3.6 copy模块

用于复制指定主机文件到远程主机上

ansible-doc -s copy				#查看相关文档

##常用参数
dest:指出复制文件的日标及位置,使用绝对路径,如果是源目录,指目标也要是目录,如果目标文件已经存在会覆盖原有的内容
src:指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是目录
mode:指出复制时,目标文件的权限
owner:指出复制时,目标文件的属主
group:指出复制时,目标文件的属组
content:指出复制到目标主机上的内容,不能与src一起使用

##测试创建文件并修改权限
ansible dbservers -a 'mkdir /test'
ansible dbservers -m copy -a 'src=/etc/passwd dest=/test/passwd.bak owner=root mode=640'
ansible dbservers -a 'ls -l /test'

##测试创建文件并写入内容
ansible dbservers -m copy -a 'content="this is test txt" dest=/test/test.txt'
ansible dbservers -a 'ls -l /test'
ansible dbservers -a 'cat /test/test.txt'

在这里插入图片描述
在这里插入图片描述

3.7 file模块

设置文件属性

ansible-doc -s file

#修改文件的属主属组权限等
ansible dbservers -m file -a 'owner=zhangsan group=mysql mode=777 path=/opt/123.txt'	
ansible dbservers -a 'ls -l /opt'

##设置/opt/123.txt.bak 为 /opt/123.txt 的链接文件
ansible dbservers -m file -a 'path=/opt/123.txt.link src=/opt/123.txt state=link'

ansible dbservers -m file -a 'path=/opt/abc.txt state=touch'	#创建一个文件
ansible dbservers -m file -a 'path=/opt/abc.txt state=absent'	#删除一个文件

3.7.1 修改属主和属组及权限

在这里插入图片描述

3.7.2 创建软链接

在这里插入图片描述

3.7.3 创建文件,并删除文件

在这里插入图片描述

3.8 hostname 模块

用于管理远程主机上的主机名

ansible dbservers -m hostname -a 'name=testhost'

在这里插入图片描述
在这里插入图片描述

3.9 ping 模块

ansible all -m ping 

在这里插入图片描述

3.10 yum 模块

在远程主机上安装与卸载软件包

ansible-doc -s yum

ansible webservers -m yum -a 'name=httpd'						#安装服务
ansible webservers -m yum -a 'name=httpd state=absent'			#卸载服务

在这里插入图片描述

3.11 service/systemd 模块

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

ansible-doc -s service

##常用的参数
name:被管理的服务名称。
state=started | stopped | restarted:动作包含启动关闭或者重启。
enabled=yes | no:表示是否设置该服务开机自启。
runlevel:如果设定了enabled开机自启去,则要定义在哪些运行目标下自启动。

ansible webservers -m service -a 'name=httpd enabled=true state=started'		#安装服务并设为开机自启
systemctl is-enabled  httpd.service			#被控制端查看是否设为开机自启

在这里插入图片描述
在这里插入图片描述

3.12 script 模块

实现远程批量运行本地 shell 脚本

ansible-doc -s script

vim test.sh						#编写一个脚本
#!/bin/bash
echo "hello ansible from script" > /opt/script.txt		#在script.txt中写入指定内容

chmod +x test.sh 										#赋予权限
ansible dbservers -m script -a 'test.sh'				#实现远程运行本地的脚本
ansible dbservers -a 'cat /opt/script.txt'				#查看生成的文档内容

在这里插入图片描述

3.13 setup 模块

facts组件是用来收集被管理节点信息的,使用 setup 模块可以获取这些信息

ansible-doc -s setup

ansible webservers -m setup							#获取webservers组主机的facts信息
ansible webservers -m setup -a 'filter=*ipv4'		#使用filter可以筛选指定的facts信息

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42327944/article/details/125631269