目录
1)SSH实现基于秘钥连接的部署步骤
(5)file – Sets attributes of files
一、 ansible批量管理服务介绍
1)ansible批量管理服务意义
01. 提高工作的效率
02. 提高工作准确度
03. 减少维护的成本
04. 减少重复性工作
2)ansible批量管理服务功能
01. 可以实现批量系统操作配置
02. 可以实现批量软件服务部署
03. 可以实现批量文件数据分发
04. 可以实现批量系统信息收集
二、 ansible批量管理服务部署
1)SSH实现基于秘钥连接的部署步骤
服务器 | 服务类型 | IP |
m01 | 管理端 | 172.16.1.61 |
nfs01 | 客户端 | 172.16.1.31 |
backup | 客户端 | 172.16.1.41 |
web01 | 客户端 | 172.16.1.7 |
第一个历程: 管理端创建秘钥对信息
[root@m01 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
第二个历程: 管理端需要将公钥进行分发
yum install -y sshpass
[root@m01 /server/scripts]# vim fenfa_pub_key.sh
#!/bin/bash
for ip in 7 31 41
do
echo "============ host 172.16.1.$ip pub-key start fenfa============="
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.$ip "-o StrictHostKeyChecking=no" &>/dev/null
echo "host 172.16.1.$ip fenfa success."
echo "============ host 172.16.1.$ip fenfa end============="
done
[root@m01 /server/scripts]# sh fenfa_pub_key.sh
第三个历程: 进行远程连接测试
[root@m01 /server/scripts]# vim check_pub_key.sh
#!/bin/bash
CMD=$1 #--传参(执行脚本的时候后面可以写一些命令,不用去调整脚本)
for ip in {7,31,41}
do
echo "==================== host 172.16.1.$ip check ==================== "
ssh 172.16.1.$ip $CMD
echo ""
done
[root@m01 /server/scripts]# sh check_pub_key.sh "hostname"
==================== host 172.16.1.31 check ====================
nfs01
==================== host 172.16.1.41 check ====================
backup
==================== host 172.16.1.7 check ====================
web01
2)管理端服务器
第一个历程: 安装部署软件
[root@m01 ~]# yum install -y ansible --- 需要依赖epel的yum源
/etc/ansible/ansible.cfg --- ansible服务配置文件
/etc/ansible/hosts --- 主机清单文件 定义可以管理的主机信息
/etc/ansible/roles --- 角色目录???
第二个历程: 需要编写主机清单文件
[root@m01 ~]# vim /etc/ansible/hosts
#定义可以管理的主机信息
172.16.1.7
172.16.1.31
172.16.1.41
第三个历程: 测试是否可以管理多个主机
脚本 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
三、 ansible服务架构信息
1) 主机清单配置
2) 软件模块信息
3) 基于秘钥连接主机
4) 主机需要关闭selinux --不然ansible连接其他主机会报错
5) 软件剧本功能
四、 ansible软件模块应用
ansible官方网站: https://docs.ansible.com/
模块的应用语法格式:
ansible 主机名称/主机组名称/主机地址信息/all -m(指定应用的模块信息) 模块名称 -a(指定动作信息) "执行什么动作"
(1)命令类型模块:
掌握第一个模块: 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.
在执行命令之前对目录进行切换
[root@m01 ~]# ansible 172.16.1.31 -m command -a "chdir=/tmp touch 123.txt"
2) creates If it already exists, this step won't be run.
如果文件存在了,不执行命令操作
[root@m01 ~]# ansible 172.16.1.31 -m command -a "creates=/tmp/hosts touch 123.txt"
3) removes If it already exists, this step will be run.
如果文件存在了, 这个步骤将执行
[root@m01 ~]# ansible 172.16.1.31 -m command -a "removes=/tmp/hosts chdir=/tmp touch 123.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 "&"
(2)掌握第二个模块: shell (万能模块)
shell – Execute commands in nodes
在节点上执行操作
简单用法:
[root@m01 scripts]# ansible 172.16.1.31 -m shell -a "hostname"
172.16.1.31 | CHANGED | rc=0 >>
nfs01
扩展应用:
1) chdir Change into this directory before running the command.
在执行命令之前对目录进行切换
[root@m01 ~]# ansible 172.16.1.31 -m shell -a "chdir=/tmp touch 123.txt"
2) creates If it already exists, this step won't be run.
如果文件存在了,不执行命令操作
[root@m01 ~]# ansible 172.16.1.31 -m shell -a "creates=/tmp/hosts touch 123.txt"
3) removes If it already exists, this step will be run.
如果文件存在了, 这个步骤将执行
[root@m01 ~]# ansible 172.16.1.31 -m shell -a "removes=/tmp/hosts chdir=/tmp touch 123.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执行脚本
第一个步骤: 编写一个脚本
[root@m01 /server/scripts]# vim yum.sh
#!/bin/bash
##yum
yum install -y htop
第二个步骤: 将脚本发送到远程主机
[root@m01 /server/scripts]# scp -rp ./yum.sh 172.16.1.41:/server/scripts
yum.sh 100% 39 37.0KB/s 00:00
第三个步骤: 将脚本权限进行修改(添加执行权限)
[root@m01 ~]# ansible 172.16.1.41 -m file -a "dest=/server/scripts/yum.sh mode=777"
第四个步骤: 运行ansible命令执行脚本
[root@m01 ~]# ansible 172.16.1.41 -m shell -a "chdir=/server/scripts sh yum.sh"
(3)掌握第三个模块: script (万能模块)
第一个步骤: 编写一个脚本
第二个步骤: 运行ansible命令执行脚本
PS: scripts模块参数功能和command模块类似
(4)文件类型模块:
copy – Copies files to remote locations
将数据信息进行批量分发
基本用法:
[root@m01 ~]# 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. 在传输文件时修改文件的属主和属组信息
[root@m01 ~]# ansible 172.16.1.41 -m copy -a "src=/data dest=/backup owner=www group=www"
02. 在传输文件时修改文件的权限信息
[root@m01 ~]# ansible 172.16.1.41 -m copy -a "src=/data dest=/backup mode=1777"
03. 在传输数据文件信息时对远程主机源文件进行备份
[root@m01 ~]# ansible 172.16.1.41 -m copy -a "src=/data dest=/backup backup=yes"
04. 创建一个文件并直接编辑文件的信息
[root@m01 ~]# ansible 172.16.1.41 -m copy -a "content='123456' dest=/123.txt"
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模块复制目录信息
[root@m01 ~]# ansible 172.16.1.41 -m copy -a "src=/data dest=/backup"
src后面目录没有/: 将目录本身以及目录下面的内容都进行远程传输复制
[root@m01 ~]# ansible 172.16.1.41 -m copy -a "src=/data/ dest=/backup"
src后面目录有/: 只将目录下面的内容都进行远程传输复制
(5)file – Sets attributes of files
设置文件属性信息
基本用法:
[root@m01 ~]# ansible 172.16.1.41 -m file -a "dest=/etc/hosts owner=www group=www mode=666"
扩展用法:
1. 可以利用模块创建数据信息 (文件 目录 链接文件)
state 参数
=absent --- 缺席/删除数据信息
=directory --- 创建一个目录信息
=file --- 检查创建的数据信息是否存在 绿色存在 红色不存在
=hard --- 创建一个硬链接文件
=link --- 创建一个软链接文件
=touch --- 创建一个文件信息
创建目录信息:
[root@m01 ~]# ansible 172.16.1.31 -m file -a "dest=/data/ state=directory"
[root@m01 ~]# ansible 172.16.1.31 -m file -a "dest=/data/data01/data02/ state=directory"
创建文件信息:
[root@m01 ~]# ansible 172.16.1.41 -m file -a "dest=/data/123.txt state=touch"
创建链接文件信息:
此src为客户端目录
[root@m01 /data]# ansible 172.16.1.41 -m file -a "src=/backup/123.txt dest=/backup/123_hard.txt state=hard"
[root@m01 /data]# ansible 172.16.1.41 -m file -a "src=/backup/123.txt dest=/backup/123_link.txt state=link"
2. 可以利用模块删除数据信息
[root@m01 ~]# ansible 172.16.1.41 -m file -a "dest=/backup/123.txt state=absent" [root@m01 ~]# ansible 172.16.1.41 -m file -a "dest=/backup/ state=absent"
3.递归地对目录内容设置指定的文件属性。仅当状态设置为“目录”时才适用。
recurse=yes/no
[root@m01 ~]# ansible 172.16.1.41 -m file -a "dest=/backup owner=rsync recurse=yes"