下面是Ansible的SSH密钥剧本的解决方法,包括一对一,一对多和多对一的示例代码:
- hosts: target_host
tasks:
- name: Generate SSH key pair
ansible.posix.ssh_keypair:
path: /path/to/ssh/key
owner: target_user
state: present
size: 2048
- name: Copy public key to target host
ansible.posix.copy:
src: /path/to/ssh/key.pub
dest: /home/target_user/.ssh/authorized_keys
owner: target_user
group: target_group
mode: '0644'
- hosts: source_host
tasks:
- name: Generate SSH key pair
ansible.posix.ssh_keypair:
path: /path/to/ssh/key
owner: source_user
state: present
size: 2048
- name: Copy public key to target hosts
ansible.posix.copy:
src: /path/to/ssh/key.pub
dest: /home/target_user/.ssh/authorized_keys
owner: target_user
group: target_group
mode: '0644'
delegate_to: "{{ item }}"
loop:
- target_host1
- target_host2
- target_host3
- hosts: target_host
tasks:
- name: Generate SSH key pair
ansible.posix.ssh_keypair:
path: /path/to/ssh/key
owner: target_user
state: present
size: 2048
- name: Copy public keys from source hosts
ansible.posix.copy:
src: /path/to/ssh/key.pub
dest: /home/target_user/.ssh/authorized_keys
owner: target_user
group: target_group
mode: '0644'
delegate_to: "{{ item }}"
loop:
- source_host1
- source_host2
- source_host3
请根据实际情况调整代码示例中的主机名、文件路径和用户信息。