1.环境准备

关闭防火墙

  1. [root@ansible-1 ~]# systemctl stop firewalld
  2. [root@ansible-1 ~]# systemctl disable firewalld
  3. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  4. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
  5. # setenforce 0
  6. # cat /etc/selinux/config
  7. # disabled - No SELinux policy is loaded.
  8. SELINUX=disabled //将此处改为disabled
  9. # SELINUXTYPE= can take one of three two values:
  10. [root@ansible-1 ~]# cat /etc/hosts
  11. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  12. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  13. 192.168.200.100 ansible-1 //添加两台主机的IP和主机名
  14. 192.168.200.110 ansible-2

更改源为阿里,并安装epel仓库

  1. [root@ansible-1 ~]# #wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-6.repo
  2. [root@ansible-1 ~]# #rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  3. [root@ansible-1 ~]# # yum install epel-release -y

2.建立互信

在AB主机中生成密钥

  1. [root@ansible-1 ~]# ssh-keygen -t rsa
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Created directory '/root/.ssh'.
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again:
  7. Your identification has been saved in /root/.ssh/id_rsa.
  8. Your public key has been saved in /root/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. 1a:42:a5:2f:7a:17:82:33:2f:4a:7b:e4:24:a6:1d:0f root@ansible-1
  11. The key's randomart image is:
  12. +--[ RSA 2048]----+
  13. | . |
  14. | o |
  15. | o |
  16. | o . |
  17. | + + + S |
  18. | oEB + + |
  19. |ooB+o o |
  20. |o.o=.. |
  21. |... |
  22. +-----------------+
  23. [root@ansible-1 ~]# cd .ssh/
  24. [root@ansible-1 .ssh]# ls
  25. id_rsa id_rsa.pub
  26. [root@ansible-1 .ssh]# cat id_rsa.pub
  27. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDkkgxhQ//HoJBFhgF4eRFDOzHKMfma5w012MwNsIYr2GA9POhMlbcpZcmUYkU59vNuZv3jN3MfeeGieiCXFXVCCZ8sasRPf84NqkDU6sXl4RdR70ZmPY9m3Vn3QLbs25oz6aumAtotstPH+jBA+CKaze0xlL12jH94yRToc0lLVa2k4gxkjd2Um6Co9m2NpkdWJ320np4avJMdFF50beZ4sicH0UZc5PXataS1cMC+TM8D/EWR4X4AGw/sBzQiOY5YqZ2rzf+hwu6yekAtlG1qCpPJ9SxBK6OUdY6CPoFRekFADyyLLh1RHn37OEMKDf5phzUgx9ATDTFFLeuJAmFV root@ansible-1
  28. [root@ansible-1 .ssh]#

复制私钥

  1. [root@ansible-2 ~]# ssh-keygen -t rsa
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Created directory '/root/.ssh'.
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again:
  7. Your identification has been saved in /root/.ssh/id_rsa.
  8. Your public key has been saved in /root/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. 56:19:87:9a:a8:b1:3c:10:20:e1:89:df:47:de:8b:48 root@ansible-2
  11. The key's randomart image is:
  12. +--[ RSA 2048]----+
  13. |+o ... |
  14. |+ o .+ |
  15. |.o . .. oo |
  16. | ....o..o. |
  17. | .oE+o S |
  18. | .=o o . |
  19. | ... . |
  20. | |
  21. | |
  22. +-----------------+
  23. [root@ansible-2 ~]# cd .ssh/
  24. [root@ansible-2 .ssh]# vim authorized_keys
  25. [root@ansible-2 .ssh]# cat authorized_keys
  26. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDkkgxhQ//HoJBFhgF4eRFDOzHKMfma5w012MwNsIYr2GA9POhMlbcpZcmUYkU59vNuZv3jN3MfeeGieiCXFXVCCZ8sasRPf84NqkDU6sXl4RdR70ZmPY9m3Vn3QLbs25oz6aumAtotstPH+jBA+CKaze0xlL12jH94yRToc0lLVa2k4gxkjd2Um6Co9m2NpkdWJ320np4avJMdFF50beZ4sicH0UZc5PXataS1cMC+TM8D/EWR4X4AGw/sBzQiOY5YqZ2rzf+hwu6yekAtlG1qCpPJ9SxBK6OUdY6CPoFRekFADyyLLh1RHn37OEMKDf5phzUgx9ATDTFFLeuJAmFV root@ansible-1
  27. [root@ansible-2 .ssh]# chmod 700 authorized_keys
  28. [root@ansible-2 .ssh]# ssh 192.168.200.100
  29. [root@ansible-1 .ssh]# vim authorized_keys
  30. [root@ansible-1 .ssh]# cat authorized_keys
  31. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8B3f352Pf/8JYupVLRqAaUoFHzV+MQYcRu4G3CpJKNpDVpsKbS1yMCVK7NGSbwGhV2j2kLJ2Kzvo9Q4Sg1QKxmTFpOl1HboOvbxY91el9VTweYaivv7HI7w5Xq641ky6gQyPY5nKWrDhmiLg9JRCaCokRfO39t2ZJPUzYu2MD/QqTAnBB+H06grJ4vrkWSxZCv15/JvDbPjCfIrwWfy6inZeACVx7mBzD9JsEnYXNzXJGK0O45wL5JADnqRFdskP7tOF0B8Wwn7EDUjJycZbTwwpRFaZltFkLfh7RuKU2pi9TUUiPzoWcHhrk3B18ap8a411sqBYqO73INCcmQ90X root@ansible-2
  32. [root@ansible-1 .ssh]# chmod 700 authorized_keys

验证互信是否建立成功

  1. [root@ansible-1 ~]# ssh 192.168.200.110
  2. Last login: Thu Apr 21 23:39:13 2022 from 192.168.200.100
  3. [root@ansible-2 ~]#
  4. [root@ansible-2 ~]# ssh 192.168.200.100
  5. Last login: Thu Apr 21 23:39:23 2022 from 192.168.200.110
  6. [root@ansible-1 ~]#

3.安装ansible

  1. [root@ansible-1 ~]# yum install -y ansible
  2. [root@ansible-1 ~]# ansible --version
  3. ansible 2.9.27
  4. config file = /etc/ansible/ansible.cfg
  5. configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  6. ansible python module location = /usr/lib/python2.7/site-packages/ansible
  7. executable location = /usr/bin/ansible
  8. python version = 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]

4.设置主机组

  1. [root@ansible-1 ~]# vim /etc/ansible/hosts
  2. 插入以下:
  3. [testhost]
  4. 192.168.200.100 \\A主机的IP
  5. 192.168.200.110 \\B主机的IP

说明:testhost为自定义的主机组名字,下面两个IP为组内的机器IP。

如果在对本机(即安装ansible的节点)进行ssh连接时报错

  1. 192.168.200.100 | UNREACHABLE! => {
  2. "changed": false,
  3. "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
  4. "unreachable": true
  5. }

原因分析:没有在该节点(即安装ansible的节点)上添加目标节点(即需要管理的节点)的ssh认证信息。
解决方法

  1. [root@ansible-1 ~]# ssh-copy-id root@ansible-1
  2. The authenticity of host 'ansible-1 (192.168.200.100)' can't be established.
  3. ECDSA key fingerprint is aa:1a:ea:6b:db:bf:68:ab:fa:2c:61:8e:7c:e1:58:89.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  6. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  7. root@ansible-1's password:
  8. Number of key(s) added: 1
  9. Now try logging into the machine, with: "ssh 'root@ansible-1'"
  10. and check to make sure that only the key(s) you wanted were added.

这里root是在目标节点上登录的用户,@符号后面接目标节点IP即可,之后会提示输入目标节点root用户密码,输入即可。
添加认证信息后,目标节点主机的~/.ssh/目录下将会出现一个authorized_keys文件,里面包含了ansible管理节点的公钥信息,可以检查一下是否存在。