安装 Ansible

在 Ansible 控制机上安装 elel-release 软件源

sudo dnf install -y epel-release

在 Ansible 控制机上安装 ansible

sudo dnf install -y ansible

配置并验证 Ansible

临时开启 Ansible 控制机和被控机的SSH 密码认证

sudo vim /etc/ssh/sshd_config

#PermitEmptyPasswords no
PasswordAuthentication yes

重新加载 Ansible 控制机和被控机的SSH 服务,使密码认证生效

sudo systemctl reload sshd

在 Ansible 控制机上配置主机清单

sudo vim /etc/ansible/hosts

[test]
192.168.72.100
192.168.72.101

在 Ansible 控制机上生成密钥对

[vagrant@test1 ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa): y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

在 Ansible 控制机上,把本机 SSH 公钥拷贝到被控机

[vagrant@test1 ~]$ ssh-copy-id 192.168.72.101
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/vagrant/.ssh/id_rsa.pub"
The authenticity of host '192.168.72.101 (192.168.72.101)' can't be established.
ECDSA key fingerprint is SHA256:oJie199+0OYUeqaozlM0+Pw+kApU9tXTrvL12P64xvE.
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
vagrant@192.168.72.101's password:

Number of key(s) added: 1

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

在 Ansible 控制机上,使用 ping 模块验证被控机是否被管理:

返回 pong(SUCCESS):全部验证通过,主机可正常被 Ansible 管理。
UNREACHABLE:网络不通、22 端口被挡、SSH 认证失败。
FAILED:能连上 SSH,但被控端缺少 Python,无法执行模块。

[vagrant@test1 ~]$ ansible all -m ping
192.168.72.100 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.72.101 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
    Logo

    汇聚全球AI编程工具,助力开发者即刻编程。

    更多推荐