1. Batch Configuration of SSH Passwordless Login
-
The prerequisite for this operation is to generate a public key on the management machine:
# Execute on the management machine (the machine with Ansible installed) to generate RSA key pairssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa
Add IP addresses and passwords in the relevant files:
-
Modify the hosts file
# Add the target server's IP in /etc/ansible/hosts; the format is as follows:[kylin_servers]192.168.88.xxx192.168.88.xxx
-
Add passwd.yml file
# Create a file in the /opt/ansible folder, pointing to192.168.88.xxx:ansible_ssh_pass: "zzzz"192.168.88.xxx:ansible_ssh_pass: "xxx!"
- Push the public key from the management machine to the target servers:
ansible all -m authorized_key -a "user=root key='{{ lookup('file', '~/.ssh/id_rsa.pub') }}'" -e "/opt/ansible-offline/passwd.yml"
2. Issues Encountered When Connecting to Target Servers in Bulk
-
Encountered a read failure issue when editing the passwd.yml file with vim
-
Solution: Since YAML files have strict formatting requirements, when editing with vim, do not add a space before ansible_ssh_pass; simply press enter on the previous line.
192.168.88.xxx:ansible-ssh-pass: "xxx!"192.168.88.xxx:ansible-ssh-pass: "xxxx!"
Note: There should be a space before the password.
-
The successful output is as follows:
192.168.xxx.xxx| SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3.7" }, "changed": false, "comment": null, "exclusive": false, "follow": false, "key": "ssh-rsa , "key_options": null, "keyfile": "/root/.ssh/authorized_keys", "manage_dir": true, "path": null, "state": "present", "user": "root", "validate_certs": true}
3. Testing:
ssh [email protected] # Passwordless login should workip a # Check current IP