Please, don’t use passwords…

Assuming you have a user called “admin” which can access with password and become “root” using SUDO

  • Create a playbook containing your SSH public key. Note the “remote_user” variable.
hosts: all
  gather_facts: yes
  remote_user: admin
  - name: check if key is authorized
      dest: /root/.ssh/authorized_keys
      backup: yes
      state: present
      line: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCb0i6nWnYTC13YTi/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  • Run using the parameters: “-k”, ask for password; “-b”, become another user; “-K”, ask for SUDO password.
$ ansible-playbook copy_key.yml -k -b -K
SSH password: 
SUDO password[defaults to SSH password]: