Ansible基本使用

  1. 安装

    [root@localhost ~]# rpm Uvh http://mirrors.ustc.edu.cn/epel/epel-release-latest-7.noarch.rpm

    [root@localhost ~]# yum -y install epel-release

    [root@localhost ~]# yum -y install ansible

      

  2. 重要文件

    ansible的安装目录是/etc/ansible

    ansible.cfg             //配置文件
    hosts                   //inventory,ansible需要连接的主机列表,可以填ip或者域名
    [root@localhost ~]# vi /etc/ansible/hosts 
    
    # This is the default ansible 'hosts' file.
    #
    # It should live in /etc/ansible/hosts
    
    [web]
    192.168.56.44
    192.168.56.45
    192.168.56.42
    192.168.56.43
    
    [client]
    192.168.56.100

     

  3. 实战
    1. ping模块  判断远程客户端主机是否在线,ping服务器本身
      [root@localhost ~]# ansible  -k all -m  ping
      SSH password: 
      192.168.56.45 | SUCCESS => {
          "changed": false, 
          "ping": "pong"
      }
      192.168.56.44 | SUCCESS => {
          "changed": false, 
          "ping": "pong"
      }
      192.168.56.42 | SUCCESS => {
          "changed": false, 
          "ping": "pong"
      }
      192.168.56.100 | SUCCESS => {
          "changed": false, 
          "ping": "pong"
      }
      192.168.56.43 | SUCCESS => {
          "changed": false, 
          "ping": "pong"
      }

       

    2. command模块 默认是command模块,所以可以不 -m command指定模块
      [root@localhost ~]# ansible  -k all -a "date"
      SSH password: 
      192.168.56.100 | SUCCESS | rc=0 >>
      Thu Jun 21 22:58:18 CST 2018
      
      192.168.56.42 | SUCCESS | rc=0 >>
      Thu Jun 21 14:58:20 CST 2018
      
      192.168.56.43 | SUCCESS | rc=0 >>
      Thu Jun 21 14:58:17 CST 2018
      
      192.168.56.44 | SUCCESS | rc=0 >>
      Thu Jun 21 14:58:17 CST 2018
      
      192.168.56.45 | SUCCESS | rc=0 >>
      Thu Jun 21 14:58:18 CST 2018

       实例:修改网卡网管并重启网络

      [root@localhost ~]# ansible  -k all -a " sed -i '/GATEWAY/s/192.168.56.2/192.168.56.1/g' /etc/sysconfig/network-scripts/ifcfg-ens33    "
      SSH password: 
       [WARNING]: Consider using the replace, lineinfile or template module rather than running sed.  If you need to use command because replace, lineinfile or template 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.56.100 | FAILED | rc=2 >>
      sed: can't read /etc/sysconfig/network-scripts/ifcfg-ens33: No such file or directorynon-zero return code
      192.168.56.45 | SUCCESS | rc=0 >>
      192.168.56.43 | SUCCESS | rc=0 >>
      192.168.56.42 | SUCCESS | rc=0 >>
      192.168.56.44 | SUCCESS | rc=0 >>
      
      
      [root@localhost ~]# ansible  -k all -a "systemctl restart network "
      SSH password: 
      192.168.56.45 | SUCCESS | rc=0 >>
      192.168.56.43 | SUCCESS | rc=0 >>
      192.168.56.44 | SUCCESS | rc=0 >>
      192.168.56.42 | SUCCESS | rc=0 >>
      192.168.56.100 | SUCCESS | rc=0 >>

       

  4. 报错处理

    1.   第一次执行ansible命令,但是管控机从来没有登录过被管控机,会报如下错误

      [root@localhost ~]# ansible -k all -m ping
      SSH password: 
      192.168.56.100 | FAILED! => {
          "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
      }

       

        解决方案:

      [root@localhost ~]# vim /etc/ansible/ansible.cfg
      .......
      # uncomment this to disable SSH key host checking
      host_key_checking = False  

       

        验证:

      [root@localhost ~]# ansible -k all -m ping
      SSH password: 
      192.168.56.100 | SUCCESS => {
          "changed": false, 
          "ping": "pong"
      }

       

        

       

转载于:https://www.cnblogs.com/gcl-top/p/9209041.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值