配置互信

  1. # 创建ansible用户
  2. [root@node1 ~]# useradd ansibleuser && echo "123456" | passwd --stdin ansibleuser
  3. # 授权sudo权限
  4. [root@node1 ~]# usermod -g root ansibleuser
  5. [root@node1 ~]# echo 'ansibleuser ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers && grep 'ansibleuser' /etc/sudoers
  6. # 管理机创建公钥
  7. [root@node1 ~]# su - ansibleuser
  8. [ansibleuser@node1 ~]$ ssh-keygen -t rsa
  9. Generating public/private rsa key pair.
  10. Enter file in which to save the key (/home/ansibleuser/.ssh/id_rsa):
  11. Created directory '/home/ansibleuser/.ssh'.
  12. Enter passphrase (empty for no passphrase):
  13. Enter same passphrase again:
  14. Your identification has been saved in /home/ansibleuser/.ssh/id_rsa.
  15. Your public key has been saved in /home/ansibleuser/.ssh/id_rsa.pub.
  16. The key fingerprint is:
  17. 6a:f5:60:9a:2a:ba:49:6a:13:0b:fe:50:9f:a4:8e:2a ansibleuser@node1
  18. The key's randomart image is:
  19. +--[ RSA 2048]----+
  20. | |
  21. | |
  22. | |
  23. | |
  24. | . . S |
  25. |... + .* o |
  26. |ooo. o= . |
  27. |E*= o |
  28. |X+o+. |
  29. +-----------------+
  30. # 传输公钥
  31. [ansibleuser@node1 ~]$ ssh-copy-id -i /home/ansibleuser/.ssh/id_rsa.pub ansibleuser@'172.18.15.182'
  32. The authenticity of host '172.18.15.182 (172.18.15.182)' can't be established.
  33. ECDSA key fingerprint is 39:19:5d:bf:17:97:31:48:63:70:ec:8c:8a:d2:9f:f9.
  34. Are you sure you want to continue connecting (yes/no)? yes
  35. /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  36. /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  37. ansibleuser@172.18.15.182's password:
  38. Number of key(s) added: 1
  39. Now try logging into the machine, with: "ssh 'ansibleuser@172.18.15.182'"
  40. and check to make sure that only the key(s) you wanted were added.
  41. # 测试连接
  42. [ansibleuser@node1 ~]$ ssh 172.18.15.182
  43. Last login: Wed Oct 14 01:01:20 2020 from 172.18.15.183
  44. [ansibleuser@node3 ~]$ exit
  45. logout
  46. Connection to 172.18.15.182 closed.
  47. [ansibleuser@node1 ~]$

安装ansible

  1. yum install ansible
  2. 或者
  3. pip install ansible

检验ansible

  1. [ansibleuser@node1 ~]$ sudo vim /etc/ansible/hosts
  2. [dbservers]
  3. 172.18.15.182
  4. 172.18.15.183

image.png