目录
[管理端安装 ansible:](#管理端安装 ansible:)
[ansible 目录结构:](#ansible 目录结构:)
[ansible 命令行模块 :](#ansible 命令行模块 :)
[1.command 模块](#1.command 模块)
[指定 ip 执行 date:](#指定 ip 执行 date:)
[2.shell 模块](#2.shell 模块)
[3.cron 模块](#3.cron 模块)
[4.user 模块](#4.user 模块)
[5.group 模块](#5.group 模块)
[6.copy 模块](#6.copy 模块)
[7.file 模块](#7.file 模块)
[8.hostname 模块](#8.hostname 模块)
[9.ping 模块](#9.ping 模块)
[10.yum 模块](#10.yum 模块)
[11.service/systemd 模块](#11.service/systemd 模块)
[12.script 模块](#12.script 模块)
[13. mount 模块](#13. mount 模块)
[14. archive 模块](#14. archive 模块)
[15. unarchive 模块](#15. unarchive 模块)
[16. replace 模块](#16. replace 模块)
[17.setup 模块](#17.setup 模块)
[三.inventory 主机清单](#三.inventory 主机清单)
ansible :自动化运维工具,也叫机器管理工具。可以实现批量管理成百上千的多台主机,是应用级别的跨主机编排工具。
ansible的特性:.
1)无客户端agent的存在,不需要在被控制的节点上安装任何客户端应用。
2)通过ssh协议与被控制节点通信
3)基于模块工作的,可以通过模块实现在被控制节点上执行相应的命令操作
4)很多模块具有幂等性,即可实现多次模块操作的状态如果没有发生改变,则不会重复执行

二.部署ansible
准备三台主机,192.168.233.10控制管理端,192.168.233.20被控制端,192.168.233.30被控制端
管理端安装 ansible:
yum install -y epel-release :先安装 epel 源

yum install -y ansible

ansible 目录结构:

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


配置密钥对验证:
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa

sshpass -p '123' ssh-copy-id -o StrictHostKeyChecking=no root@192.168.233.20
sshpass -p '123' ssh-copy-id -o StrictHostKeyChecking=no root@192.168.233.30


ansible 命令行模块 :
命令格式:ansible <组名> -m <模块> -a <参数列表>
ansible-doc -l #列出所有已安装的模块,按q退出

1.command 模块
是ansible默认模块,在远程主机执行命令,不支持管道,重定向等shell的特性。
ansible-doc -s command #-s 列出指定模块的描述信息和操作动作

指定 ip 执行 date:
ansible 192.168.233.20 -m command -a "date"

指定组执行 date
ansible webservers -m command -a 'date'
ansible dbservers -m command -a 'date'


all 代表所有 hosts 主机
ansible all -m command -a 'date'

如省略 -m 模块,则默认运行:
ansible all -a 'ls /'

常用的参数:
chdir:在远程主机上运行命令前提前进入目录
creates:判断指定文件是否存在,如果存在,不执行后面的操作
removes:判断指定文件是否存在,如果存在,执行后面的操作
ansible all -m command -a "chdir=/home ls ./"
ansible dbservers -m command -a "creates=/opt/a touch /opt/a"
ansible dbservers -m command -a "removes=/opt/a touch /opt/b"

看下30主机/opt下文件:

如果30主机/opt下没有a文件就会执行touch命令:

看下30主机:

如果执行ansible dbservers -m command -a "creates=/opt/a touch /opt/b"这条命令,a文件有了就不会再执行touch命令:


删除a文件:


ansible dbservers -m command -a "removes=/opt/a rm -rf /opt/a"




2.shell 模块
在远程主机执行命令,相当于调用远程主机的shell进程,然后在该shell下打开一个子shell运行命令(支持管道符号等功能)
ansible-doc -s shell :查看操作

ansible dbservers -m shell -a 'ifconfig ens33 | awk "NR==2{print $2}"'

.
ansible dbservers -m shell -a 'echo aaa >/opt/b'


3.cron 模块
在远程主机定义任务计划。其中有两种状态(state):present表示添加(可以省略),absent表示移除。
job:任务计划要执行的命令
name:任务计划的名称
user:指定计划任务属于哪个用户,默认是root用户
ansible-doc -s cron

ansible dbservers -m cron -a 'minute="*/1" job="/bin/echo helloworld" name="test crontab"'


删除计划:
ansible dbservers -m cron -a 'name="test crontab" state=absent'
4.user 模块
用户管理的模块:
ansible-doc -s user

常用的参数:
name:用户名,必选参数
state=present|absent:创建账号或者删除账号,present表示创建,absent表示删除
system=yes|no:是否为系统账号
uid:用户uid
group:用户基本组
groups: 用户所属附加组
shell:默认使用的shell
create_home=yse|no: 是否创建家目录
password:用户的密码,建议使用加密后的字符串
remove=yes|no:当state=absent时,是否删除用户的家目录
ansible dbservers -m user -a 'name="test01" ' :创建用户



ansible dbservers -m user -a 'name="test01" state=absent' :删除用户



5.group 模块
用户组管理的模块:ansible-doc -s group

创建mysql组:
ansible dbservers -m group -a 'name=mysql gid=306 system=yes'


将test01用户添加到mysql组中:
ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=mysql'

6.copy 模块
用于复制指定主机文件到远程主机的:
ansible-doc -s copy

常用的参数:
dest:指出复制文件的目标及位置,使用绝对路径,如果源是目录,指目标也要是目录,如果目标文件已经存在会覆盖原有的内容
src:指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是目录
mode:指出复制时,目标文件的权限
owner:指出复制时,目标文件的属主
group:指出复制时,目标文件的属组
content:指出复制到目标主机上的内容,不能与src一起使用
ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'


ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt'


7.file 模块
设置文件属性:
ansible-doc -s file

修改文件的属主属组权限等:
ansible dbservers -m file -a 'owner=test01 group=mysql mode=644 path=/opt/fstab.bak'


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


创建一个文件:
ansible dbservers -m file -a "path=/opt/abc.txt state=touch"


删除一个文件:
ansible dbservers -m file -a "path=/opt/abc.txt state=absent"
8.hostname 模块
用于管理远程主机上的主机名:
ansible dbservers -m hostname -a "name=mysql01":主机名改为mysql01


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

关闭一台,在ping:


10.yum 模块
在远程主机上安装与卸载软件包
ansible-doc -s yum

ansible dbservers -m yum -a 'name=httpd state=latest':latest表示下载最新的



卸载服务:
ansible dbservers -m yum -a 'name=httpd state=absent'


11.service/systemd 模块
用于管理远程主机上的管理服务的运行状态
ansible-doc -s service

常用的参数:
name:被管理的服务名称
state=started|stopped|restarted:动作包含启动关闭或者重启
enabled=yes|no:表示是否设置该服务开机自启
runlevel:如果设定了enabled开机自启去,则要定义在哪些运行目标下自启动
查看防火墙运行状态:
ansible dbservers -a 'systemctl status firewalld'

启动服务:
ansible dbservers -m service -a 'name=firewalld enabled=true state=started'



关闭服务:
ansible dbservers -m systemd -a 'name=firewalld enabled=no state=stopped'


12.script 模块
实现远程批量运行本地的 shell 脚本
ansible-doc -s script

编写脚本;



ansible dbservers -m script -a 'test.sh'


13. mount 模块
挂载文件系统:
ansible-doc -s mount

常用的参数:
src:定义挂载设备的路径
path:定义挂载到哪个目录,必须指定
fstype:指定挂载文件的系统类型,必须指定,xfs、iso9660、nfs...
opts:定义挂载的参数,defaults、rw、ro...
state:定义挂载的状态,mounted(进行挂载,修改/etc/fstab信息)、absent(永久性卸载,并修改 /etc/fstab信息)、unmounted(临时卸载,不修改/etc/fstab信息)
ansible dbservers -m mount -a 'src=/dev/sr0 path=/mnt state=mounted fstype=xfs'




解挂载:
ansible dbservers -m mount -a "src=/dev/sr0 path=/mnt state=unmounted"


14. archive 模块
打包压缩
ansible-doc -s archive

常用的参数:
path: 必须参数,远程主机上需要被打包压缩的源文件/目录
dest: 打包压缩后的包文件路径(包文件的父目录必须存在);如果包文件已存在,则会被覆盖
format: 指定压缩类型,包括: bz2、gz(默认)、tar、xz、zip
remove=yes|no: 是否删除源文件
ansible dbservers -m archive -a "path=/etc/yum.repos.d/ dest=/opt/repo.zip format=zip"


remove:


15. unarchive 模块
解包解压缩
ansible-doc -s unarchive

'常用的参数:
copy:默认为 copy=yes ,拷贝的文件从 ansible 主机复制到远程主机,copy=no 表示在远程主机上寻找源文件解压
src:tar包源路径,可以是 ansible 主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需设置 copy=no
dest:解压后文件的目标绝对路径
remote_src: 和 copy 功能一样且互斥,设置 remote_src=yes 表示文件在远程主机上,设置为 remote_src=no 表示文件在 ansible 主机上
ansible dbservers -m unarchive -a "src=/opt/repo.zip dest=/tmp/ copy=no"


16. replace 模块
类似于sed命令,主要也是基于正则进行匹配和替换
ansible-doc -s replace

常用的参数:
path:必须参数,指定要修改的文件
regexp:必须参数,指定一个正则表达式
replace:替换regexp参数匹配到的字符串
backup=yes|no: 修改源文件前创建一个包含时间戳信息的备份文件
before:如果指定,则仅替换/删除此匹配之前的内容,可以和after参数结合使用
after:如果指定,则仅替换/删除此匹配之后的内容,可以和before参数结合使用
owner:修改文件用户名
group:修改文件组名
mode:修改文件权限
vim /opt/a.txt


匹配 333 并修改为 ccc:
ansible dbservers -m replace -a "path=a.txt regexp='33' replace='cc'"


匹配到任意一个或多个开头的行增加注释:
ansible dbservers -m replace -a "path=/opt/a.txt regexp='^(.*)' replace='#\1'"


取消注释:
ansible dbservers -m replace -a "path=/opt/a.txt regexp='^#(.*)' replace='\1'"


只给a开头的行加#号:
ansible dbservers -m replace -a "path=/opt/a.txt regexp='^(a.*)' replace='#\1'

修改2b中的2为0:
ansible dbservers -m replace -a "path=/opt/a.txt regexp='2' replace='0' after=1a"


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

ansible webservers -m setup #获取mysql组主机的facts信息

ansible dbservers -m setup -a 'filter=*ipv4' #使用filter可以筛选指定的facts信息

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


开启40主机,并做ssh免密登录:

ansible dbservers -m command -a 'date'

修改40主机的ssh端口号:



测试:

修改配置文件:



inventory 中的变量
Inventory变量名 含义
ansible_host ansible连接节点时的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 提升为指定用户权限时的密码
设置主机变量:



嵌套组:




