zoukankan      html  css  js  c++  java
  • Ansible

    Ansible

    • 运维自动化应用场景

      • 操作系统预备自动化 (pxe)

      • 配置自动化 (ansible)

      • 监控自动化 (zabbix、Prometheus,ELK)

        • 系统与应用监控 zabbix Prometheus、

        • 日志监控 ELK

      • 代码持续集成与代码持续发布自动化 Git GitHub Jenkins docker

    配置自动化场景


    配置自动化在企业大量服务器存在的情况下,快速实现应用部署,软件配置的利器:

    • 提高配置效率

    • 提高配置的准确性

    • 降低人工参与度

     

    配置自动化产品:

    • ansible :开箱即用 使用ssh协议

    • saltstack:需要agent端配合,配置速度快

    • puppet:老牌配置自动化工具,需要agent端配合

     

     

    Ansible介绍:

    ansible一句话说明:无主无从架构,开箱即用,用完即走

     

    工作原理:

    image-20201015164225578

     

    分为两大块:

    • 主机

    • ansible (模块、主机清单、ssh、playbook)

     

    ansible 安装

    ansible只需要在ansible controller节点(操作机)安装即可,其他节点可以直接通过ssh协议进行连接操作。

    安装步骤:

     

    #第一步安装 epel-release YUM源
    [root@master ~]# yum -y install epel-release
    #第二步安装 ansible
    [root@master ~]# yum -y install ansible

    检验是否安装成功:
    [root@master ~]# rpm -qa | grep ansible
    ansible-2.9.13-1.el7.noarch
    [root@master ~]# an
    anacron               ansible-console-2     ansible-galaxy-2.7   ansible-pull-2.7
    ansible               ansible-console-2.7   ansible-inventory     ansible-test

     

    主机清单:

    作用:ansible controller在对主机进行操作时,仅认主机清单中定义的主机列表,即从主机清单中读取主机列表时,才可以配置。

    • 用于ansible controller配置主机时读取主机列表

    • 实现主机分组

    主机清单存储位置:

     /etc/ansible/hosts
    [root@master ~]# ls /etc/ansible/
    ansible.cfg hosts roles

    主机清单定义方法:

    方法一:

    直接在主机清单文件中写入主机IP或主机名(需要能够解析)

    [root@master ansible]# vim /etc/ansible/hosts
    192.168.1.8
    192.168.1.9

    方法二:

    在主机清单文件中添加主机分组,然后把主机IP或主机名写入分组内即可。(常用)

    [root@master ansible]# vim /etc/ansible/hosts
    [webgroup]
    192.168.1.9

     

    ansible应用案例

    使用ansible ping模块实现测试主机互通性

    场景:准备三台服务器用于开发人员项目上线使用,准备好后需要了解主机之间的连通性是否正确,想到使用ansible模块对主机之间的连通性进行测试。如何实现?

    步骤:

    第1步:实现多主机之间免密登录

    #在ansible controller生成密钥
    [root@master ansible]# ssh-keygen -t rsa -f /root/.ssh/id_rsa -N ''
    #实现密钥同步
    [root@master ansible]# ssh-copy-id 192.168.1.8
    #连接测试
    [root@master ansible]# ssh 192.168.1.8
    Last login: Tue Sep 22 08:50:13 2020 from 192.168.1.116
    [root@node1 ~]# exit
    logout

     

    第二步:定义主机清单

    #主机清单文件存储位置
    /etc/ansible/hosts
    [root@master ]# vim /etc/ansible/hosts
    192.168.1.8

     

    第三步:ping模块使用

    #ansible 命令格式 
    [root@master ~]# ansible 主机清单中IP或分支名称 -m 模块

    #ping模块应用
    [root@master ~]# ansible 192.168.1.8 -m ping
    192.168.1.8 | SUCCESS => {
       "ansible_facts": {
           "discovered_interpreter_python": "/usr/bin/python"
      },
       "changed": false,
       "ping": "pong"
       
    [root@master ~]# ansible webgroup -m ping
    192.168.1.9 | SUCCESS => {
       "ansible_facts": {
           "discovered_interpreter_python": "/usr/bin/python"
      },
       "changed": false,
       "ping": "pong"
    }

     

    使用ansible cron模块实现配置多主机时间同步

    步骤:

    第一步:选择时钟源服务器

    • 国内建议使用阿里时钟源 time1.aliyun.com

    • 国际建议使用微软时钟源 time.windows.com

    [root@master ~]# yum -y install ntpdate
    [root@master ~]# ntpdate time1.aliyun.com
    15 Oct 17:53:08 ntpdate[12620]: step time server 203.107.6.88 offset 2014263.265087 sec

     

    第二步:cron模块应用

    #ansible 命令格式
    ansible 主机清单中IP或分组名 -m 模块 -a 参数

    #cron 模块应用
    [root@master ~]# ansible 192.168.1.8 -m cron -a 'name="test cron1" job="ntpdate time1.aliyun.com" minute=0 hour=*/1' 每小时与时钟源同步一次
    192.168.1.8 | CHANGED => {
       "ansible_facts": {
           "discovered_interpreter_python": "/usr/bin/python"
      },
       "changed": true,
       "envs": [],
       "jobs": [
           "test cron1"
      ]
    }

    #远程结果:
    [root@node1 ~]# crontab -l
    no crontab for root
    [root@node1 ~]# crontab -l
    #Ansible: test cron1
    0 */1 * * * ntpdate time1.aliyun.com

     

    使用ansible copy模块实现多主机配置文件同步

    步骤:

    第一步:准备本地的解析文件

    [root@master ~]# vim /etc/hosts
    192.168.1.7 master
    192.168.1.8 node1
    192.168.1.9 node2

     

    第二步:copy模块应用

    #ansible 命令格式
    ansible 主机清单中IP或分组名 -m 模块 -a '参数'

    #cron 模块应用 src=本地路径 dest=远程路径
    [root@master ~]# ansible 192.168.1.8 -m copy -a "src=/etc/hosts dest=/etc/hosts"
    192.168.1.8 | CHANGED => {
       "ansible_facts": {
           "discovered_interpreter_python": "/usr/bin/python"
      },
       "changed": true,
       "checksum": "6cc798b225d077c62748ac36ed814435fb53ad2a",
       "dest": "/etc/hosts",
       "gid": 0,
       "group": "root",
       "md5sum": "6e74d048f3a5e0351e42637539dfba40",
       "mode": "0644",
       "owner": "root",
       "secontext": "system_u:object_r:net_conf_t:s0",
       "size": 214,
       "src": "/root/.ansible/tmp/ansible-tmp-1602756314.85-12685-91707990957594/source",
       "state": "file",
       "uid": 0
    }

    #远程结果:
    [root@node1 ~]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.1.7 master
    192.168.1.8 node1
    192.168.1.9 node2

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    SAX方式解析XML文件实例
    DOM方式解析XML文件实例
    国外程序员整理的Java资源
    研发十大站点
    UMA
    [转]Hadoop YARN任务提交流程
    JAVA的内存模型
    2014年总结
    Storm因机器断电等,启动supervisor异常
    Python几个算法实现
  • 原文地址:https://www.cnblogs.com/james-23/p/13822111.html
Copyright © 2011-2022 走看看