zoukankan      html  css  js  c++  java
  • 1 ansible安装和常用模块

    一 概况:

    不需要安装客户端,通过sshd去通信
    基于模块工作,模块可以由任何语言开发
    不仅支持命令行使用模块,也支持编写yaml格式的playbook
    支持sudo
    有提供UI(浏览器图形化)www.ansible.com/tower 10台主机以内免费
    开源UI https://github.com/alaxli/ansible_ui 文档 http://download.csdn.net/detail/liyang23456/7741185

    二 安装:只需要安装服务端,客户端不需要安装任何东西

    两台机器 172.7.15.106 172.7.15.111
    只需要在106上安装ansible即可

    yum install -y epel-release
    yum install -y ansible
    

    安装完成之后,编辑/etc/ansible/hosts 去添加你要管理的主机

    [test]
    192.168.6.220
    192.168.10.227
    
    

    三 设置秘钥,用来免密码管理

    #################用哪个用户生成的密钥,就必须放到哪个用户的家目录里面
    106上生成密钥对

    ssh-keygen -t rsa  直接回车即可,不用设置密钥密码
    把公钥(id_rsa.pub)内容放到对方机器(111)的/root/.ssh/authorized_keys里面
    方法:scp .ssh/id_rsa.pub  172.7.15.111:/root/.ssh/authorized_keys
    本机也要操作 
    cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
    chmod 600 /root/.ssh/authorized_keys
    关闭selinux
    setenforce 0
    

    如何验证你的秘钥是否成功,可以用ssh 客户端ip 然后看看是否要输入密码,如果直接连接就代表你的秘钥成功了。

    ssh root@172.7.15.111

    四 常用模块

    4.1ping模块

    [yx@localhost ~]$ ansible test -m ping
    192.168.6.220 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    192.168.10.227 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    

    4.2 常用远程命令模块(command、script、shell copy cron)

    ## command 作为 ansible 的默认模块,可以运行远程权限范围所有的 shell 命令,不支持管道符
    
    [yx@localhost ~]$ ansible test -m command -a 'free -h'
    192.168.6.220 | CHANGED | rc=0 >>
                  total        used        free      shared  buff/cache   available
    Mem:           1.8G        629M        201M        8.9M        1.0G        804M
    Swap:          1.6G        194M        1.4G
    
    192.168.10.227 | CHANGED | rc=0 >>
                  total        used        free      shared  buff/cache   available
    Mem:           7.5G        692M        6.4G         74M        488M        6.4G
    Swap:          4.0G          0B        4.0G
    
     
     ## script 的功能是在远程主机执行主控端存储的 shell 脚本文件,前提是ansible服务端已经存在的文件或脚本
     ansible test -m script -a "/tmp/test.sh"
    192.168.6.220 | CHANGED => {
        "changed": true, 
        "rc": 0, 
        "stderr": "Shared connection to 192.168.6.220 closed.
    ", 
        "stderr_lines": [
            "Shared connection to 192.168.6.220 closed."
        ], 
        "stdout": "", 
        "stdout_lines": []
    }
    192.168.10.227 | CHANGED => {
        "changed": true, 
        "rc": 0, 
        "stderr": "Shared connection to 192.168.10.227 closed.
    ", 
        "stderr_lines": [
            "Shared connection to 192.168.10.227 closed."
        ], 
        "stdout": "", 
        "stdout_lines": []
    }
    
    
    ## shell 的功能是执行远程主机上的 shell 脚本文件,支持管道符。前提是远程主机必须存在的
    # 用shell模块的时候,本机上面的脚本是不会执行的,要想执行本机上面的必须用script
    yx@localhost ~]$ ansible test -m shell -a "/tmp/test.sh"
    192.168.10.227 | FAILED | rc=126 >>
    /bin/sh: /tmp/test.sh: 权限不够non-zero return code
    
    192.168.6.220 | CHANGED | rc=0 >>
    
        
     #copy模块用于实现主控端向目标机器复制文件
    #src是源目录,dest是目标目录,mode是权限
    ansible 192.168.6.220 -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755" 
    
    # cron 模块用于配置远程主机 crontab 
    # name是注释的名字,minute是分钟,job是crontab的内容,其他的时间表示:分钟 minute 小时 hour 日期 day 月份 month
    
    [yx@localhost ~]$ ansible test -m cron -a "name='check' minute='1' job='ntpdate asia.pool.ntp.org'"
    192.168.10.227 | CHANGED => {
        "changed": true, 
        "envs": [], 
        "jobs": [
            "check"
        ]
    }
    192.168.6.220 | CHANGED => {
        "changed": true, 
        "envs": [], 
        "jobs": [
            "check"
        ]
    }
    
    
    #查看crontab 
    [yx@localhost ~]$ ansible test -m command -a "crontab -l"
    192.168.6.220 | CHANGED | rc=0 >>
    #Ansible: check
    1 * * * * ntpdate asia.pool.ntp.org
    
    192.168.10.227 | CHANGED | rc=0 >>
    #Ansible: check
    1 * * * * ntpdate asia.pool.ntp.org
     
     ###批量删除crontab  若要删除该cron 只需要加一个字段 state=absent
     
     [yx@localhost ~]$ ansible test -m cron -a "name='check' minute='1' job='ntpdate asia.pool.ntp.org' state=absent"
    192.168.10.227 | CHANGED => {
        "changed": true, 
        "envs": [], 
        "jobs": []
    }
    192.168.6.220 | CHANGED => {
        "changed": true, 
        "envs": [], 
        "jobs": []
    }
    
    
    

    4.3 get_url, yum,service,user ,setup

    # get_url 模块可以实现从远程主机下载指定 URL 到本地,支持 sha256sum 文件教验。
    ansible test -m get_url -a "url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes"
    
    ##yum用于安装软件,这个必须要root用户 
    ansible test -m yum -a "name=httpd state=installed"  # 安装
    ansible testhost -m yum -a "name=httpd state=removed"  ##卸载http软件包 
    
    ##service 模块可以用来管理远程主机的系统服务。 也必须要有root权限才可以
    ansible 192.168.6.220 -m service -a "name=firewalld state=stopped enabled=yes"  #停止
    ansible 192.168.6.220 -m service -a "name=firewalld state=restarted"   #重启
    ansible 192.168.6.220 -m service -a "name=firewalld state=reloaded" # 重新加载
    
    ##user 模块用来进行远程主机用户的管理。 
    ansible Client -m user -a "name=wang comment='user wang'"     # 创建用户 wang
    ansible Client -m user -a "name=wang state=absent remove=yes" # 删除用户 wang
    
    ########查看主机的软硬件信息的
    ansible 192.168.6.220 -m setup
    
    

    ansible-doc -l 列出所有的模块
    ansible-doc cron 查看指定模块的文档

  • 相关阅读:
    MongoDB--CSharp Driver Quickstart .
    关于 IIS7.0下文件写入无权限的解决办法
    Android和WCF通信
    要想有什么样的成就就要有什么样的眼光
    DateTime.Now.ToString() 用法
    机械硬盘怎么看是否4k对齐
    【.Net】在WinForm中选择本地文件
    【.Net】C#获取Windows系统特殊文件夹的路径
    【Python】Python网络编程
    【Asp.Net Core】在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序
  • 原文地址:https://www.cnblogs.com/huningfei/p/12739146.html
Copyright © 2011-2022 走看看