zoukankan      html  css  js  c++  java
  • Linux 期中架构 Ansible

    ansible  自动化软件   基于Python开发

     

    特点概述:

     

    配置文件不需要过多配置  了解就可以了

    ###部署ansble软件

    ##受控主机部署 backup   nfs01   web01

    yum install -y libselinux-python

    ##管理主机部署m01 

           yum -y install ansible

    至此  ansible软件部署完毕

    ###软件配置应用

    架构:

     

     

    安装前要实现公钥批量管理  默认已经部署好了

     

    查看软件版本:

    [root@m01 ~]# ansible --version

    ansible 2.5.2

      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.6/site-packages/ansible

      executable location = /usr/bin/ansible

      python version = 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]

    ansble目录结构信息:

    /usr/bin/ansible

    /usr/bin/ansible-playbook       ###ansible 剧本信息

    [root@m01 ~]# cd /etc/ansible/

    [root@m01 ansible]# ll

    total 28

    -rw-r--r-- 1 root root 19315 Apr 27 04:22 ansible.cfg      ###配置文件所在位置

    -rw-r--r-- 1 root root  1048 May 20 10:20 hosts                   ###定义ansible可以管理的主机信息

    drwxr-xr-x 2 root root  4096 Apr 27 04:22 roles                   ###主要在自动化部署多台主机时应用

    关于hosts文件说明:需要将被管理的主机添加进去

     

    ###ansible命令语法格式

     

     

    [root@m01 scripts]# ansible oldboy -m command -a "date"

    172.16.1.8 | SUCCESS | rc=0 >>

    Mon Jun  4 16:40:51 CST 2018

    172.16.1.41 | SUCCESS | rc=0 >>

    Mon Jun  4 16:40:52 CST 2018

    172.16.1.31 | SUCCESS | rc=0 >>

    Mon Jun  4 16:40:52 CST 2018

    ansible官方说明

    说明信息:

    ansible软件相关参考链接信息

    http://docs.ansible.com/ansible/intro_installation.html

    http://www.ansible.com.cn/

    http://docs.ansible.com/modules_by_category.html

    http://www.ansible.cn/docs/

    ansible常用参数

     

    #1说明 command模块作为默认模块  在不指定时即是

    [root@m01 scripts]# ansible oldboy -a "date"

    172.16.1.31 | SUCCESS | rc=0 >>

    Mon Jun  4 16:59:02 CST 2018

    172.16.1.8 | SUCCESS | rc=0 >>

    Mon Jun  4 16:59:02 CST 2018

    172.16.1.41 | SUCCESS | rc=0 >>

    Mon Jun  4 16:59:02 CST 2018

    #2说明  测试所有主机的连通性模块

    [root@m01 scripts]# ansible oldboy -m ping

    172.16.1.41 | SUCCESS => {

        "changed": false,

        "ping": "pong"                                   ###表示成功连接

    }

    172.16.1.31 | SUCCESS => {

        "changed": false,

        "ping": "pong"

    }

    172.16.1.8 | SUCCESS => {

        "changed": false,

        "ping": "pong"

    }

    #3   debug

    [root@m01 scripts]# ansible oldboy -m debug

    172.16.1.31 | SUCCESS => {

        "msg": "Hello world!"

    }

    172.16.1.41 | SUCCESS => {

        "msg": "Hello world!"

    }

    172.16.1.8 | SUCCESS => {

        "msg": "Hello world!"

    }

    [root@m01 scripts]# ansible oldboy -m debug -a "msg=nod"

    172.16.1.31 | SUCCESS => {

        "msg": "nod"

    }

    172.16.1.41 | SUCCESS => {

        "msg": "nod"

    }

    172.16.1.8 | SUCCESS => {

        "msg": "nod"

    }

    #4  复制模块--copy

    [root@m01 scripts]# ansible oldboy -m copy -a "src=/home/history/0604.txt dest=/home/history/"

    查看对端效果:

    文件传输成功

     

    补充案例  :假定我们在做copy操作时,需要回滚,或者误操作

    ;这个时候有一个backup参数就显得很重要(在分发文件前,对已有源文件进行备份)

    [root@m01 scripts]# ansible oldboy -m copy -a "src=/home/history/0604.txt dest=/home/history/ backup=yes"

     

     

    #设置文件的属组信息  权限信息

    [root@m01 scripts]# ansible oldboy -m copy -a "src=/home/history/0604.txt dest=/home/history/ mode=0600 owner=oldboy group=oldboy backup=yes"

     

     

    ###ansible命令输出信息中:

    绿色表示查询或者没有发生任何改变时

    红色表示执行命令操作出现异常

    黄色表示执行命令后发生对受控主机产生了影响,发生了配置改变

    ###ansible基础模块实践 

    #批量执行脚本   shell   (万能模块)

     #第一个里程碑:先在m01撰写脚本

     

    #第二个里程碑:分发脚本到oldboy群组的主机上

    [root@m01 scripts]# ansible oldboy -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts"

    #第三个里程碑:远程执行shell脚本

    [root@m01 scripts]# ansible oldboy -m shell -a "/bin/bash /server/scripts/yum.sh"

     

    备注:对于shell能识别特殊符号  对比command  command不行的操作shell都可以实现

    [root@m01 scripts]# ansible oldboy -m shell -a "cat /etc/hosts>>/home/history/0604v2.txt"

     

    ##删除指定文件    ---file

    ###运行脚本模块   script(不需要分发脚本  第二种方法)远程执行本地脚本中的命令

    [root@m01 scripts]# ansible oldboy -m script -a "/server/scripts/yum.sh"

    对端没有脚本:&但是命令也执行成功了

     

    ###yum模块

     

    ###ansible模块-->file

    [root@m01 scripts]# ansible oldboy -m file -a "src=/server/scripts/yum.sh dest=/server/scripts/yum.sh.link state=link"

     

    ansible帮助信息系统中查看方法:

    ansible-doc -l | wc -l

    ansible-doc -s file

    ###ansible 定时任务模块实践   CRON

    之前如何撰写定时任务

    *****    /bin/sh /server/scripts/test.sh &>/dev/null

     

     

    minute

    hour

    day

    month

    weekday

    job

    [root@m01 scripts]# crontab -l

    */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1               ###每隔五分钟做一次时间同步

    ansible书写方式:

    [root@m01 scripts]# ansible oldboy -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"

    在对端已经生效:

    TIP:设置name属性 避免重复添加

    重复添加时会报错:

    删除定时任务   absent(缺席)慎用

    [root@m01 scripts]# ansible oldboy -m cron -a "name=luna minute=*/5 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' state=absent"

    说明删除计划任务

    如何注释计划任务:

    正在不是注释状态的计划任务可以注释

    如何取消注释:

     

    ansible剧本书写方式

     

    编写ansible-playbook

    01   编写的时候需要识别空格字符  (严格判断空格)

    02   一定得用空格键  不能用tab

    03   减号和冒号在剧本中使用时(非末尾),后面要接一个空格(例如:   - oldboy) 对于注释信息不能有空格

    04  剧本编写有等级划分   每个等级相差2个空格

    ###剧本编写实践

    ansible-playbook编写格式

    #注释信息

    ###剧本的开头,可以不写

    • hosts:空格all(oldboy&172.16.1.41)

    ###处理所有服务器,找到所有服务器

         cron.yml  剧本内容  存放位置:

    /etc/ansible/ansible-playbook

     

    创建目录

    /etc/ansible/ansible-playbook

    之前学过的其他语法检查命令:

    [root@m01 ansible-playbook]# visudo -c

    /etc/sudoers: parsed OK

    #检查ansible剧本的语法

     

    如果语法有错误的话 ,则会是该提示

     

    ###执行1个剧本

      ##执行前先彩排:本地模拟运行  不影响主机

     

    ##彩排结束后,真正执行剧本

     

    检查确认:

    ###剧本编写扩展

    ansible-playbook cron.yml -v  ###显示ansible命令详细过程

    - hosts: oldboy
      tasks:
      - cron: name=nod03 minute=00 hour=03 job='/bin/sh /server/scripts/test.sh &>/dev/null' state=absent
    cron.yml
    #add cron
    - hosts: all
      tasks:
        - name: cron:nod03
          cron: name=nod03 minute=00 hour=03 job='/bin/sh /server/scripts/test.sh &>/dev/null'
    cron2.yml

     

     

  • 相关阅读:
    Windows 认证小结
    Linux 提权学习小结
    ssrf与gopher与redis
    hacker101 CTF 学习记录(二)
    Hacker101 CTF 学习记录(一)
    libwebsockets支持外部eventloop变更
    ypipe, zmq的核心部件,并行读写的管道。
    std::regex与boost::regex的性能差5倍,有profile有真相。
    Spring整合WebSocket
    温故知新——Spring AOP(二)
  • 原文地址:https://www.cnblogs.com/nodchen/p/9135385.html
Copyright © 2011-2022 走看看