部署ansible架构:

部署ansible架构:

ansible主机和3台受控节点,使用普通用户student进行测试 ansible all -m ping

安装yum源

root@ansible ~]# cd /etc/yum.repos.d/
[root@ansible yum.repos.d]# rm -rf *
[root@ansible yum.repos.d]#  dnf -y install centos-release-ansible-29-1- 2.el8.noarch
[root@ansible yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@ansible yum.repos.d]#  dnf list all|grep ansible
root@ansible yum.repos.d]#  dnf -y install centos-release-ansible-29-1-2.el8.noarch 
Complete!
[root@ansible yum.repos.d]# ls /etc/yum.repos.d/ |grep ansible
[root@ansible yum.repos.d]# dnf -y install ansible
Complete!

查看ansible版本

[root@ansible yum.repos.d]# ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]

通过使用setup模块验证localhost上的ansible_python_version

[root@ansible yum.repos.d]# ansible -m setup localhost | grep ansible_pyt
        "ansible_python": {
        "ansible_python_version": "3.6.8",

配置/etc/hosts

在ansible节点vim /etc/hosts
[root@ansible ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.112.134 ansible.example.com  ansible
192.168.112.129 node1.example.com  node1
192.168.112.133 node2.example.com  node2
192.168.112.131 node3.example.com  node3

配置ssh的基于密钥认证

在ansible节点执行ssh-keygen命令,一直回车
[root@ansible ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
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:
SHA256:sxbYts5uWA2Nv4Fd1VgCxIQLndNOzk4NwRi6/ckz7hc root@ansible.example.com
The key's randomart image is:
+---[RSA 3072]----+
|         ..@=o.+.|
|        ..* * o..|
|        .+ B +   |
|       oooo * .  |
|      . S*.+     |
|       .o==o..E  |
|       o+  o*  . |
|      .+. .. o.  |
|       o+  .o.   |
+----[SHA256]-----+

[root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@node1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node1 (192.168.112.129)' can't be established.
ECDSA key fingerprint is SHA256:9R1NHSXR+IVDNA5kzG4oMCd8+SYV9V3lZ9q+Ryle/lE.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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
root@node1's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@node1'"
and check to make sure that only the key(s) you wanted were added.

[root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@node2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node2 (192.168.112.133)' can't be established.
ECDSA key fingerprint is SHA256:nyR/cR6LpvgNZwUji0OSFEHahZN+E3yIESfWDTjoi9c.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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
root@node2's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@node2'"
and check to make sure that only the key(s) you wanted were added.

[root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@node3
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node3 (192.168.112.131)' can't be established.
ECDSA key fingerprint is SHA256:Du+5ve7y+/kK9kBqXL7U3jKuAeyxogDYJ4TrgXtdfM0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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
root@node3's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@node3'"
and check to make sure that only the key(s) you wanted were added.

将ansible本地的/etc/hosts文件发送给受控主机

[root@ansible ~]# scp /etc/hosts root@node1:/etc/hosts
hosts                                                               100%  326   138.8KB/s   00:00    
[root@ansible ~]# scp /etc/hosts root@node2:/etc/hosts
hosts                                                               100%  326   151.2KB/s   00:00    
[root@ansible ~]# scp /etc/hosts root@node3:/etc/hosts
hosts                                                               100%  326   123.6KB/s   00:00  

验证能不能链接到node1、node2、node3

[root@ansible ~]#  ssh root@node1
Last login: Thu Oct 20 14:19:32 2022 from 192.168.112.1
[root@node1 ~]# exit
logout
Connection to node1 closed.
[root@ansible ~]# ssh root@node2
Last login: Thu Oct 20 14:19:57 2022 from 192.168.112.1
[root@node2 ~]# exit
logout
Connection to node2 closed.
[root@ansible ~]# ssh root@node3
Last login: Thu Oct 20 14:20:40 2022 from 192.168.112.1
[root@node3 ~]# exit
logout
Connection to node3 closed.

在ansible主机和所有受控主机中创建student用户,并设置密码为redhat

[root@ansible ~]# useradd student
[root@ansible ~]# echo redhat | passwd --stdin student
Changing password for user student.
passwd: all authentication tokens updated successfully.

root@node1 ~]# useradd student
[root@node1 ~]# echo redhat | passwd --stdin student
Changing password for user student.
passwd: all authentication tokens updated successfully.

[root@node2 ~]# echo redhat | passwd --stdin student
Changing password for user student.
passwd: all authentication tokens updated successfully.

[root@node3 ~]# useradd student
[root@node3 ~]# echo redhat | passwd --stdin student
Changing password for user student.
passwd: all authentication tokens updated successfully.

使用student用户创建基于密钥认证

[student@ansible ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/student/.ssh/id_rsa): 
Created directory '/home/student/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/student/.ssh/id_rsa.
Your public key has been saved in /home/student/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:lHG+wNZeGVRgy1E1/VmtiGblim+vnLia9L2NoLuwxPM student@ansible.example.com
The key's randomart image is:
+---[RSA 3072]----+
|        . ..*+oo+|
|       . * o.=  =|
|        * o+=. .+|
|       o o+oo ...|
|        S+o.     |
|   .    . .      |
|    = . ..       |
|   . * + =o+     |
|    . E++oB+o    |
+----[SHA256]-----+

发送到node1、node2、node3的root用户

[student@ansible ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@node1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/student/.ssh/id_rsa.pub"
The authenticity of host 'node1 (192.168.112.129)' can't be established.
ECDSA key fingerprint is SHA256:9R1NHSXR+IVDNA5kzG4oMCd8+SYV9V3lZ9q+Ryle/lE.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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
root@node1's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@node1'"
and check to make sure that only the key(s) you wanted were added.

[student@ansible ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@node2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/student/.ssh/id_rsa.pub"
The authenticity of host 'node2 (192.168.112.133)' can't be established.
ECDSA key fingerprint is SHA256:nyR/cR6LpvgNZwUji0OSFEHahZN+E3yIESfWDTjoi9c.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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
root@node2's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@node2'"
and check to make sure that only the key(s) you wanted were added.

[student@ansible ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@node3
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/student/.ssh/id_rsa.pub"
The authenticity of host 'node3 (192.168.112.131)' can't be established.
ECDSA key fingerprint is SHA256:Du+5ve7y+/kK9kBqXL7U3jKuAeyxogDYJ4TrgXtdfM0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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
root@node3's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@node3'"
and check to make sure that only the key(s) you wanted were added.

发送到node1、node2、node3的student用户

[student@ansible ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub student@node1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/student/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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
student@node1's password: 
Permission denied, please try again.
student@node1's password: 
Permission denied, please try again.
student@node1's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'student@node1'"
and check to make sure that only the key(s) you wanted were added.

[student@ansible ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub student@node2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/student/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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
student@node2's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'student@node2'"
and check to make sure that only the key(s) you wanted were added.

[student@ansible ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub student@node3
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/student/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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
student@node3's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'student@node3'"
and check to make sure that only the key(s) you wanted were added.

验证

[student@ansible ~]$ mkdir ansible
[student@ansible ~]$ cd ansible/
[student@ansible ansible]$ ls
[student@ansible ansible]$ cp /etc/ansible/ansible.cfg .
[student@ansible ansible]$ ls
ansible.cfg
[student@ansible ansible]$ vim ansible.cfg 
inventory               = /home/student/ansible/inventory

列一个清单

[student@ansible ansible]$ cat inventory
node1
node2
node3

检测

[student@ansible ansible]$ ansible all -m ping
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
node3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值