网络知识 娱乐 大型企业中如何批量管理千万台服务器之ansible自动化运维工具详解 [⭐建议收藏⭐]

大型企业中如何批量管理千万台服务器之ansible自动化运维工具详解 [⭐建议收藏⭐]

文章目录

  • ansible 自动化运维工具 详解
    • 关于作者
    • 作者介绍
    • 一、ansible 概述
      • 1.1 ansible 概述
      • 1.2 是什么要使用 ansible
      • 1.3 ansible 功能
      • 1.4 ansible 优点
      • 1.5 ansible工作机制
      • 1.6 ansible 工作原理/流程图
    • 二、ansible 批量管理服务部署
      • 2.1 实验环境
      • 2.2 官网安装教程
        • 2.2.1 ansible 安装
        • 2.2.2 ansible 命令参数介绍
        • 2.2.3 定义主机清单
        • 2.2.4 基于ssh 免密登录
        • 2.2.5 管理端 运行命令测试
    • 三、ansible 批量管理服务 应用
      • 3.1 ansible ping 模块实战
        • 3.1.0 ping模块参数介绍
        • 3.1.1 ansible 实现自己管理自己
        • 3.1.2 ansible 匹配一个网段机器进行管理
      • 3.2 ansible shell 模块实战
        • 3.2.0 shell模块介绍
        • 3.2.1通过ansible 在docker 服务器上创建/查看目录
        • 3.2.2 通过ansible shell 模块远程查看nginx 服务是否启动
        • 3.2.3 通过ansible shell 模块远程查看防火墙状态
        • 3.2.4 通过ansible 批量修改 用户密码
      • 3.3 ansible command 模块实战
        • 3.3.0 command 模块介绍
        • 3.3.1 通过ansible 使用df -h 查看磁盘空间使用信息
        • 3.3.2 ansible 远程查看 系统时间
        • 3.3.3 Ansible 远程执行下载http安装包
      • 3.4 ansible copy 模块实战
        • 3.4.0 copy 模块介绍
        • 3.4.1 将 ansible 服务器上的 httpd安装包远程考到ip为10.0.0.5 服务器上
      • 3.5 ansible yum 模块实战
        • 3.5.0 yum 模块介绍
        • 3.5.1 ansible 服务器上 远程在 10.0.0.6 服务器上安装/启动/卸载 httpd 服务
        • 3.5.2 将www 组服务器中的所有yum 进程都杀掉
      • 3.6 ansible file 模块实战(创建、删除、修改、权限、属性的维护和管理)
        • 3.6.0 ansible file 模块介绍
        • 3.6.1 在 docker 服务器上 /usr/local 目录下面创建一个rivers的目录
        • 3.6.2 在docker 服务器上远程创建一 脚本文件
      • 3.7 ansible cron 模块实战
        • 3.7.0 cron 模块介绍
        • 3.7.1 利用cron 模块创建/删除计划任务---时间同步(阿里云)
        • 3.7.2 给定时任务设置注释信息
      • 3.8 ansible user 模块实战
        • 3.8.0 模块介绍
        • 3.8.1 给docker 服务器创建一个用户名称 dock普通管理用户
        • 3.8.2 给web 服务器上的apache 服务创建/删除一个apache用户
      • 3.9 Ansible service模块实战
        • 3.9.0 service 模块介绍
        • 3.9.1 远程重启web服务器上的防火墙 服务
        • 3.9.2 远程重启eht0 网卡(平滑启动)
        • 3.9.3 远程设置nfs 开启自启动,启动级别3,5级别
      • 3.10 ansible synchronize 实战模块
        • 3.10.0 synchronize 模块介绍
        • 3.10.1 将源目录同步至目标目录(增量同步)
        • 3.10.2 将源目录同步至目标目录(完全同步)
    • 四、ansible 批量管理服务
      • 4.1 ansible 剧本 介绍
        • 4.1.0 ansible playbook剧本介绍和基础使用说明
        • 4.1.1 playbook 常见的组件及作用
        • 4.1.2 远程主机安装httpd-2.4.46.tar.bz2 web服务案例演示
      • 4.2 playbook 企业案列实战
        • 4.2.0 使用 ansible playbook 安装 nginx WEB服务,安装完并启动Nginx 实战
        • 4.2.1 使用ansible playbook 批量创建系统用户
    • 五、ansible 综合应用实战 ---部署lnmp架构
      • 5.1 实验环境介绍
      • 5.1.1 使用ansible playbook 搭建一键部署lnmp架构
        • 5.1.2 整合 lnmp 架构、进行测试
    • 六、ansible 调优
      • 6.1 ansible 配置文件详解
      • 6.2 ansible 常见优化介绍
        • 6.1.1 关闭ssh 密钥检测
        • 6.1.2 openssh 连接优化
        • 6.1.3 加速 ansible 优化
  • 总结

ansible 自动化运维工具 详解

关于作者

  • 作者介绍


    🍊 博客主页:作者主页
    🍊 简介:云计算领域优质创作者🏆、学长的it故事 公众号创作者🎓、在校期间参与众多计算机相关的省赛、国赛,斩获系列荣誉。考取华为资深工程师、红帽工程师等系列认证。

    🍊 关注我:关注我简历模板、学习资料、文档下载、技术支持
    让生命像一团热烈燃烧的火,直到死亡才能使它熄灭


前言

​ 最近有老铁私信给我,让我出一份ansible的博文,今天小编就和带着学习新知识的思想,和大家一起复习自动化运维工具。在早起的自动化运维工具中,常见的有puppet、saltstack、ansible 等软件,目前常用的就是ansible 和saltstack 2款工具。saltstack 与puppet 都是基于 C/S 模式工作的,他们需要安装服务端与客户端,基于Python 编写,加入MQ消息同步,可以是执行命令和执行结果高效返回,但执行过程需要等客户端返回。所以如果客户端没有及时返回或响应,就可能导致部分机器没有执行结果。目前用的较少。

今天就主要分享ansible 自动化运维工具。我将详细的从概念、到基础搞高级,层层递进,让你轻松掌握ansible自动化运维

当然,每一篇文章都不是100%的全,我能做的就是将我会的,最大限度的分享给你,让你也可以掌握。

一、ansible 概述

1.1 ansible 概述

  • Ansible是一款自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

  • Ansible 是一种安装在控制节点上的无代理自动化工具。Ansible 从控制节点远程管理机器和其他设备(默认情况下,通过 SSH 协议)。Ansible 与Saltstack均是基于Python 语言开发,Ansible只需要在一台普通的服务器上运行即可,不需要在客户端服务器上安装客户端。因为 Ansible是基于SSH远程管理,而Linux 服务器大都离不开SSH,所以Ansible不需要为配置工作添加额外的支持。

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

    • (1) 连接插件connection plugins:负责和被监控端实现通信;
      (2) host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
      (3) 各种模块核心模块、command模块、自定义模块;
      (4) 借助于插件完成记录日志邮件等功能;
      (5) playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务
  • 架构图

  • 在这里插入图片描述

  • ansible 官网

1.2 是什么要使用 ansible

  • 提高工作的效率
  • 提高工作准确度
  • 减少维护的成本
  • 减少重复性工作

1.3 ansible 功能

  • 可以实现批量系统操作配置
  • 可以实现批量软件服务部署
  • 可以实现批量文件数据分发
  • 可以实现批量系统信息收集

1.4 ansible 优点

  • 管理端不需要启动服务程序(no server)
  • 管理端不需要编写配置文件(/etc/ansible/ansible.cfg)
  • 受控端不需要安装软件程序(libselinux-python)
  • 受控端不需要启动服务程序(no agent)
  • 服务程序管理操作模块众多(module)
  • 利用剧本编写来实现自动化(playbook)
  • 支持sudo 普通用户

1.5 ansible工作机制

  • 在这里插入图片描述

    # 由上面的图可以看到 Ansible 的组成由 5 个部分组成:
    
    #Ansible :    ansible核心
    #Modules :    包括 Ansible 自带的核心模块及自定义模块
    #Plugins :    完成模块功能的补充,包括连接插件、邮件插件等
    #Playbooks :  剧本;定义 Ansible 多任务配置文件,由Ansible 自动执行
    #Inventory :  定义 Ansible 管理主机的清单  [ˈɪnvəntri] 清单
    
    

1.6 ansible 工作原理/流程图

  • 在这里插入图片描述

二、ansible 批量管理服务部署

2.1 实验环境

  • 实验环境

    名称系统ip地址
    ansible_serverCentos 7.610.0.0.61
    Host-01Centos 7.610.0.0.5
    Host-02Centos 7.610.0.0.6
    Host-03Centos 7.610.0.0.7
  • 实验拓扑

  • 在这里插入图片描述

2.2 官网安装教程

2.2.1 ansible 安装

  • 官网安装步骤

    # Centso 需要设置EPEL仓库
    [root@ansible_server~]# yum install epel-release
    
    # 使用yum 安装ansible 
    [root@ansible_server~]# yum install ansible
    
    # 查看ansible 安装版本
    [root@ansible_server~]# ansible --version
    ansible 2.9.25
      config file = /etc/ansible/ansible.cfg
      configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
      ansible python module location = /usr/lib/python2.7/site-packages/ansible
      executable location = /usr/bin/ansible
      python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
    
    

2.2.2 ansible 命令参数介绍

  • ansible 常见参数

    # ansible 命令参数
    
    # anisble命令语法: ansible [-i 主机文件] [-f 批次] [组名] [-m 模块名称] [-a 模块参数]
    ansible详细参数:
     -v,-verbose            # 详细模式,如果命令执行成功,输出详细的结果 (-vv –vvv -vvvv)
     -i PATH, -inventory=PATH  #  指定 host 文件的路径,默认是在 /etc/ansible/hosts 
    inventory  [ˈɪnvəntri]  库存
     -f NUM,-forks=NUM      # NUM 是指定一个整数,默认是 5 ,指定 fork 开启同步进程的个数。
     -m NAME,-module-name=NAME    #   指定使用的 module 名称,默认使用 command模块
     -a,MODULE_ARGS         #指定 module 模块的参数
     -k,-ask-pass           #提示输入 ssh 的密码,而不是使用基于 ssh 的密钥认证
     -sudo                  # 指定使用 sudo 获得 root 权限
     -K,-ask-sudo-pass      #提示输入 sudo 密码,与 -sudo 一起使用
     -u USERNAME,-user=USERNAME          # 指定移动端的执行用户
     -C,-check             #测试此命令执行会改变什么内容,不会真正的去执行
    
    # ansible-doc详细参数:
    	ansible-doc -l          #列出所有的模块列表
    	ansible-doc -s 模块名    #查看指定模块的参数  -s,   snippet  [ˈsnɪpɪt]   片断
     
     [root@ansible_server~]#  ansible-doc -s service
     
    #列出模块使用简介
     [root@ansible_server~]# ansible-doc -l   
     
    # 指定一个模块详细说明
     [root@ansible_server~]# ansible-doc -s fetch   
     
     # 查询模块在剧本中应用方法
     [root@ansible_server~]# ansible-doc fetch      
    
    
  • Ansible基于多模块管理,常用的Ansible工具管理模块包括: commandshellscriptyumcopyFileasyncdockercronmysql_userpingsysctluseracladd_hosteasy_installhaproxy_等。

  • command 不支持管道符,可以使用shell模块

  • 可以使用ansible-doc -l more查看ansible支持的模块,也可以查看每个模块的帮助文档,ansible-doc module name

  • 在这里插入图片描述

2.2.3 定义主机清单

  • 定义主机清单

    [root@ansible_server~]# cd /etc/ansible/
    [root@ansible_server/etc/ansible]# ll
    total 24
    -rw-r--r-- 1 root root 19985 Aug 22 04:07 ansible.cfg
    -rw-r--r-- 1 root root  1016 Aug 22 04:07 hosts
    drwxr-xr-x 2 root root     6 Aug 22 04:07 roles
    
    # 操作配置文件前,习惯先备份 
    [root@ansible_server/etc/ansible]# cp -f hosts hosts.backup
    [root@ansible_server/etc/ansible]# ll
    total 28
    -rw-r--r-- 1 root root 19985 Aug 22 04:07 ansible.cfg
    -rw-r--r-- 1 root root  1016 Aug 22 04:07 hosts
    -rw-r--r-- 1 root root  1016 Sep 14 15:34 hosts.backup
    drwxr-xr-x 2 root root     6 Aug 22 04:07 roles
    
    # 情况hosts 配置文件内容,写hosts文件
    [root@ansible_server/etc/ansible]# cat /dev/null > hosts
    [root@ansible_server/etc/ansible]# cat hosts
    
    # 写入主机IP ,这些写了[webserver]、[docker] 等都是分组,可以不写(这样不方便管理)
    [root@ansible_server/etc/ansible]# cat hosts
    [webserver]
    10.0.0.5
    10.0.0.6
    
    # 管理10.0.0.7 docker服务器
    [docker]
    10.0.0.7
    
    # 本机也可以管理自己管自己
    [manager]
    10.0.0.61
    [root@ansible_server/etc/ansible]# 
    

2.2.4 基于ssh 免密登录

  • ssh 免密登录

    # 第一个历程: 创建秘钥对(管理端服务器)	
    	sh-keygen -t 秘钥的类型(dsa|rsa)
    #第二个历程: 将公钥进行分发(被管理端服务器)
        ssh-copy-id -i /root/.ssh/id_dsa.pub root@10.0.0.5
        
    # 本地生成rsa 密钥对
    [root@ansible_server~]# ssh-keygen 
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):    # 直接回车
    Enter passphrase (empty for no passphrase):                 # 直接回车
    Enter same passphrase again:                                # 直接回车
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:HhsjmGY6DJoSREojVpJmSI63vuoXKy6sK2ESh/eQJr0 root@ansible_server
    The key's randomart image is:
    +---[RSA 2048]----+
    |+Bo.             |
    |X+o              |
    |==..             |
    |=.B. o           |
    |o*.+= . S        |
    |+BE=.  o =       |
    |B.= o   o        |
    |+o =             |
    |X=+              |
    +----[SHA256]-----+
    
    # 上传到被管理端
    [root@web01_server~]# ssh-copy-id root@10.0.0.5
    [root@web01_server~]# ssh-copy-id root@10.0.0.6
    [root@web01_server~]# ssh-copy-id root@10.0.0.7
    
    

2.2.5 管理端 运行命令测试

  • ping模块检查网络连通性

    # 常用参数
     -i    #  指定 host 文件的路径,默认是在 /etc/ansible/hosts ,用的时候可以省略
     -m    # 指定使用的ping模块
    
    [root@ansible_server~]# ansible -i /etc/ansible/hosts 'www' -m ping
    10.0.0.5 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"
    }
    10.0.0.6 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"
    }
    [root@ansible_server~]# 
    
    
  • 在这里插入图片描述


三、ansible 批量管理服务 应用

3.1 ansible ping 模块实战

3.1.0 ping模块参数介绍

  • 模块介绍

    Ansible最基础的模块为ping模块,主要用于判断远程客户端是否在线,用于ping本身服务器,返回值为changed、ping。
    

3.1.1 ansible 实现自己管理自己

  • 自己管理自己

    # ansible基于ssh连接-i (inventory)参数后指定的远程主机时,也可以写端口,用户,密码。
    格式:ansible_ssh_port:指定ssh端口   ansible_ssh_user:指定 ssh 用户 ansible_ssh_pass:指定 ssh 用户登录是认证密码(明文密码不安全)  ansible_sudo_pass:指明 sudo 时候的密码
    
    [root@ansible_server~]# cat /etc/ansible/hosts
    [www]
    10.0.0.5
    10.0.0.6
    
    [docker]
    10.0.0.7
    
    [manager]
    10.0.0.61  ansible_ssh_port=22  ansible_ssh_user=root  ansible_ssh_pass=123456
    [root@ansible_server~]# 
    
    
    # 同时还可以基于SSH 免密登录,2种方式选一种。
    # 将自己的密钥拷贝到本地,然后ssh 连接一下即可。
    
    [root@web01_server~]# ssh-copy-id root@10.0.0.61
    

3.1.2 ansible 匹配一个网段机器进行管理

  • ping 网段的连通性

    # ping 某一个网段
    [root@ansible_server~]# ansible 10.0.0.* -m ping
    
    # 路径是可以省略的  -i ,因为默认路径就是 /etc/ansible/hosts
    [root@ansible_server~]# ansible -i /etc/ansible/hosts "docker" -m ping
    10.0.0.7 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": false, 
        "ping": "pong"
    }
    [root@ansible_server~]# 
    
    
  • 在这里插入图片描述

3.2 ansible shell 模块实战

3.2.0 shell模块介绍

  • 模块介绍

    # Ansible shell模块主要用于远程客户端上执行各种Shell命令或者运行脚本,远程执行命令通过/bin/sh环境来执行,支持比command更多的指令,Shell模块使用详解:
    
    # 常用参数
    chdir					执行命令前,切换到目录;
    creates					当该文件存在时,则不执行该步骤;
    executable				换用shell环境执行命令;
    free_form				需要执行的脚本;
    removes					当该文件不存在时,则不执行该步骤;
    warn					如果在ansible.cfg中存在告警,如果设定了False,不会警告此行。
    
    

3.2.1通过ansible 在docker 服务器上创建/查看目录

  • 创建/查看目录

    # 在docker服务器上 ,在/opt 目录下创建一个名字为rivers+当前时间的目录
    [root@ansible_server~]# ansible docker -m shell -a "mkdir /opt/rivers`date +%F`;ls /opt"
    [WARNING]: Consider using the file module with state=directory rather than
    running 'mkdir'.  If you need to use command because file is insufficient you
    can add 'warn: false' to this command task or set 'command_warnings=False' in
    ansible.cfg to get rid of this message.
    10.0.0.7 | CHANGED | rc=0 >>
    rivers2021-09-16
    
    
    # 通过ls 查看是否创建
    [root@ansible_server~]# ansible docker -m shell -a "ls /opt"
    10.0.0.7 | CHANGED | rc=0 >>
    rivers2021-09-16
    [root@ansible_server~]# 
    
  • 在这里插入图片描述

3.2.2 通过ansible shell 模块远程查看nginx 服务是否启动

  • 远程查看nginx 服务状态

    # 远程查看docker服务器上nginx 进程信息
    [root@ansible_server~]# ansible docker -m shell -a "ps -ef|grep nginx"
    10.0.0.7 | CHANGED | rc=0 >>
    root      11711      1  0 15:25 ?        00:00:00 nginx: master process nginx
    nginx     11712  11711  0 15:25 ?        00:00:00 nginx: worker process
    root      11775  11770  3 15:26 pts/0    00:00:00 /bin/sh -c ps -ef|grep nginx
    root      11777  11775  0 15:26 pts/0    00:00:00 grep nginx
    [root@ansible_server~]# 
    
    
  • 在这里插入图片描述

3.2.3 通过ansible shell 模块远程查看防火墙状态

  • 远程查看docker 服务器上的防火墙状态

    # ansible 服务器上远程查看 dockerf 服务器行的防火墙状态是否运行
    
    [root@ansible_server~]# ansible docker -m shell -a "firewall-cmd --state"
    10.0.0.7 | CHANGED | rc=0 >>
    running
    [root@ansible_server~]# 
    
    
  • 在这里插入图片描述

3.2.4 通过ansible 批量修改 用户密码

  • 批量修改用户密码

    # 通通过shell 远程批量修改密码
    [root@ansible_server~]# ansible www -m shell -a "echo 123456|passwd --stdin root"
    10.0.0.5 | CHANGED | rc=0 >>
    Changing password for user root.
    passwd: all authentication tokens updated successfully.
    10.0.0.6 | CHANGED | rc=0 >>
    Changing password for user root.
    passwd: all authentication tokens updated successfully.
    
  • 在这里插入图片描述

3.3 ansible command 模块实战

3.3.0 command 模块介绍

  • command 模块介绍

    # Ansible command模块为ansible默认模块,主要用于执行Linux基础命令,可以执行远程服务器命令执行、任务执行等操作。Command模块使用详解:
    
    Chdir					执行命令前,切换到目录;
    Creates					当该文件存在时,则不执行该步骤;
    Executable				换用shell环境执行命令;
    Free_form				需要执行的脚本;
    Removes					当该文件不存在时,则不执行该步骤;
    Warn					如果在ansible.cfg中存在告警,如果设定了False,不会警告此行。
    
    

3.3.1 通过ansible 使用df -h 查看磁盘空间使用信息

  • 查看磁盘空间

    # 使用df -h 查看 docker组中机器的磁盘使用情况
    [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.7 -m command -a "df -h"
    10.0.0.7 | CHANGED | rc=0 >>
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        99G  1.7G   97G   2% /
    devtmpfs        442M     0  442M   0% /dev
    tmpfs           453M     0  453M   0% /dev/shm
    tmpfs           453M  7.2M  446M   2% /run
    tmpfs           453M     0  453M   0% /sys/fs/cgroup
    /dev/sda1       197M  105M   93M  54% /boot
    tmpfs            91M     0   91M   0% /run/user/0
    [root@ansible_server~]# 
    
    # 简便方式写(效果一样的)
    [root@ansible_server~]# ansible docker -m command -a "df -h"
    
    
    # 能否使用shell 查看呢 ?
    [root@ansible_server~]# ansible docker -m shell -a "df -h"
    
    # 答案:可以查看,shell 本质上和command 区别不大,知识支持的命令更多
    
  • 在这里插入图片描述

3.3.2 ansible 远程查看 系统时间

  • 使用date 命令查看时间

    # 查看docker 服务器上的时间
    [root@ansible_server~]# ansible docker -m command -a "date %F"
    10.0.0.7 | FAILED | rc=1 >>
    date: invalid date ‘%F’non-zero return code
    [root@ansible_server~]# 
    
    
  • 在这里插入图片描述

3.3.3 Ansible 远程执行下载http安装包

  • 远程下载

    # 先远程查看docker服务器上有没有httpd安装包
    [root@ansible_server~]# ansible docker -m command -a "ls"
    10.0.0.7 | CHANGED | rc=0 >>
    anaconda-ks.cfg
    
    
    # 在docker 服务器上远程下载httpd-2.4.46.tar.bz2
    [root@ansible_server~]# ansible docker -m command -a "wget https://downloads.apache.org/httpd/httpd-2.4.46.tar.bz2"
    
    
  • 在这里插入图片描述

3.4 ansible copy 模块实战

3.4.0 copy 模块介绍

  • 模块介绍

    # Ansible copy模块主要用于文件或者目录拷贝,支持文件、目录、权限、用户组功能,copy模块使用详解:
    
    # 常用参数
    src		 	 	Ansible端源文件或者目录,空文件夹不拷贝;
    content		 	用来替代src,用于将指定文件的内容,拷贝到远程文件内;
    dest			    客户端目标目录或者文件,需要绝对路径;
    backup			# 拷贝之前,先备份远程节点上的原始文件;backup=yes
    directory_mode	用于拷贝文件夹,新建的文件会被拷贝,而老旧的不会被拷贝;
    follow			支持link文件拷贝;
    force			覆盖远程主机不一致的内容;
    group		 	设定远程主机文件夹的组名;
    mode		 	指定远程主机文件及文件及的权限;
    owner		 	设定远程主机文件夹的用户名,拥有者。
    
    

3.4.1 将 ansible 服务器上的 httpd安装包远程考到ip为10.0.0.5 服务器上

  • 远程拷贝

    #将ansible 服务器上的http 安装包远程 拷贝到 10.0.0.5 服务器上,且权限设置为 755
    [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.5 -m command -a "ls"
    10.0.0.5 | CHANGED | rc=0 >>
    anaconda-ks.cfg
    [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.5 -m copy -a "src=/root/httpd-2.4.46.tar.bz2 dest=/root mode=755"
    10.0.0.5 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "checksum": "1b7cd10ff3a2a07a576d77e34f0204d95fa4aceb", 
        "dest": "/root/httpd-2.4.46.tar.bz2", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "7d661ea5e736dac5e2761d9f49fe8361", 
        "mode": "0755", 
        "owner": "root", 
        "size": 7187805, 
        "src": "/root/.ansible/tmp/ansible-tmp-1631779035.15-12735-92572188724853/source", 
        "state": "file", 
        "uid": 0
    }
    [root@ansible_server~]# 
    
    # 查看10.0.0.5 文件是否拷贝成功
    [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.5 -m command -a "ls"
    10.0.0.5 | CHANGED | rc=0 >>
    anaconda-ks.cfg
    httpd-2.4.46.tar.bz2
    [root@ansible_server~]# 
    
    # 查看同一个www组中的 10.0.0.6 是否也有
    [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.6 -m command -a "ls"
    10.0.0.6 | CHANGED | rc=0 >>
    anaconda-ks.cfg
    [root@ansible_server~]#
    

3.5 ansible yum 模块实战

3.5.0 yum 模块介绍

  • yum 模块介绍

    #Ansible yum 模块主要用于软件的安装、升级、卸载,支持红帽.rpm软件的管理,YUM模块使用详解:
    # 常用参数
    conf_file					    设定远程yum执行时所依赖的yum配置文件
    disable_gpg_check			    安装软件包之前是否坚持gpg  key;
    name						    需要安装的软件名称,支持软件组安装;
    update_cache				    安装软件前更新缓存;
    enablerepo					    指定repo源名称;
    skip_broken      			    跳过异常软件节点;
    state						    #软件包状态,包括:installed、present、latest、absent、removed。
    
    

3.5.1 ansible 服务器上 远程在 10.0.0.6 服务器上安装/启动/卸载 httpd 服务

  • 远程安装/卸载

    # 远程在 10.0.0.6 服务器上安装 httpd 服务
    [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.6 -m yum -a "name=httpd state=installed"
    
    # 远程启动 httpd 服务
    [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.6 -m command -a "systemctl start httpd"
    10.0.0.6 | CHANGED | rc=0 >>
    
    
    # 远程查看http服务进程
    [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.6 -m shell -a "ps -ef|grep httpd"
    10.0.0.6 | CHANGED | rc=0 >>
    root      10924      1  0 16:11 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
    apache    10925  10924  0 16:11 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
    apache    10926  10924  0 16:11 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
    apache    10928  10924  0 16:11 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
    apache    10929  10924  0 16:11 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
    apache    10930  10924  0 16:11 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
    root      11066  11061  0 16:12 pts/0    00:00:00 /bin/sh -c ps -ef|grep httpd
    root      11068  11066  0 16:12 pts/0    00:00:00 grep httpd
    
    
    # 停止 httpd 服务
    [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.6 -m command -a "systemctl stop httpd"
    10.0.0.6 | CHANGED | rc=0 >>
    
    # 远程卸载 httpd
    [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.6 -m yum -a "name=httpd state=removed"
    
    
  • 在这里插入图片描述

3.5.2 将www 组服务器中的所有yum 进程都杀掉

  • 停止yum 进程

    # 停止正在工作的yum进程
    [root@ansible_server~]# ansible www -m shell -a "pkill -yum"
    
    # 查看进程
    [root@ansible_server~]# ansible www -m shell -a "ps -ef|grep yum"
    10.0.0.6 | CHANGED | rc=0 >>
    root      12032  12027  0 09:12 pts/1    00:00:00 /bin/sh -c ps -ef|grep yum
    root      12034  12032  0 09:12 pts/1    00:00:00 grep yum
    10.0.0.5 | CHANGED | rc=0 >>
    root      12260  12255  0 09:12 pts/0    00:00:00 /bin/sh -c ps -ef|grep yum
    root      12262  12260  0 09:12 pts/0    00:00:00 grep yum
    [root@ansible_server~]# 
    
    

3.6 ansible file 模块实战(创建、删除、修改、权限、属性的维护和管理)

3.6.0 ansible file 模块介绍

  • file 模块介绍

    # Ansible file模块主要用于对文件的创建、删除、修改、权限、属性的维护和管理,File模块使用详解:
    
    # 常用参数介绍
    src					   Ansible端源文件或者目录;
    follow				   支持link文件拷贝;
    force				   覆盖远程主机不一致的内容;
    group				   设定远程主机文件夹的组名;
    mode				   指定远程主机文件及文件及的权限;
    owner				   设定远程主机文件夹的用户名;
    path				   目标路径,也可以用dest,name代替;
    state				  #状态包括:file、link、directory、hard、touch、absent;
    attributes			   文件或者目录特殊属性。
    

3.6.1 在 docker 服务器上 /usr/local 目录下面创建一个rivers的目录

  • 使用file模块创建目录

    # 在 docker 服务器上 /usr/local 目录下面创建一个rivers的目录,名字为 rivers,属主为nginx
    [root@ansible_server~]# ansible docker -m file -a "path=/usr/local/rivers state=directory mode=644 owner=nginx"
    10.0.0.7 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "gid": 0, 
        "group": "root", 
        "mode": "0644", 
        "owner": "nginx", 
        "path": "/usr/local/rivers", 
        "size": 6, 
        "state": "directory", 
        "uid": 998
    }
    
    # 查看常见的目录
    [root@ansible_server~]# ansible docker -m command -a "ls /usr/local/rivers"
    10.0.0.7 | FAILED | rc=2 >>
    
    # path表示目录的名称和路径, state=directory表示创建目录
    
  • 在这里插入图片描述

3.6.2 在docker 服务器上远程创建一 脚本文件

  • 远程创建文件

    #在/usr/local/rivers/script.sh文件
    [root@ansible_server~]# ansible docker -m file -a "path=/usr/local/rivers/script.sh state=touch mode=777"
    10.0.0.7 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "dest": "/usr/local/rivers/script.sh", 
        "gid": 0, 
        "group": "root", 
        "mode": "0777", 
        "owner": "root", 
        "size": 0, 
        "state": "file", 
        "uid": 0
    }
    [root@ansible_server~]#
    
    # path表示目录的名称和路径, state=touch 表示创建文件
    
  • 在这里插入图片描述

3.7 ansible cron 模块实战

3.7.0 cron 模块介绍

  • cron 模块介绍

    # Ansible cron模块主要用于添加、删除、更新操作系统Crontab任务计划,Cron模块使用详解:
    
    # 常用参数
    name      					任务计划名称;
    cron_file 					替换客户端该用户的任务计划的文件;
    minute    					分( 0-59 ,* ,*/2 );
    hour      					时( 0-23 ,* ,*/2 );
    day       					日( 1-31 ,* ,*/2 );
    month     					月( 1-12 ,* ,*/2 );
    weekday   					周( 0-6 或 1-7 ,* );
    job       					任何计划执行的命令,state要等于present;
    backup    					是否备份之前的任务计划;
    user      					新建任务计划的用户;
    state     					指定任务计划present、absent。
    
    

3.7.1 利用cron 模块创建/删除计划任务—时间同步(阿里云)

  • 基本同步用法

    # docker 服务器 自动同步 阿里云
    [root@ansible_server~]# ansible 10.0.0.7 -m cron -a "minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'" 
    [DEPRECATION WARNING]: The 'name' parameter will be required in future releases.. This 
    feature will be removed in version 2.12. Deprecation warnings can be disabled by setting
     deprecation_warnings=False in ansible.cfg.
    10.0.0.7 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        }, 
        "changed": true, 
        "envs": [], 
        "jobs"