[Playbook] SSH authorized_keys 등록

Ansible 마스터 노드에서 각각의 호스트에 ssh rsa key 전파
authorized_key 모듈을 이용합니다.
root 유저로 할 경우 아래와 같이 진행합니다.
---
- name: Create authority between server and nodes
hosts: ceph
connection: local
serial: 1
gather_facts: no
tasks:
- name: ssh-keyscan for known_hosts file
command: /usr/bin/ssh-keyscan -t ecdsa {{ ansible_host }}
register: keyscan
- name: input key
lineinfile:
path: ~/.ssh/known_hosts
line: "{{ item }}"
create: yes
with_items:
- "{{ keyscan.stdout_lines }}"
- name: ssh-keygen for authorized_keys file
command: "ssh-keygen -b 2048 -t rsa -f ~/.ssh/id_rsa -q -N ''"
ignore_errors: yes
- name: Set authorized key taken from file
authorized_key:
user: root
state: present
key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
$ ansible-playbook -i inventory add_sshauthkey.yml -k
root 유저가 아닐 경우 맨 아랫줄 /root/.ssh/id_rsa.pub 를 ~/.ssh/id_rsa.pub 로 바꿔 줍니다.
인벤토리 파일에 ip만 추가해주면 몇번이든 다시 돌려도 키 값이 없는 노드에만 rsa키를 등록합니다.
처음 rsa키를 넣어주는 작업이기 때문에 -k 옵션을 넣어서 암호를 넣어줘야 ssh 접속이 가능합니다.

최신 댓글