Radoslav Panev

SysAdmin and DevOps

Upload ssh key through ansible

23 Jul 2019 » ansible, devops

Upload ssh key through ansible
Create a file named ssh-key-setup.yml in directory name /etc/ansible/playbooks.

---

  - hosts: all

    become: yes

    #To start  ansible-playbook ssh-key-setup.yml -u panev --ask-pass


    tasks:



      - name: Creates destination directory

        file: state=directory mode=0700 dest=/root/.ssh/ 

        #file: state=directory mode=0700 owner=panev group=panev dest=/home/panev/.ssh/ #FOR USERS



      - name: Pushes user's rsa key to root's users box (it's ok if this TASK fails)

        copy: src=~/.ssh/id_rsa.pub dest=/root/.ssh/authorized_keys owner=root mode=0600

        #copy: src=~/.ssh/id_rsa.pub dest=/home/panev/.ssh/authorized_keys owner=panev group=panev mode=0600 #FOR USERS



 #     - name: Set authorized key for user X copying it from current user

 #       authorized_key:

 #         user: panev

 #         state: present

 #         key: ""



      - name: Change SSH port

        lineinfile:

          dest: /etc/ssh/sshd_config

          regexp: "^Port"

          line: "Port 2222"

          state: present


          #Remove root login

 #     - name: Remove root SSH access

 #       lineinfile:

 #        dest: /etc/ssh/sshd_config

 #        regexp: "^PermitRootLogin"

 #        line: "PermitRootLogin no"

 #        state: present



      - name: Remove password SSH access

        lineinfile:

          dest: /etc/ssh/sshd_config

          regexp: "^PasswordAuthentication"

          line: "PasswordAuthentication no"

          state: present


      - name: restart ssh

        service: name=ssh state=restarted

To start ansible-playbook ssh-key-setup.yml -u root –ask-pass

Upload ssh key through ansible