Ansible免密码登陆

配置Linux主机无密码访问

生成秘钥

ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
73:80:07:fa:9a:0d:e0:0e:d1:c2:44:d2:d2:61:67:21 root@ansible
The key's randomart image is:
+--[ RSA 2048]----+
|o=E.+..          |
|=oo+ . o         |
|ooo . . o        |
| + . . . .       |
|. . . . S .      |
| o   =   o       |
|  . o .          |
|                 |
|                 |
+-----------------+

将管理机上生成的秘钥发送到被管理机

ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

测试是否不需要密码直接执行任务

ansible test -a "date"

172.16.1.10 | SUCCESS | rc=0 >>
Fri Apr  6 21:47:21 CST 2018

172.16.1.11 | SUCCESS | rc=0 >>
Fri Apr  6 21:47:21 CST 2018

执行Ansible任务时的排错

  通过Ansible执行任务时代上-v参数,打开调试模式(加的v个数越多调试的输出信息越多,最多4个v):

  -v的情况下:

ansible test -a "date" -v

Using /etc/ansible/ansible.cfg as config file
172.16.1.11 | SUCCESS | rc=0 >>
Fri Apr  6 21:49:43 CST 2018

172.16.1.10 | SUCCESS | rc=0 >>
Fri Apr  6 21:49:43 CST 2018

  -vv的情况下:

ansible test -a "date" -vv

ansible 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
Using /etc/ansible/ansible.cfg as config file
META: ran handlers
172.16.1.11 | SUCCESS | rc=0 >>
Fri Apr  6 21:49:47 CST 2018

172.16.1.10 | SUCCESS | rc=0 >>
Fri Apr  6 21:49:47 CST 2018

META: ran handlers
META: ran handlers

猜你喜欢

转载自www.cnblogs.com/jie-fang/p/10293182.html