zoukankan      html  css  js  c++  java
  • ansible批量管理服务

    ansible批量管理服务

    ansible软件介绍

    1. ansible是一个基于Python开发的自动化运维工具
    2. ansible是一个基于ssh协议实现远程管理的工具
    3. ansible软件可以实现多种批量管理操作(批量系统配置、批量软件部署、批量文件拷贝、批量运行命令),类似软件saltstack与puppet
    

    ansible软件的特点

    1.ansible软件服务端(管理端):不需要启动任何服务 默认服务端不需要任何的配置
    2.ansible软件客户端(受控端):没有客户端软件安装
    

    ansible软件应用模块

    ansible批量管理语法

    语法:ansible 远程主机ip/远程主机组名称 -m 模块名 -a "命令"
    -m:指定模块
    -a:利用模块中的某些参数
    [root@m01 ansible]# ansible 172.16.1.7 -m command -a "hostname"
    172.16.1.7 | SUCCESS | rc=0 >>
    web01
    

    命令类型模块

    第一个模块:command(在远程主机上执行某些命令)

    官方参考链接:http://docs.ansible.com/ansible/latest/modules/command_module.html
    
    参数:chdir--执行某个命令前,先切换目录
    [root@m01 ansible]# ansible 172.16.1.41 -m command -a "chdir=/backup/ touch a.txt "
    172.16.1.41 | SUCCESS | rc=0 >>
    
    参数:creates--判断一个文件是否存在,如果存在了,后面的命令将不执行
    #文件不存在,则会创建a1.TXT这一个文件
    [root@m01 ansible]# ansible 172.16.1.41 -m command -a "creates=/backup/a1.txt touch /
    backup/a1.txt" 
    [WARNING]: Consider using the file module with state=touch rather than running
    touch.  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.
    
    172.16.1.41 | SUCCESS | rc=0 >>
    
    #文件存在,则不会执行后面的命令
    [root@m01 ansible]# ansible 172.16.1.41 -m command -a "creates=/backup/a1.txt touch /
    backup/a1.txt"172.16.1.41 | SUCCESS | rc=0 >>
    skipped, since /backup/a1.txt exists
    
    参数:removes--判断一个文件是否存在,如果存在,则会执行后面的命令
    #文件不存在,后面的命令不会执行
    [root@m01 ansible]# ansible 172.16.1.41 -m command -a "removes=/backup/a12.txt ls"
    172.16.1.41 | SUCCESS | rc=0 >>
    skipped, since /backup/a12.txt does not exist
    #文件存在,后面的命令会执行
    [root@m01 ansible]# ansible 172.16.1.41 -m command -a "removes=/backup/a1.txt ls /bac
    kup/"172.16.1.41 | SUCCESS | rc=0 >>
    a1.txt
    a.txt
    
    参数:free_from--表示command的模块时,必须要有linux合法命令信息
    [root@m01 ansible]# ansible 172.16.1.41 -m command -a "hostname"
    172.16.1.41 | SUCCESS | rc=0 >>
    backup
    

    第二个模块:shell模块(万能模块)

    参数:chdir---在执行莫个命令前,先切换目录
    参数:creates---判断一个文件是否存在,如果已经存在了,后面的命令就不会执行
    参数:removes---判断一个文件是否存在,如果存在,执行后面的命令
    参数(必须要有的):free_form---表示执行command模块时,必须要有linux合法命令信息
    ps:shell模块可以满足command模块的所有功能,并且可以支持识别特殊符号(<,>,|,&)
    示例:
    #command模块不支持特殊符号
    [root@m01 ansible]# ansible 172.16.1.41 -m command -a "hostname;ls"
    172.16.1.41 | FAILED | rc=2 >>
    [Errno 2] No such file or directory
    #shell模块支持特殊符号
    [root@m01 ansible]# ansible 172.16.1.41 -m shell -a "hostname;ls"
    172.16.1.41 | SUCCESS | rc=0 >>
    backup
    a1.txt
    anaconda-ks.cfg
    install.log
    install.log.syslog
    

    第三个模块:script--专门用来运行脚本的模块

    参数:chdir---在执行莫个命令前,先切换目录
    参数:creates---判断一个文件是否存在,如果已经存在了,后面的命令就不会执行
    参数:removes---判断一个文件是否存在,如果存在,执行后面的命令
    参数(必须要有的):free_form---表示执行command模块时,必须要有linux合法命令信息
    示例:
    [root@m01 scripts]# ansible backup -m script -a "/server/scripts/test.sh"
    

    文件类型模块

    第一个模块:copy--复制模块

    参数:src--定义要推送的数据信息
    参数:dest--定义将数据推送到远程主机的什么目录中
    参数:group--设置复制后的文件属组权限
    参数:owner--设置复制后文件的属主权限
    参数:mode--设置复制后的文件权限(600 755)
    #复制文件后并将文件的所属主与组权限变成oldboy,文件权限变成755
    [root@m01 scripts]# ansible 172.16.1.41 -m copy -a "src=/server/scripts/file01.txt de
    st=/tmp/ group=oldboy owner=oldboy mode=755"
    172.16.1.41 | SUCCESS => {
        "changed": true, 
        "checksum": "a9f1095e878205d8714b996413832284f6315efd", 
        "dest": "/tmp/file01.txt", 
        "gid": 501, 
        "group": "oldboy", 
        "mode": "0755", 
        "owner": "oldboy", 
        "path": "/tmp/file01.txt", 
        "size": 5, 
        "state": "file", 
        "uid": 501
    }
    #查看复制到远程主机的文件权限信息
    [root@m01 scripts]# ansible 172.16.1.41 -m shell -a "ls -l /tmp/file01.txt"
    172.16.1.41 | SUCCESS | rc=0 >>
    -rwxr-xr-x 1 oldboy oldboy 5 Feb 19 14:55 /tmp/file01.txt
    
    参数:backup--对数据信息进行备份
    [root@m01 scripts]# ansible 172.16.1.41 -m copy -a "src=/server/scripts/file01.txt de
    st=/tmp/ backup=yes"
    172.16.1.41 | SUCCESS => {
        "backup_file": "/tmp/file01.txt.13861.2019-02-19@14:55:29~", 
        "changed": true, 
        "checksum": "a9f1095e878205d8714b996413832284f6315efd", 
        "dest": "/tmp/file01.txt", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "1f59a02b8121a2ca886bf842ad8c5cf1", 
        "mode": "0644", 
        "owner": "root", 
        "size": 5, 
        "src": "/root/.ansible/tmp/ansible-tmp-1550559328.04-203924633988673/source", 
        "state": "file", 
        "uid": 0
    }
    

    第二个模块:file--文件属性修改、文件创建、目录创建

    参数:src--定义要推送的数据信息
    参数:dest--定义将数据推送到远程主机的什么目录中
    参数:group--设置复制后的文件属组权限
    参数:owner--设置复制后文件的属主权限
    #改变远程主机文件权限
    [root@m01 scripts]# ansible 172.16.1.41 -m file -a "dest=/tmp/123.tar.gz owner=oldboy
     group=oldboy mode=600 "
    172.16.1.41 | SUCCESS => {
        "changed": true, 
        "gid": 501, 
        "group": "oldboy", 
        "mode": "0600", 
        "owner": "oldboy", 
        "path": "/tmp/123.tar.gz", 
        "size": 127456, 
        "state": "file", 
        "uid": 501
    }
    #查看修改后文件信息
    [root@m01 scripts]# ansible 172.16.1.41 -m shell -a "ls -l /tmp/123.tar.gz"
    172.16.1.41 | SUCCESS | rc=0 >>
    -rw------- 1 oldboy oldboy 127456 Feb 12 18:34 /tmp/123.tar.gz
    
    参数:state--用于指定创建目录或文件
    #创建文件
    [root@m01 scripts]# ansible 172.16.1.41 -m file -a "dest=/tmp/file02.txt state=touch"
     172.16.1.41 | SUCCESS => {
        "changed": true, 
        "dest": "/tmp/file02.txt", 
        "gid": 0, 
        "group": "root", 
        "mode": "0644", 
        "owner": "root", 
        "size": 0, 
        "state": "file", 
        "uid": 0
    }
    #创建目录
    [root@m01 scripts]# ansible 172.16.1.41 -m file -a "dest=/tmp/test state=directory"
    172.16.1.41 | SUCCESS => {
        "changed": true, 
        "gid": 0, 
        "group": "root", 
        "mode": "0755", 
        "owner": "root", 
        "path": "/tmp/test", 
        "size": 4096, 
        "state": "directory", 
        "uid": 0
    }
    #查看文件内容
    [root@m01 scripts]# ansible 172.16.1.41 -m shell -a "ls -l /tmp/"
    172.16.1.41 | SUCCESS | rc=0 >>
    total 292
    -rw-r--r-- 1 root   root        0 Feb 19 16:51 file02.txt
    drwxr-xr-x 2 root   root     4096 Feb 19 16:52 test
    

    包管理模块类型

    第一个模块:yum--安装软件包模块

    参数:name--执行要安装软件的名称,以及软件的版本
    参数:state--指定的命令,installed安装,absent卸载
    #安装
    [root@m01 scripts]# ansible 172.16.1.41 -m yum -a "name=iftop state=installed"
    #卸载
    [root@m01 scripts]# ansible 172.16.1.41 -m yum -a "name=iftop state=absent"
    
    参数:list--指定软件名称,用于查看软件是否已经安装
    [root@m01 scripts]# ansible 172.16.1.41 -m yum -a "list=iftop"
    

    系统模块类型

    第一个模块:service--管理服务模块

    参数:name--指定管理的服务名称(管理的服务一定在在chkconfig中看到)
    参数:state--指定类型(stopped,started,restarted,reloaded)
    参数:enabled:yes表示开机自启动,NO表示开机不启动
    #将crond服务停止
    [root@m01 scripts]# ansible 172.16.1.41 -m service -a "name=crond state=stopped"
    172.16.1.41 | SUCCESS => {
        "changed": true, 
        "name": "crond", 
        "state": "stopped"
    }
    #将crond服务启动
    [root@m01 scripts]# ansible 172.16.1.41 -m service -a "name=crond state=started"
    172.16.1.41 | SUCCESS => {
        "changed": true, 
        "name": "crond", 
        "state": "started"
    }
    #设置crond服务开启不启动
    [root@m01 scripts]# ansible 172.16.1.41 -m service -a "name=crond  enabled=no"
    172.16.1.41 | SUCCESS => {
        "changed": true, 
        "enabled": false, 
        "name": "crond"
    }
    #设置crond服务开机启动
    [root@m01 scripts]# ansible 172.16.1.41 -m service -a "name=crond  enabled=yes"
    172.16.1.41 | SUCCESS => {
        "changed": true, 
        "enabled": true, 
        "name": "crond"
    }
    

    第二个模块:cron--定时任务模块

    参数:minute--分
    参数:hour--时
    参数:day --日
    参数:month--月
    参数:weekday--周
    参数:job--需要执行的任务
    #添加定时任务
    [root@m01 scripts]# ansible 172.16.1.41 -m cron -a "name=backup minute=*/10 job='/bin
    /sh /server/scripts/test.sh >/dev/null 2>&1'"
    172.16.1.41 | SUCCESS => {
        "changed": true, 
        "envs": [], 
        "jobs": [
            "None", 
            "backup"
        ]
    }
    [root@m01 scripts]# ansible 172.16.1.41 -m shell -a "crontab -l"
    172.16.1.41 | SUCCESS | rc=0 >>
    #time sync by lidao at 2017-03-08
    */5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
    #Ansible: None
    */10 * * * * /bin/sh /server/scripts/test.sh
    #Ansible: backup
    */10 * * * * /bin/sh /server/scripts/test.sh >/dev/null 2>&1
    #删除定时任务
    [root@m01 scripts]# ansible 172.16.1.41 -m cron -a "name=backup state=absent"
    172.16.1.41 | SUCCESS => {
        "changed": true, 
        "envs": [], 
        "jobs": []
    }
    #注释定时任务
    [root@m01 scripts]# ansible 172.16.1.41 -m cron -a "name=backup minute=*/10 job='/bin
    /sh /server/scripts/test.sh >/dev/null 2>&1' disabled=yes"
    172.16.1.41 | SUCCESS => {
        "changed": true, 
        "envs": [], 
        "jobs": [
            "backup"
        ]
    }
    #打开注释定时任务
    [root@m01 scripts]# ansible 172.16.1.41 -m cron -a "name=backup minute=*/10 job='/bin
    /sh /server/scripts/test.sh >/dev/null 2>&1' disabled=no"172.16.1.41 | SUCCESS => {
        "changed": true, 
        "envs": [], 
        "jobs": [
            "backup"
        ]
    }
    

    总结ansible颜色信息

    绿色:查看远程主机信息,不会对远程主机系统做任何修改
    红色:执行操作出现异常错误
    黄色:对远程主机系统进行修改操作
    粉色:警告或者忠告信息
    

    ansible编写规范

    编写剧本规范:http://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
    遵循pyyaml
    
    
    - 用法说明,表示列表显示的内容
        水果信息:
            - 苹果
            - 香蕉
            - 西瓜
    : 用法说明:
        姓名: 张三
            性别: 男
            人员信息:
            - 运维人员: sa
            - 开发人员: dev
            - 存储人员: dba
    空格 用法说明:
            对内容进行分级时,需要有两个空格表示分级
            软件安装步骤:
              - 服务端安装步骤:
                第一个里程碑: 检查软件是否安装
                第二个里程碑: 编写配置文件内容
              - 客户端安装步骤:
            补充:必须使用空格分隔ansible剧本级别,一定不要使用tab键进行分割
    
    执行脚本方法:
    #正式执行脚本
    ansible-playbook /etc/ansible/ansible-playbook/test.yaml
    #测试
    ansible-playbook -C /etc/ansible/ansible-playbook/test.yaml
    
    示例1:ansible部署rsync服务
    [root@m01 conf]# cat /server/scripts/rsync.yaml 
    # command playbook
    - hosts: 172.16.1.41
      tasks:
        - name: setup01:install rsync
          yum: name=rsync state=installed
        - name: setup02:edit rsync conf file
          copy: src=/etc/ansible/conf/rsyncd.conf dest=/etc/
        - name: setup03:create rsync user
          user: name=rsync state=present create_home=no shell=/sbin/nologin
        - name: setup04:create auth file
          copy: src=/etc/ansible/conf/rsync.password dest=/etc/ mode=600
        - name: setup05:create backup dir
          file: dest=/backup state=directory owner=rsync group=rsync
        - name: setup06:boot rsync server
          shell: rsync --daemon creates=/var/run/rsyncd.pid
    - hosts: 172.16.1.31
      tasks:
        - name: setup01:create auth file
          copy: src=/etc/ansible/conf/rsync_client.password dest=/etc/rsync.password mode=600
    
  • 相关阅读:
    ssh中的 Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
    Http中的Get/Post方法
    Node.js(day2)
    使用clipBoard.js进行页面内容复制
    SVG之图形的引用use、剪切clipPath和蒙板mask
    SVG之文本
    SVG之Path
    SVG之颜色、渐变和笔刷的使用
    SVG坐标系统
    SVG入门
  • 原文地址:https://www.cnblogs.com/yjiu1990/p/10508643.html
Copyright © 2011-2022 走看看