day33 ansible


02. ansible批量管理服务介绍
ansible批量管理服务意义
01. 提高工作的效率
02. 提高工作准确度
03. 减少维护的成本
04. 减少重复性工作
ansible批量管理服务功能
01. 可以实现批量系统操作配置
02. 可以实现批量软件服务部署
03. 可以实现批量文件数据分发
04. 可以实现批量系统信息收集

03. ansible批量管理服务部署
管理端服务器
第一个历程: 安装部署软件
yum install -y ansible --- 需要依赖epel的yum源
/etc/ansible/ansible.cfg --- ansible服务配置文件
/etc/ansible/hosts --- 主机清单文件 定义可以管理的主机信息
/etc/ansible/roles --- 角色目录???

第二个历程: 需要编写主机清单文件
vim /etc/ansible/hosts

第三个历程: 测试是否可以管理多个主机
脚本 hostname
ansible all -a "hostname"
[root@m01 scripts]# ansible all -a "hostname"
172.16.1.41 | CHANGED | rc=0 >>
backup

172.16.1.7 | CHANGED | rc=0 >>
web01

172.16.1.31 | CHANGED | rc=0 >>
nfs01

04. ansible服务架构信息
1) 主机清单配置
2) 软件模块信息 OK
3) 基于秘钥连接主机 OK
4) 主机需要关闭selinux OK
5) 软件剧本功能

05. ansible软件模块应用
ansible官方网站: https://docs.ansible.com/
模块的应用语法格式:
ansible 主机名称/主机组名称/主机地址信息/all -m(指定应用的模块信息) 模块名称 -a(指定动作信息) "执行什么动作"

命令类型模块:
掌握第一个模块: command (默认模块)
command – Executes a command on a remote node
在一个远程主机上执行一个命令
简单用法:
[root@m01 scripts]# ansible 172.16.1.31 -m command -a "hostname"
172.16.1.31 | CHANGED | rc=0 >>
nfs01

扩展应用:
1) chdir Change into this directory before running the command.
在执行命令之前对目录进行切换
ansible 172.16.1.31 -m command -a "chdir=/tmp touch oldboy.txt"

2) creates If it already exists, this step won't be run.
如果文件存在了,不执行命令操作
ansible 172.16.1.31 -m command -a "creates=/tmp/hosts touch oldboy.txt"

3) removes If it already exists, this step will be run.
如果文件存在了, 这个步骤将执行
ansible 172.16.1.31 -m command -a "removes=/tmp/hosts chdir=/tmp touch oldboy.txt"
4) free_form(required)
The command module takes a free form command to run.
There is no parameter actually named 'free form'. See the examples!
使用command模块的时候,-a参数后面必须写上一个合法linux命令信息

注意事项:
有些符号信息无法识别: <", ">", "|", ";" and "&"

掌握第二个模块: shell (万能模块)
shell – Execute commands in nodes
在节点上执行操作
简单用法:
[root@m01 scripts]# ansible 172.16.1.31 -m command -a "hostname"
172.16.1.31 | CHANGED | rc=0 >>
nfs01

扩展应用:
1) chdir Change into this directory before running the command.
在执行命令之前对目录进行切换
ansible 172.16.1.31 -m command -a "chdir=/tmp touch oldboy.txt"

2) creates If it already exists, this step won't be run.
如果文件存在了,不执行命令操作
ansible 172.16.1.31 -m command -a "creates=/tmp/hosts touch oldboy.txt"

3) removes If it already exists, this step will be run.
如果文件存在了, 这个步骤将执行
ansible 172.16.1.31 -m command -a "removes=/tmp/hosts chdir=/tmp touch oldboy.txt"
4) free_form(required)
The command module takes a free form command to run.
There is no parameter actually named 'free form'. See the examples!
使用command模块的时候,-a参数后面必须写上一个合法linux命令信息

实践应用: 利用shell执行脚本
第一个步骤: 编写一个脚本
第二个步骤: 将脚本发送到远程主机
第三个步骤: 将脚本权限进行修改(添加执行权限)
第四个步骤: 运行ansible命令执行脚本

掌握第三个模块: script (万能模块)
第一个步骤: 编写一个脚本
第二个步骤: 运行ansible命令执行脚本

PS: scripts模块参数功能和command模块类似

文件类型模块:
copy – Copies files to remote locations
将数据信息进行批量分发

基本用法:
ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/etc/"
172.16.1.31 | CHANGED => { --- 对哪台主机进行操作
"changed": true, --- 是否对主机信息进行改变
"checksum": "6ed7f68a1d6b4b36c1418338b2001e421eeba270", --- 生成一个文件校验码==MD5数值
"dest": "/etc/hosts", --- 显示目标路径信息
"gid": 0, --- 显示复制后文件gid信息
"group": "root", --- 显示复制后文件属组信息
"md5sum": "7afd7b74854f0aaab646b3e932f427c0", --- 生成一个文件校验码==MD5数值
"mode": "0644", --- 显示复制后文件权限信息
"owner": "root", --- 显示复制后文件属主信息
"size": 401, --- 显示文件的大小信息
"src": "/root/.ansible/tmp/ansible-tmp-1557804498.23-26487341925325/source",
"state": "file", --- 显示文件的类型信息
"uid": 0 --- 显示复制后文件uid信息
}

补充说明: ansible软件输出颜色说明:
01. 绿色信息: 查看主机信息/对主机未做改动
02. 黄色信息: 对主机数据信息做了修改
03. 红色信息: 命令执行出错了
04. 粉色信息: 忠告信息
05. 蓝色信息: 显示ansible命令执行的过程???

扩展用法:
01. 在传输文件时修改文件的属主和属组信息
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ owner=oldboy group=oldboy"
02. 在传输文件时修改文件的权限信息
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ mode=1777"
03. 在传输数据文件信息时对远程主机源文件进行备份
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ backup=yes"
04. 创建一个文件并直接编辑文件的信息
ansible 172.16.1.31 -m copy -a "content='oldboy123' dest=/etc/rsync.password"

自行研究: remote_src directory_mode local_follow
If no, it will search for src at originating/master machine.
src参数指定文件信息,会在本地管理端服务进行查找
If yes it will go to the remote/target machine for the src. Default is no.
src参数指定文件信息,会从远程主机上进行查找

PS: ansible软件copy模块复制目录信息
ansible 172.16.1.31 -m copy -a "src=/oldboy dest=/oldboy"
src后面目录没有/: 将目录本身以及目录下面的内容都进行远程传输复制
ansible 172.16.1.31 -m copy -a "src=/oldboy/ dest=/oldboy"
src后面目录有/: 只将目录下面的内容都进行远程传输复制

file – Sets attributes of files
设置文件属性信息

基本用法:
ansible 172.16.1.31 -m file -a "dest=/etc/hosts owner=oldboy group=oldboy mode=666"

扩展用法:
1. 可以利用模块创建数据信息 (文件 目录 链接文件)
state 参数
=absent --- 缺席/删除数据信息
=directory --- 创建一个目录信息
=file --- 检查创建的数据信息是否存在 绿色存在 红色不存在
=hard --- 创建一个硬链接文件
=link --- 创建一个软链接文件
=touch --- 创建一个文件信息

创建目录信息:
ansible 172.16.1.31 -m file -a "dest=/oldboy/ state=directory"
ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy01/oldboy02/ state=directory"
创建文件信息:
ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy.txt state=touch"
创建链接文件信息:
ansible 172.16.1.31 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy/oldboy_hard.txt state=hard"
ansible 172.16.1.31 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy/oldboy_link.txt state=link"

2. 可以利用模块删除数据信息
ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy.txt state=absent"
ansible 172.16.1.31 -m file -a "dest=/oldboy/ state=absent"

自行研究: recurse

原文地址:https://www.cnblogs.com/huangfeng666/p/12446829.html