선 밖에 선 자유인

Ansible ssh key 배포 playbook 본문

IT/Cloud & DevOps

Ansible ssh key 배포 playbook

Hotman 2020. 4. 14. 13:19

ssh-key-deploy.yml

---
- hosts: all
  gather_facts: no
  user: vagrant
  
  tasks:
  - name: ssh-key delete
    connection: local
    shell: rm -f /home/vagrant/.ssh/id_rsa*

  - name: ssh-key making
    connection: local
    command: "ssh-keygen -b 2048 -t rsa -f /home/vagrant/.ssh/id_rsa -q -N ''"
    ignore_errors: yes
    run_once: true

  - name: import id_rsa.pub
    connection: local
    command: "cat /home/vagrant/.ssh/id_rsa.pub"
    register: id_pub
    run_once: true
 
  - name: host key to known hosts
    connection: local
    shell: "ssh-keyscan -H {{ inventory_hostname }} >> ~/.ssh/known_hosts"
 
  - name: .ssh dir
    file:
      path: /home/vagrant/.ssh
      state: directory
      mode: '0700'
 
  - name: auth file making
    file:
      path: /home/vagrant/.ssh/authorized_keys
      state: touch
      mode: '0600'
 
  - name: ssh key deploy
    lineinfile:
      dest: /home/vagrant/.ssh/authorized_keys
      line: "{{ id_pub.stdout }}"

  - name: ssh key deploy
    lineinfile:
      dest: /home/vagrant/.ssh/authorized_keys
      line: "{{ id_pub.stdout }}"

 

** Ubuntu 18.04 의 경우 대상 시스템에 python 이 /usr/bin/python 에 없으면 에러가 발생하기도 함

   간단히 아래와 같은 plyabook으로 링크를 생성해 주고 실행하면 됨

 

  - name: python link

    shell: ln -s /usr/bin/python3.6 /usr/bin/python

 

 

Comments