美文网首页
推荐-linux自动化运维工具ansible

推荐-linux自动化运维工具ansible

作者: 昵称已被使用_ | 来源:发表于2021-03-02 19:48 被阅读0次

    前言

    • 服务器运维时,经常会依次操作每台服务器执行相同的命令,重复操作显然效率低下
    • 本文推荐使用工具ansible,批量操作服务器
    • 我这里先准备3台服务器
    IP地址          
    192.168.88.5    
    192.168.88.6    
    192.168.88.7    
    

    安装及配置

    # 安装 ansible,你想用哪台服务器批量操作就给哪台安装
    yum -y install ansible
    
    # 编辑配置文件,配置ip组
    vim /etc/ansible/hosts
    
    # 给配置文件添加ip组,如下内容,我这里建了4个组
    [my]
    192.168.88.5
    192.168.88.6
    192.168.88.7
    
    [a]
    192.168.88.5
    
    [b]
    192.168.88.6
    
    [c]
    192.168.88.7
    

    设置无密访问

    ssh-keygen
    ssh-copy-id root@192.168.88.5
    ssh-copy-id root@192.168.88.6
    ssh-copy-id root@192.168.88.7
    

    测试是否连通

    ansible my -m ping
    

    发送指令到ip组,成功会返回结果

    # my组3台服务器执行date命令
    ansible my -m shell -a "date"
    # my组3台服务器执行ip addr | grep 88
    ansible my -m shell -a "ip addr | grep 88"
    # my组台服务器停止redis服务
    ansible my -m shell -a "/sbin/service redis stop"
    # 操作b组,执行date命令
    ansible b -m shell -a "date"
    

    效果演示

    [root@localhost redis]# ansible my -m shell -a "date"
    192.168.88.5 | CHANGED | rc=0 >>
    Wed Mar  3 03:25:43 CST 2021
    192.168.88.7 | CHANGED | rc=0 >>
    Wed Mar  3 03:25:43 CST 2021
    192.168.88.6 | CHANGED | rc=0 >>
    Wed Mar  3 03:25:43 CST 2021
    [root@localhost redis]# ansible test -m shell -a "ip addr | grep 88"
    192.168.88.5 | CHANGED | rc=0 >>
        inet 192.168.88.5/24 brd 192.168.88.255 scope global noprefixroute ens32
    192.168.88.6 | CHANGED | rc=0 >>
        inet 192.168.88.6/24 brd 192.168.88.255 scope global noprefixroute ens32
    192.168.88.7 | CHANGED | rc=0 >>
        inet 192.168.88.7/24 brd 192.168.88.255 scope global noprefixroute ens32
        inet 192.168.88.88/24 scope global secondary ens32
    [root@localhost redis]# ansible ^Cm shell -a "/sbin/service redis stop"
    [root@localhost redis]# ^C
    [root@localhost redis]# ^C
    [root@localhost redis]# ansible my -m shell -a "date"
    192.168.88.5 | CHANGED | rc=0 >>
    Wed Mar  3 03:26:24 CST 2021
    192.168.88.7 | CHANGED | rc=0 >>
    Wed Mar  3 03:26:25 CST 2021
    192.168.88.6 | CHANGED | rc=0 >>
    Wed Mar  3 03:26:24 CST 2021
    [root@localhost redis]# ansible my -m shell -a "ip addr | grep 88"
    192.168.88.6 | CHANGED | rc=0 >>
        inet 192.168.88.6/24 brd 192.168.88.255 scope global noprefixroute ens32
    192.168.88.5 | CHANGED | rc=0 >>
        inet 192.168.88.5/24 brd 192.168.88.255 scope global noprefixroute ens32
    192.168.88.7 | CHANGED | rc=0 >>
        inet 192.168.88.7/24 brd 192.168.88.255 scope global noprefixroute ens32
        inet 192.168.88.88/24 scope global secondary ens32
    [root@localhost redis]# ansible my -m shell -a "/sbin/service redis stop"
    [WARNING]: Consider using the service module rather than running 'service'.  If you need to use command because service 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.
    192.168.88.6 | CHANGED | rc=0 >>
    Stopping ...
    Redis stoppedWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    192.168.88.5 | CHANGED | rc=0 >>
    /www/server/redis/src/redis-cli -h 0.0.0.0 -p 6379 -a 123123
    Stopping ...
    Redis stoppedWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    192.168.88.7 | CHANGED | rc=0 >>
    Stopping ...
    Redis stoppedWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    [root@localhost redis]# ansible b -m shell -a "date"
    192.168.88.6 | CHANGED | rc=0 >>
    Wed Mar  3 03:27:25 CST 2021
    

    相关文章

      网友评论

          本文标题:推荐-linux自动化运维工具ansible

          本文链接:https://www.haomeiwen.com/subject/qevxqltx.html