Ansible

220610_1_앤서블_심화_자동으로 authorized_keys 등록

kimhope 2022. 6. 10. 16:32
728x90

authorized_keys란

- 인증된 키

- 이미 사용 허가를 받아 믿을 수 있는 사용자

 

authorized_keys 적용 과정

1. 앤서블 서버에서 앤서블 노드로 접속 시도

2. 앤서블 노드의 ~/.ssh/authorized_keys 파일의 존재 확인

3. authorized_keys 파일에 해당 호스트의 접속 허가 정보가 저장되어 있다면  접속 허용

 

 

autopass.yml 파일 수정

1) vars: 추가

2) ssh-keygen for authorized_keys file과 input key for each node 부분 추가

  vars:
    ansible_password: vagrant
    - name: ssh-keygen for authorized_keys file
      command: "ssh-keygen -b 2048 -t rsa -f ~/.ssh/id_rsa -q -N ''"
      ignore_errors: yes
      run_once: true

    - name: input key for each node
      connection: ssh
      authorized_key:
        user: vagrant
        state: present
        key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

코드 전체

---
- name: Create authority between server and nodes
  hosts: nodes
  connection: local
  serial: 1
  gather_facts: no
  vars:
    ansible_password: vagrant

  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
      run_once: true

    - name: input key for each node
      connection: ssh
      authorized_key:
        user: vagrant
        state: present
        key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

 

vag_reconf.bat 파일 실행

PS C:\HashiCorp\udemy-ansible> .\vag_reconf.bat

 

 

-k 옵션과 암호 입력 없이 통신 확인 가능

ans nodes -m ping

728x90