LINUX运维学习之综合架构篇——Ansible 批量管理服务上

1、什么是ansible

  1. ansible 是一个基于Python开发的自动化运维工具
  2. 其功能实现基于SSH远程连接服务
  3. ansible可以实现批量系统配置、批量软件部署、批量文件拷贝、批量运行命令等功能
  4. ansible官方网站:https://docs.ansible.com/

2、ansible 的特点

  1. 列表项目不需要安装客户端。基于系统自带的sshd服务
  2. 不需要服务端
  3. 需要依靠大量的模块实现批量管理功能
  4. 配置文件/etc/ansible/ansible.cfg

3、ansible 批量管理服务意义

  1. 提高工作效率
  2. 提高工作准确度
  3. 减少维护的成本
  4. 减少重复性工作

4、ansible 批量管理服务功能

  1. 可以实现批量系统操作配置
  2. 可以实现批量软件服务部署
  3. 可以实现批量文件数据分发
  4. 可以实现批量系统信息收集
常用模块 模块说明
command 执行命令模块,ansible命令执行默认模块
shell 执行shell脚本模块
script 把脚本发到客户端,然后执行,执行脚本命令在远端服务器上
copy 把文件发送到远端
file 设定文件属性模块
service 系统服务管理模块
cron 定时任务管理模块
yum yum软件包安装管理模块
synchronize 使用rsync同步文件模块

5、ansible 批量管理服务部署

管理端服务器

1、安装部署软件

    yum install -y ansible

2、需编写主机清单文件

vim /etc/ansible/hosts

添加需要管理的主机的IP地址

定义可以管理的主机的信息

    172.16.1.31 
    172.16.1.41 
    172.16.1.7

3、测试是否可以管理多个主机,由于我没开1.7 的主机会报个错,影响不大

    ansible all -a "hostname"

ansible

6、ansible服务架构信息

1、主机清单配置
2、 软件模块信息
3、 基于秘钥连接主机
4、 主机需要关闭selinux
5、 软件剧本功能
7、ansible 软件模块应用
ansible官方网站:https://docs.ansible.com/
模块的应用语法格式:
ansible 主机名称/主机组名称/主机地址信息/all -m(指定应用的模块信息) 模块名称 -a(指定动作信息) “执行什么动作”
命令类型模块:

command   – Executes a command on a remote node

在一个远程主机上执行一个命令

    ansible 172.16.1.31 -m command -a "hostname"

ansible
拓展应用:
1、chdir Change into this directory before running the command.
在执行命令之前对目录进行切换

      ansible 172.16.1.31 -m coomand -a "chdir=/tmp touch test.txt"

如果不加chdir=/tmp,默认在/root下创建
2、creates If it already exists, this step won’t be run.
若是文件存在了,不执行命令操作

       ansible 172.16.1.31 -m command -a "creates=/tmp/hosts chdir=/tmp touch test.txt"

ansible
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”

ansible
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 “&”
7、第二个模块:shell (万能模块)

shell   – Execute commands in nodes

在节点上执行操作
用法:

    ansible 172.16.1.31 -m shell -a "hostname"

请输入图片描述

    ansible 172.16.1.31 -m shell -a "cd /tmp;pwd"

请输入图片描述
请输入图片描述
实践应用: 利用shell执行脚本
第一个步骤: 编写一个脚本

    vim /server/scripts/yum.sh
    #!/bin/bash
    yum install -y htop

第二个步骤: 将脚本发送到远程主机

    scp -rp yum.sh 172.16.1.31:/server/scripts

请输入图片描述
第三个步骤: 将脚本权限进行修改(添加执行权限)

    ansible 172.16.1.31 -m shell -a "chmod +x /server/scripts/yum.sh"

请输入图片描述
第四个步骤: 运行ansible命令执行脚本

    ansible 172.16.1.31 -m shell -a "/server/scripts/yum.sh"

rpm -e htop --nodeps 卸载软件
8、script (万能模块)
第一个步骤: 编写一个脚本
第二个步骤: 运行ansible命令执行脚本

    ansible 172.16.1.31 -m scripts -a "/server/scripts/yum.sh"

PS: scripts模块参数功能和command模块类似
9、文件类型模块:
copy – Copies files to remote locations
将数据信息进行批量分发
基本用法:

    ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/root"

请输入图片描述

    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软件输出颜色说明:

  1. 绿色信息: 查看主机信息/对主机未做改动
  2. 黄色信息: 对主机数据信息做了修改
  3. 红色信息: 命令执行出错了
  4. 粉色信息: 忠告信息
  5. 蓝色信息: 显示ansible命令执行的过程
    拓展应用:
  6. 在传输文件是修改文件是属主和属组信息
        ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/root owner=oldboy group=oldboy"

请输入图片描述

  1. 在传输文件时修改文件的权限信息
        ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/root/ mode=1777"
  1. 在传输数据文件信息时对远程主机源文件进行备份

    ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/root/ backup=yes"
  2. 创建一个文件并直接编辑文件的信息
        ansible 172.16.1.31 -m copy -a "content='oldboy123' dest=/etc/hosts"

remote_src
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参数指定文件信息,会从远程主机上进行查找
        ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/root"

这个命令会把远程管理端的hosts文件传输到服务端

        ansible 172.16.1.31 -m copy -a "src=/etc/hosts dest=/root remote_src=yes"

这个命令会把远程服务端的hosts文件移动到/root目录
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后面目录有/: 只将目录下面的内容都进行远程传输复制
10、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"
© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    请登录后查看评论内容