inventory 文件host配置,小戳,配置的明文密码;关于加密可查看这篇blog
wfq@ubuntu:/etc/ansible$ cat hosts
[asb]
ubuntu132 ansible_ssh_host=192.168.174.132 ansible_ssh_user=wfq ansible_ssh_pass=wfq
ubuntu133 ansible_ssh_host=192.168.174.133 ansible_ssh_user=wfq ansible_ssh_pass=wfq
ubuntu134 ansible_ssh_host=192.168.174.134 ansible_ssh_user=wfq ansible_ssh_pass=wfq
1. 使用adhoc命令方式
ansible asb -m authorized_key -a "user=wfq key='{{ lookup('file','/home/wfq/.ssh/id_rsa.pub') }}'"
若首次配置的话,要先将/etc/ansible/ansible.cfg
配置文件的host_key_checking设置为false,避免本机的~/.ssh/known_hosts
文件中并有fingerprint key串的校验而导致报错失败;如文章详细说明
2. 使用ansible-playbook
ansible 官方说该模块说明
playbook文件为:/etc/ansible/playbook/config_no_psw.yml
---
- hosts: "asb"
gather_facts: false
tasks:
- name: config no password
authorized_key:
user: wfq
state: present
key: "{{ lookup('file', '/home/wfq/.ssh/id_rsa.pub') }}"
执行及结果
wfq@ubuntu:/etc/ansible/playbook$ ansible-playbook -i /etc/ansible/hosts /etc/ansible/playbook/config_no_psw.yml
PLAY [asb] ********************************************************************
TASK: [config no password] ****************************************************
changed: [ubuntu134]
ok: [ubuntu132]
changed: [ubuntu133]
PLAY RECAP ********************************************************************
ubuntu132 : ok=1 changed=0 unreachable=0 failed=0
ubuntu133 : ok=1 changed=1 unreachable=0 failed=0
ubuntu134 : ok=1 changed=1 unreachable=0 failed=0