练习

练习1:编写一个inventory文件,管理虚拟机(假设虚拟机IP为192.168.33.10)。

文件名:hosts

[static_page]
192.168.33.10

练习2:使用Ansibleping模块。

ansible -i hosts all -m ping

执行成功之后,应该显示如下结果:

$ ansible -i hosts all -m ping
192.168.33.10 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
可能出现的错误:
  • 错误1:宿主机的public key没有放到虚拟机的authorized_keys中。

    $ ansible -i hosts all -m ping
    192.168.33.10 | UNREACHABLE! => {
      "changed": false, 
      "msg": "ERROR! SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue", 
      "unreachable": true
    }
    

      可执行下述命令,或者手动将宿主机(以Mac为例)的public key粘贴追加到虚拟机的authorized_keys中:

    cat ~/.ssh/id_rsa.pub | ssh [email protected] "grep . >> .ssh/authorized_keys"
    
  • 错误2:宿主机的known_hosts中以前加入过和该IP地址。

    $ cat ~/.ssh/id_rsa.pub | ssh [email protected] "grep . >> .ssh/authorized_keys"
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
    Someone could be eavesdropping on you right now (man-in-the-middle attack)!
    It is also possible that a host key has just been changed.
    The fingerprint for the RSA key sent by the remote host is
    f2:dc:fd:39:b2:fe:04:2b:cb:d0:b1:e8:c0:12:53:ca.
    Please contact your system administrator.
    Add correct host key in /Users/yuzhang/.ssh/known_hosts to get rid of this message.
    Offending RSA key in /Users/yuzhang/.ssh/known_hosts:4
    RSA host key for 192.168.33.10 has changed and you have requested strict checking.
    Host key verification failed.
    

      可执行下述命令。或者手动去宿主机的know_hosts中删除对应的IP:

    ssh-keygen -R 192.168.33.10
    

results matching ""

    No results matching ""