要使用Ansible和密钥库,你可以按照以下步骤操作:
ansible --version
inventory.ini
的Ansible inventory文件,并将目标主机的IP地址或主机名添加到该文件中:[hosts]
192.168.1.100
playbook.yml
的Ansible playbook文件,并在其中定义你的任务。以下是一个简单的示例:---
- name: Configure SSH keys
hosts: hosts
become: true
tasks:
- name: Install sshpass
apt:
name: sshpass
state: present
- name: Copy SSH key to remote host
shell: sshpass -p "{{ ssh_password }}" ssh-copy-id -o StrictHostKeyChecking=no "{{ ssh_user }}"@"{{ inventory_hostname }}"
vars:
ssh_password: "your_ssh_password"
ssh_user: "your_ssh_user"
在上面的示例中,我们首先安装了sshpass
工具,然后使用ssh-copy-id
命令将SSH密钥复制到远程主机。你需要将your_ssh_password
和your_ssh_user
替换为实际的SSH密码和用户名。
ansible.cfg
的Ansible配置文件,并在其中添加以下内容:[defaults]
private_key_file = /path/to/your/private_key.pem
将/path/to/your/private_key.pem
替换为实际的私钥文件路径。
ansible-playbook -i inventory.ini playbook.yml
这样,Ansible将使用密钥库中的私钥来与远程主机进行身份验证和通信。