• Linux之免密登录及ansible安装(超详解)


    目录

     准备三台机器:配置IP,主机名,/etc/hosts​编辑然后去配置免密登录

    1.上传公钥

     2.将公钥传给用户

     3.验证免密登录

    配置yum源使用Centos-stream.repo

    ​ 安装ansible

    1.首先安装源

    2. 下载ansible

    3.使用ansible --version去验证

    配置清单

    /etc/ansible/hosts[node]rhcerhel

    ​使用ansible node -m ping去验证


     准备三台机器:
    配置IP,主机名,/etc/hosts
    然后去配置免密登录

    1.上传公钥

    1. [root@rhcsa .ssh]# 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. Enter passphrase (empty for no passphrase):
    5. Enter same passphrase again:
    6. Your identification has been saved in /root/.ssh/id_rsa.
    7. Your public key has been saved in /root/.ssh/id_rsa.pub.
    8. The key fingerprint is:
    9. SHA256:0ihaGqzhrrggldA2+PdMixdf80qkmODATm08Z/77wmE root@rhcsa
    10. The key's randomart image is:
    11. +---[RSA 3072]----+
    12. | |
    13. | o |
    14. |o + |
    15. | * = o |
    16. |. X X B S + |
    17. |.* X % BE+ o |
    18. |+.+ o Ooo.. . |
    19. |= . .o. . |
    20. |=o o+o |
    21. +----[SHA256]-----+

     2.将公钥传给用户

    1. [root@rhcsa .ssh]# ssh-copy-id root@rhce
    2. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
    3. The authenticity of host 'rhce (192.168.40.132)' can't be established.
    4. ECDSA key fingerprint is SHA256:70V58bQzrfUci7EE23sAS/cd7Zjc3zbRhTx15uN1PVY.
    5. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    6. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    7. /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
    8. root@rhce's password:
    9. Number of key(s) added: 1
    10. Now try logging into the machine, with: "ssh 'root@rhce'"
    11. and check to make sure that only the key(s) you wanted were added.
    12. [root@rhcsa .ssh]# ssh-copy-id root@rhel
    13. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
    14. The authenticity of host 'rhel (192.168.40.135)' can't be established.
    15. ECDSA key fingerprint is SHA256:70V58bQzrfUci7EE23sAS/cd7Zjc3zbRhTx15uN1PVY.
    16. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    17. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    18. /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
    19. root@rhel's password:
    20. Number of key(s) added: 1
    21. Now try logging into the machine, with: "ssh 'root@rhel'"
    22. and check to make sure that only the key(s) you wanted were added.

     3.验证免密登录

    1. [root@rhcsa .ssh]# ssh root@rhce
    2. Activate the web console with: systemctl enable --now cockpit.socket
    3. This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
    4. To register this system, run: insights-client --register
    5. Last failed login: Tue Aug 2 17:13:21 CST 2022 from 192.168.40.129 on ssh:notty
    6. There were 2 failed login attempts since the last successful login.
    7. Last login: Tue Aug 2 16:26:36 2022 from 192.168.40.129
    8. [root@rhce ~]# exit
    9. logout
    10. Connection to rhce closed.
    11. [root@rhcsa .ssh]# ssh root@rhel
    12. Activate the web console with: systemctl enable --now cockpit.socket
    13. This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
    14. To register this system, run: insights-client --register
    15. Last login: Tue Aug 2 16:55:17 2022 from 192.168.40.129
    16. [root@rhel ~]# exit
    17. logout
    18. Connection to rhel closed.

    配置yum源使用Centos-stream.repo

     安装ansible

    1.首先安装源

    1. [root@rhcsa yum.repos.d]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-1.el8.noarch.rpm
    2. Retrieving https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-1.el8.noarch.rpm
    3. warning: /var/tmp/rpm-tmp.uSbUwD: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
    4. Verifying... ################################# [100%]
    5. Preparing... ################################# [100%]
    6. Updating / installing...
    7. 1:zabbix-release-6.0-1.el8 ################################# [100%]
    1. [root@rhcsa yum.repos.d]# yum install -y https://mirrors.tuna.tsinghua.edu.cn/epel/epel-release-latest-8.noarch.rpm
    2. Updating Subscription Management repositories.
    3. Unable to read consumer identity
    4. This system is not registered with an entitlement server. You can use subscription-manager to register.
    5. Repository AppStream is listed more than once in the configuration
    6. Zabbix Official Repository - x86_64 25 kB/s | 107 kB 00:04
    7. Zabbix Official Repository non-supported - x86_ 294 B/s | 1.2 kB 00:04
    8. epel-release-latest-8.noarch.rpm 33 kB/s | 24 kB 00:00
    9. Dependencies resolved.
    10. ================================================================================
    11. Package Architecture Version Repository Size
    12. ================================================================================
    13. Installing:
    14. epel-release noarch 8-16.el8 @commandline 24 k
    15. Transaction Summary
    16. ================================================================================
    17. Install 1 Package
    18. Total size: 24 k
    19. Installed size: 34 k
    20. Downloading Packages:
    21. Running transaction check
    22. Transaction check succeeded.
    23. Running transaction test
    24. Transaction test succeeded.
    25. Running transaction
    26. Preparing : 1/1
    27. Installing : epel-release-8-16.el8.noarch 1/1
    28. Running scriptlet: epel-release-8-16.el8.noarch 1/1
    29. Many EPEL packages require the CodeReady Builder (CRB) repository.
    30. It is recommended that you run /usr/bin/crb enable to enable the CRB repository.
    31. Verifying : epel-release-8-16.el8.noarch 1/1
    32. Installed products updated.
    33. Installed:
    34. epel-release-8-16.el8.noarch
    35. Complete!

    2. 下载ansible

    1. [root@rhcsa yum.repos.d]# yum install ansible -y
    2. Updating Subscription Management repositories.
    3. Unable to read consumer identity
    4. This system is not registered with an entitlement server. You can use subscription-manager to register.
    5. Repository AppStream is listed more than once in the configuration
    6. Extra Packages for Enterprise Linux 8 - x86_64 248 kB/s | 13 MB 00:52
    7. Extra Packages for Enterprise Linux Modular 8 - 211 kB/s | 1.0 MB 00:04
    8. Last metadata expiration check: 0:00:03 ago on Tue 02 Aug 2022 05:25:07 PM CST.
    9. Dependencies resolved.
    10. ================================================================================
    11. Package Arch Version Repo Size
    12. ================================================================================
    13. Installing:
    14. ansible noarch 5.4.0-3.el8 epel 40 M
    15. Installing dependencies:
    16. ansible-core x86_64 2.12.7-1.el8 AppStream 2.5 M
    17. python38 x86_64 3.8.13-1.module_el8.7.0+1177+19c53253 AppStream 80 k
    18. python38-asn1crypto
    19. noarch 1.2.0-3.module_el8.5.0+742+dbad1979 AppStream 184 k
    20. python38-babel noarch 2.7.0-11.module_el8.6.0+929+89303463 AppStream 5.9 M
    21. python38-cffi x86_64 1.13.2-3.module_el8.5.0+742+dbad1979 AppStream 248 k
    22. python38-cryptography
    23. x86_64 2.8-3.module_el8.5.0+742+dbad1979 AppStream 553 k
    24. python38-idna noarch 2.8-6.module_el8.5.0+742+dbad1979 AppStream 87 k
    25. python38-jinja2 noarch 2.11.3-1.module_el8.7.0+1171+3bbf18c8 AppStream 248 k
    26. python38-libs x86_64 3.8.13-1.module_el8.7.0+1177+19c53253 AppStream 8.3 M
    27. python38-markupsafe
    28. x86_64 1.1.1-6.module_el8.5.0+742+dbad1979 AppStream 36 k
    29. python38-pip-wheel
    30. noarch 19.3.1-6.module_el8.7.0+1184+30eba247 AppStream 1.0 M
    31. python38-ply noarch 3.11-10.module_el8.5.0+742+dbad1979 AppStream 112 k
    32. python38-pycparser
    33. noarch 2.19-3.module_el8.5.0+742+dbad1979 AppStream 128 k
    34. python38-pytz noarch 2019.3-3.module_el8.5.0+742+dbad1979 AppStream 55 k
    35. python38-pyyaml x86_64 5.4.1-1.module_el8.6.0+929+89303463 AppStream 212 k
    36. python38-resolvelib
    37. noarch 0.5.4-5.el8 AppStream 30 k
    38. python38-setuptools
    39. noarch 41.6.0-5.module_el8.6.0+929+89303463 AppStream 667 k
    40. python38-setuptools-wheel
    41. noarch 41.6.0-5.module_el8.6.0+929+89303463 AppStream 304 k
    42. python38-six noarch 1.12.0-10.module_el8.5.0+742+dbad1979 AppStream 39 k
    43. sshpass x86_64 1.09-4.el8 AppStream 30 k
    44. Installing weak dependencies:
    45. python3-jmespath noarch 0.9.0-11.el8 AppStream 45 k
    46. python38-pip noarch 19.3.1-6.module_el8.7.0+1184+30eba247 AppStream 1.8 M
    47. Enabling module streams:
    48. python38 3.8
    49. Transaction Summary
    50. ================================================================================
    51. Install 23 Packages
    52. Total download size: 63 M
    53. Installed size: 389 M
    54. Downloading Packages:
    55. (1/23): python3-jmespath-0.9.0-11.el8.noarch.rp 20 kB/s | 45 kB 00:02
    56. (2/23): python38-3.8.13-1.module_el8.7.0+1177+1 34 kB/s | 80 kB 00:02
    57. (3/23): python38-asn1crypto-1.2.0-3.module_el8. 132 kB/s | 184 kB 00:01
    58. (4/23): python38-cffi-1.13.2-3.module_el8.5.0+7 142 kB/s | 248 kB 00:01
    59. (5/23): python38-cryptography-2.8-3.module_el8. 173 kB/s | 553 kB 00:03
    60. (6/23): python38-idna-2.8-6.module_el8.5.0+742+ 72 kB/s | 87 kB 00:01
    61. (7/23): python38-jinja2-2.11.3-1.module_el8.7.0 180 kB/s | 248 kB 00:01
    62. (8/23): ansible-core-2.12.7-1.el8.x86_64.rpm 146 kB/s | 2.5 MB 00:17
    63. (9/23): python38-markupsafe-1.1.1-6.module_el8. 129 kB/s | 36 kB 00:00
    64. (10/23): python38-pip-19.3.1-6.module_el8.7.0+1 169 kB/s | 1.8 MB 00:10
    65. (11/23): python38-pip-wheel-19.3.1-6.module_el8 170 kB/s | 1.0 MB 00:06
    66. (12/23): python38-ply-3.11-10.module_el8.5.0+74 144 kB/s | 112 kB 00:00
    67. (13/23): python38-pycparser-2.19-3.module_el8.5 147 kB/s | 128 kB 00:00
    68. (14/23): python38-pytz-2019.3-3.module_el8.5.0+ 95 kB/s | 55 kB 00:00
    69. (15/23): python38-babel-2.7.0-11.module_el8.6.0 174 kB/s | 5.9 MB 00:34
    70. (16/23): python38-resolvelib-0.5.4-5.el8.noarch 98 kB/s | 30 kB 00:00
    71. (17/23): python38-pyyaml-5.4.1-1.module_el8.6.0 187 kB/s | 212 kB 00:01
    72. (18/23): python38-setuptools-wheel-41.6.0-5.mod 183 kB/s | 304 kB 00:01
    73. (19/23): python38-six-1.12.0-10.module_el8.5.0+ 112 kB/s | 39 kB 00:00
    74. (20/23): sshpass-1.09-4.el8.x86_64.rpm 104 kB/s | 30 kB 00:00
    75. (21/23): python38-setuptools-41.6.0-5.module_el 185 kB/s | 667 kB 00:03
    76. (22/23): python38-libs-3.8.13-1.module_el8.7.0+ 175 kB/s | 8.3 MB 00:48
    77. (23/23): ansible-5.4.0-3.el8.noarch.rpm 239 kB/s | 40 MB 02:52
    78. --------------------------------------------------------------------------------
    79. Total 301 kB/s | 63 MB 03:33
    80. CentOS-8-stream - AppStream - mirrors.aliyun.co 6.6 kB/s | 1.6 kB 00:00
    81. Importing GPG key 0x8483C65D:
    82. Userid : "CentOS (CentOS Official Signing Key) "
    83. Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
    84. From : https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
    85. Key imported successfully
    86. Extra Packages for Enterprise Linux 8 - x86_64 1.6 MB/s | 1.6 kB 00:00
    87. Importing GPG key 0x2F86D6A1:
    88. Userid : "Fedora EPEL (8) "
    89. Fingerprint: 94E2 79EB 8D8F 25B2 1810 ADF1 21EA 45AB 2F86 D6A1
    90. From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
    91. Key imported successfully
    92. Running transaction check
    93. Transaction check succeeded.
    94. Running transaction test
    95. Transaction test succeeded.
    96. Running transaction
    97. Preparing : 1/1
    98. Installing : sshpass-1.09-4.el8.x86_64 1/23
    99. Installing : python38-setuptools-wheel-41.6.0-5.module_el8.6.0+ 2/23
    100. Installing : python38-pip-wheel-19.3.1-6.module_el8.7.0+1184+30 3/23
    101. Installing : python38-libs-3.8.13-1.module_el8.7.0+1177+19c5325 4/23
    102. Installing : python38-3.8.13-1.module_el8.7.0+1177+19c53253.x86 5/23
    103. Running scriptlet: python38-3.8.13-1.module_el8.7.0+1177+19c53253.x86 5/23
    104. Installing : python38-setuptools-41.6.0-5.module_el8.6.0+929+89 6/23
    105. Running scriptlet: python38-setuptools-41.6.0-5.module_el8.6.0+929+89 6/23
    106. Installing : python38-pip-19.3.1-6.module_el8.7.0+1184+30eba247 7/23
    107. Running scriptlet: python38-pip-19.3.1-6.module_el8.7.0+1184+30eba247 7/23
    108. Installing : python38-six-1.12.0-10.module_el8.5.0+742+dbad1979 8/23
    109. Installing : python38-asn1crypto-1.2.0-3.module_el8.5.0+742+dba 9/23
    110. Installing : python38-idna-2.8-6.module_el8.5.0+742+dbad1979.no 10/23
    111. Installing : python38-markupsafe-1.1.1-6.module_el8.5.0+742+dba 11/23
    112. Installing : python38-ply-3.11-10.module_el8.5.0+742+dbad1979.n 12/23
    113. Installing : python38-pycparser-2.19-3.module_el8.5.0+742+dbad1 13/23
    114. Installing : python38-cffi-1.13.2-3.module_el8.5.0+742+dbad1979 14/23
    115. Installing : python38-cryptography-2.8-3.module_el8.5.0+742+dba 15/23
    116. Installing : python38-pytz-2019.3-3.module_el8.5.0+742+dbad1979 16/23
    117. Installing : python38-babel-2.7.0-11.module_el8.6.0+929+8930346 17/23
    118. Installing : python38-jinja2-2.11.3-1.module_el8.7.0+1171+3bbf1 18/23
    119. Installing : python38-pyyaml-5.4.1-1.module_el8.6.0+929+8930346 19/23
    120. Installing : python38-resolvelib-0.5.4-5.el8.noarch 20/23
    121. Installing : ansible-core-2.12.7-1.el8.x86_64 21/23
    122. Installing : python3-jmespath-0.9.0-11.el8.noarch 22/23
    123. Installing : ansible-5.4.0-3.el8.noarch 23/23
    124. Running scriptlet: ansible-5.4.0-3.el8.noarch 23/23
    125. Verifying : ansible-core-2.12.7-1.el8.x86_64 1/23
    126. Verifying : python3-jmespath-0.9.0-11.el8.noarch 2/23
    127. Verifying : python38-3.8.13-1.module_el8.7.0+1177+19c53253.x86 3/23
    128. Verifying : python38-asn1crypto-1.2.0-3.module_el8.5.0+742+dba 4/23
    129. Verifying : python38-babel-2.7.0-11.module_el8.6.0+929+8930346 5/23
    130. Verifying : python38-cffi-1.13.2-3.module_el8.5.0+742+dbad1979 6/23
    131. Verifying : python38-cryptography-2.8-3.module_el8.5.0+742+dba 7/23
    132. Verifying : python38-idna-2.8-6.module_el8.5.0+742+dbad1979.no 8/23
    133. Verifying : python38-jinja2-2.11.3-1.module_el8.7.0+1171+3bbf1 9/23
    134. Verifying : python38-libs-3.8.13-1.module_el8.7.0+1177+19c5325 10/23
    135. Verifying : python38-markupsafe-1.1.1-6.module_el8.5.0+742+dba 11/23
    136. Verifying : python38-pip-19.3.1-6.module_el8.7.0+1184+30eba247 12/23
    137. Verifying : python38-pip-wheel-19.3.1-6.module_el8.7.0+1184+30 13/23
    138. Verifying : python38-ply-3.11-10.module_el8.5.0+742+dbad1979.n 14/23
    139. Verifying : python38-pycparser-2.19-3.module_el8.5.0+742+dbad1 15/23
    140. Verifying : python38-pytz-2019.3-3.module_el8.5.0+742+dbad1979 16/23
    141. Verifying : python38-pyyaml-5.4.1-1.module_el8.6.0+929+8930346 17/23
    142. Verifying : python38-resolvelib-0.5.4-5.el8.noarch 18/23
    143. Verifying : python38-setuptools-41.6.0-5.module_el8.6.0+929+89 19/23
    144. Verifying : python38-setuptools-wheel-41.6.0-5.module_el8.6.0+ 20/23
    145. Verifying : python38-six-1.12.0-10.module_el8.5.0+742+dbad1979 21/23
    146. Verifying : sshpass-1.09-4.el8.x86_64 22/23
    147. Verifying : ansible-5.4.0-3.el8.noarch 23/23
    148. Installed products updated.
    149. Installed:
    150. ansible-5.4.0-3.el8.noarch
    151. ansible-core-2.12.7-1.el8.x86_64
    152. python3-jmespath-0.9.0-11.el8.noarch
    153. python38-3.8.13-1.module_el8.7.0+1177+19c53253.x86_64
    154. python38-asn1crypto-1.2.0-3.module_el8.5.0+742+dbad1979.noarch
    155. python38-babel-2.7.0-11.module_el8.6.0+929+89303463.noarch
    156. python38-cffi-1.13.2-3.module_el8.5.0+742+dbad1979.x86_64
    157. python38-cryptography-2.8-3.module_el8.5.0+742+dbad1979.x86_64
    158. python38-idna-2.8-6.module_el8.5.0+742+dbad1979.noarch
    159. python38-jinja2-2.11.3-1.module_el8.7.0+1171+3bbf18c8.noarch
    160. python38-libs-3.8.13-1.module_el8.7.0+1177+19c53253.x86_64
    161. python38-markupsafe-1.1.1-6.module_el8.5.0+742+dbad1979.x86_64
    162. python38-pip-19.3.1-6.module_el8.7.0+1184+30eba247.noarch
    163. python38-pip-wheel-19.3.1-6.module_el8.7.0+1184+30eba247.noarch
    164. python38-ply-3.11-10.module_el8.5.0+742+dbad1979.noarch
    165. python38-pycparser-2.19-3.module_el8.5.0+742+dbad1979.noarch
    166. python38-pytz-2019.3-3.module_el8.5.0+742+dbad1979.noarch
    167. python38-pyyaml-5.4.1-1.module_el8.6.0+929+89303463.x86_64
    168. python38-resolvelib-0.5.4-5.el8.noarch
    169. python38-setuptools-41.6.0-5.module_el8.6.0+929+89303463.noarch
    170. python38-setuptools-wheel-41.6.0-5.module_el8.6.0+929+89303463.noarch
    171. python38-six-1.12.0-10.module_el8.5.0+742+dbad1979.noarch
    172. sshpass-1.09-4.el8.x86_64
    173. Complete!

    3.使用ansible --version去验证

    1. [root@rhcsa yum.repos.d]# ansible --version
    2. ansible [core 2.12.7]
    3. config file = /etc/ansible/ansible.cfg
    4. configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
    5. ansible python module location = /usr/lib/python3.8/site-packages/ansible
    6. ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
    7. executable location = /usr/bin/ansible
    8. python version = 3.8.13 (default, Jun 24 2022, 15:27:57) [GCC 8.5.0 20210514 (Red Hat 8.5.0-13)]
    9. jinja version = 2.11.3
    10. libyaml = True

    配置清单

    /etc/ansible/hosts
    [node]
    rhce
    rhel

    [root@rhcsa yum.repos.d]# vim /etc/ansible/hosts 


    使用ansible node -m ping去验证

     

     

  • 相关阅读:
    PHP写一个 电商Api接口需要注意哪些?考虑哪些?
    【C++11保姆级教程】深入浅出异常说明符、异常操作符和lambda表达式
    StatefulSet:有状态应用部署
    【electron】判断当前进程是否是开发环境的五种方案(获取一些资源的路径)
    LeetCode每日一题(2161. Partition Array According to Given Pivot)
    【毕业设计源码】基于JAVA的企业人事管理系统
    8、自定义映射resultMap
    临时关闭Pcie ACS 重定向服务
    网络精通-VLAN的高级配置之基于IP地址划分VLAN
    VisualStudio运行程序,点击应用程序时,弹出多个个窗体问题
  • 原文地址:https://blog.csdn.net/weixin_64051859/article/details/126126267