Ansible

115 阅读9分钟

Ansible

什么是ansible

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。Ansible架构相对比较简单,仅需通过SSH连接客户机执行任务即可

ansible框架主要包括

ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:

(1)、连接插件connection plugins:负责和被监控端实现通信;

(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

(3)、各种模块核心模块、command模块、自定义模块;

(4)、借助于插件完成记录日志邮件等功能;

(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

ansible部署与安装

环境

  • 管理端:192.168.42.18
  • 被管理端:192.168.42.15
  • 被管理端:192.168.42.14

管理端

安装ansible

[root@localhost ~]# yum install -y epel-release
#首先安装epel源,在安装ansible服务
[root@localhost ~]# yum install -y ansible

image.png

ansible目录结构

/etc/ansible/
├── ansible.cfg	#ansible的配置文件,一般无需修改
├── hosts	#ansible的主机清单,用于存储需要管理的远程主机的相关信息
└── roles/	#公共角色目录

管理端

配置主机清单

[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# vim hosts
················
20 [webservers]
#配置组名
················
23 192.168.10.14
#组里面包括被管理的主机ip地址或者主机名
················
33 [dbservers]
34 192.168.42.15
················

image.png

image.png

配置密钥对验证

[root@localhost ansible]# ssh-keygen -t rsa
#设置免密登陆,一直回车
[root@localhost ansible]# vim /etc/ssh/ssh_config
#修改ssh客户端配置文件
·············
35    StrictHostKeyChecking no
#修改35行的ask为no,并且取消注释
·············
[root@localhost ansible]# systemctl restart sshd
#重启ssh服务
[root@localhost ansible]# sshpass -p '123456' ssh-copy-id root@192.168.42.14
[root@localhost ansible]# sshpass -p '123456' ssh-copy-id root@192.168.42.15
或者
ssh-copy-id root@192.168.42.14
ssh-copy-id root@192.168.42.15

image.png

image.png

image.png

ansible模块命令

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

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

image.png

command 模块

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

[root@localhost ansible]# ansible-doc -s command
#列出指定模块的描述信息和操作命令
[root@localhost ansible]# ansible 192.168.42.14 -m command -a 'date'
#指定ip执行date
[root@localhost ansible]# ansible webservers -m command -a 'date'
#指定组执行date
[root@localhost ansible]# ansible dbservers -m command -a 'date'
[root@localhost ansible]# ansible all -m command -a 'date'
#all,表示所有的hosts主机都执行date
[root@localhost ansible]# ansible all -a 'ls /'
#如省略 -m 模块,则默认运行 command 模块

image.png

image.png

command模块常用参数

参数说明
chdir在远程主机上运行命令前提前进入目录
creates判断指定文件是否存在,如果存在,不执行后面的操作
removes判断指定文件是否存在,如果存在,执行后面的操作
[root@localhost ansible]# ansible all -m commad -a "chdir=/home ls ./"

image.png

shell 模块

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

[root@localhost ansible]# ansible-doc -s shell
#列出shell模块信息
[root@localhost ansible]# ansible dbservers -m shell -a 'echo 123456 | passwd --stdin test'
#使用shell免交互方式修改dbservers组里面主机的test用户的密码
[root@localhost ansible]# ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}") | cut -d " " -f2'
[root@localhost ansible]# ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print \$2}")'

image.png

image.png

cron 模块

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

[root@localhost ansible]# ansible-doc -s cron
[root@localhost ansible]# ansible webservers -m cron -a 'minute="*/1" job="/bin/echo helloworld" name="test crontab"'
#写一个计划任务,要求每分钟输出一次helloword
[root@localhost ansible]# ansible webservers -a 'crontab -l'
#查看目标主机的计划任务
[root@localhost ansible]# ansible webservers -m cron -a 'name="test crontab" state=absent'
#移除计划任务test

image.png

image.png

shell模块常用参数

minute/hour/day/month/weekday:分/时/日/月/周

参数说明
job任务计划要执行的命令
name任务计划的名称

user 模块

[root@localhost ansible]# ansible-doc -s user
[root@localhost ansible]# ansible dbservers -m user -a 'name="test01"'
#创建用户test01
[root@localhost ansible]# ansible dbservers -m command -a 'tail /etc/passwd'
#查看目标主机的账户文件
[root@localhost ansible]# ansible dbservers -m user -a 'name="test01" state=absent'
#删除用户test01

image.png

image.png

user模块常用参数

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

group 模块

用户组管理的模块

[root@localhost ~]# ansible-doc -s group
[root@localhost ~]# ansible dbservers -m group -a 'name=mysql gid=306 system=yes'
#创建mysql组
[root@localhost ansible]# ansible dbservers -a 'tail /etc/group'
#查看目标主机的组信息
[root@localhost ansible]# ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=mysql'
#将test01用户谁知为系统用户并且添加到mysql组中
[root@localhost ansible]# ansible dbservers -a 'tail /etc/passwd'
#查看dbservers组中的主机密码账户
[root@localhost ansible]# ansible dbservers -a 'id test01'
#查看test01用户的基本信息

image.png

image.png

image.png

copy 模块

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

copy模块常用参数

参数说明
dest指出复制文件的目标及位置,使用绝对路径,如果是源目录,指目标也要是目录,如果目标文件已经存在会覆盖原有的内容
src指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是目录
mode指出复制时,目标文件的权限
owner指出复制时,目标文件的属主
group指出复制时,目标文件的属组
content指出复制到目标主机上的内容,不能与src一起使用
[root@localhost ansible]# ansible-doc -s copy
[root@localhost ansible]# ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'
#将fstab文件复制到/opt下并修改名字为fstab.bak,修改属主为root,文件权限为640
[root@localhost ansible]# ansible dbservers -a 'ls -l /opt'
#查看opt目录
[root@localhost ansible]# ansible dbservers -a 'cat /opt/fstab.bak'
#查看/opt/fstab.bak内容

[root@localhost ansible]# ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt'
#将helloworld写入/opt/hello.txt文件中
[root@localhost ansible]# ansible dbservers -a 'cat /opt/hello.txt'
#查看

image.png

image.png

image.png

file 模块

设置文件属性

[root@localhost ansible]# ansible-doc -s file

[root@localhost ansible]# ansible dbservers -m file -a 'owner=test01 group=mysql mode=644 path=/opt/fstab.bak'
#修改/opt/fstab.bak文件的属组、属主、权限等
[root@localhost ansible]# ansible dbservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link'
#设置/opt/fstab.link为/opt/fstab.bak的链接文件
[root@localhost ansible]# ansible dbservers -m file -a "path=/opt/abc.txt state=touch"
#创建一个文件
[root@localhost ansible]# ansible dbservers -m file -a "path=/opt/abc.txt state=absent"
#删除一个文件

image.png

image.png

image.png

hostname 模块

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

[root@localhost ansible]# ansible dbservers -m hostname -a "name=mysql01"
#将dbservers组里面的主机名称修改为mysql01

image.png

image.png

ping 模块

检测远程主机的连通性

[root@localhost ansible]# ansible all -m ping
#检测连通性

image.png

yum 模块

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

[root@localhost ansible]# ansible-doc -s yum

[root@localhost ansible]# ansible webservers -m yum -a 'name=httpd'
#安装一个http服务
[root@localhost ansible]# ansible webservers -m yum -a 'name=httpd state=absent'
#卸载http服务

image.png

image.png

image.png

service/systemd 模块

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

常用参数

参数说明
name被管理的服务名称
state=started/stopped/restarted动作包含启动关闭或者重启
enabled=yes/no表示是否设置该服务开机自启
runlevel如果设定了enabled开机自启去,则要定义在哪些运行目标下自启动
[root@localhost ansible]# ansible-doc -s service

[root@localhost ansible]# ansible webservers -a 'systemctl status httpd'
#查看web服务器httpd运行状态
[root@localhost ansible]# ansible webservers -m service -a 'enabled=true name=httpd state=started'
#启动httpd服务

image.png

image.png

script 模块

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

[root@localhost ansible]# ansible-doc -s script

[root@localhost ansible]# vim test.sh
#!/bin/bash
echo "hello ansible from script" > /opt/script.txt

[root@localhost ansible]# chmod +x test.sh
[root@localhost ansible]# ansible webservers -m script -a 'test.sh'
[root@localhost ansible]# ansible webservers -a 'cat /opt/script.txt'

image.png

image.png

setup 模块

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

[root@localhost ansible]# ansible-doc -s setup

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

image.png

image.png

主机清单

Inventory支持对主机进行分组,每个组内可以定义多个主机,每个主机都可以定义在任何一个或多个主机组内。

如果主机名字类似,可以使用列表的方式标记

[root@localhost ansible]# vim /etc/ansible/hosts
··············
[webservers]
23 192.168.42.1[4:6]
#表示主机ip为192.168.42.14,192.168.42.15,192.168.42.16,这三个主机
24 192.168.42.14:2222
#都有192.168.42.14主机,但是端口不一样
#冒号后定义远程连接端口,默认是 ssh 的 22 端口
··············
[dbservers]
38 db-[a:f].example.org
#支持匹配 a~f
··············

inventory 中的变量

变量名说明
ansible_hostansible连接节点时的IP地址
ansible_port连接对方的端口号,ssh连接时默认为22
ansible_user连接对方主机时使用的主机名。不指定时,将使用执行ansible或ansible-playbook命令的用户
ansible_password连接时的用户的ssh密码,仅在未使用密钥对验证的情况下有效
ansible_ssh_private_key_file指定密钥认证ssh连接时的私钥文件
ansible_ssh_common_args提供给ssh、sftp、scp命令的额外参数
ansible_become允许进行权限提升
ansible_become_method指定提升权限的方式,例如可使用sudo/su/runas等方式
ansible_become_user提升为哪个用户的权限,默认提升为root
ansible_become_password提升为指定用户权限时的密

主机变量

[webservers]
192.168.10.14 ansible_port=22 ansible_user=root ansible_password=abc1234

组变量

[webservers:vars]	#表示为 webservers 组内所有主机定义变量
ansible_user=root
ansible_password=abc1234

[all:vars]	#表示为所有组内的所有主机定义变量
ansible_port=22

组嵌套

[nginx]
192.168.10.20
192.168.10.21
192.168.10.22

[apache]
192.168.10.3[0:3]

[webs:children]		#表示为 webs 主机组中包含了 nginx 组和 apache 组内的所有主机
nginx
apache