# How to change form ssh username/password to public key encryption in a playbook?

**URL:** <https://forum.ansible.com/t/how-to-change-form-ssh-username-password-to-public-key-encryption-in-a-playbook/33519>\
**Category:** Ansible Project\
**Created:** [July 29, 2020, 11:30am UTC](https://forum.ansible.com/t/how-to-change-form-ssh-username-password-to-public-key-encryption-in-a-playbook/33519 "2020-07-29T11:30:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hans\_IJ](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Hans\_IJ](https://forum.ansible.com/u/Hans_IJ)\
**Post date:** [July 29, 2020, 11:30am UTC](https://forum.ansible.com/t/how-to-change-form-ssh-username-password-to-public-key-encryption-in-a-playbook/33519/1 "2020-07-29T11:30:30Z")

</div>

Hi,

I am a beginner with ansible; I couldn’t find the answer yet.  
I want to bootstrap virgin raspberry pi’s (raspbian based);

It all works fine as intended, except for one step,  
I have to change the ansible hosts file manually half way between two playbooks to make it work.

What I want to fully automated:

1. Start with pi:raspberry account
2. Add a new admin account & add admin’s public key to authorized hosts file
3. Change default password of “pi”
4. Some other stuff, install a few packages
5. reboot
6. Continue configuration via admin account & public key authentication

Question: how can I change midway in a playbook from username:password to public key (with admin account?)  
or…I am fine to split in 2 playbooks, how can I have one playbook with pi:raspberry username/password  
(as it is configured in hosts) and 2nd playbook with with admin:

> > > > > > > > > > > > > > > > > > > > > > >

hosts  
…  
…  
[pi:vars]  
ansible\_connection=ssh

# Disable below after new admin account with public key is created

ansible\_user=pi  
ansible\_ssh\_pass=raspberry

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [July 29, 2020, 11:39am UTC](https://forum.ansible.com/t/how-to-change-form-ssh-username-password-to-public-key-encryption-in-a-playbook/33519/2 "2020-07-29T11:39:16Z")

</div>

> Hi,
> 
> I am a beginner with ansible; I couldn't find the answer yet.  
> I want to bootstrap virgin raspberry pi's (raspbian based);
> 
> It all works fine as intended, except for one step,  
> I have to change the ansible hosts file manually half way between two playbooks to make it work.
> 
> What I want to fully automated:
> 
> 0. Start with pi:raspberry account  
> 1. Add a new admin account & add admin's public key to authorized hosts file  
> 2. Change default password of "pi"  
> 3. Some other stuff, install a few packages  
> 4. reboot  
> 5. Continue configuration via admin account & public key authentication
> 
> Question: how can I change midway in a playbook from username:password to public key (with admin account?)  
> or...I am fine to split in 2 playbooks, how can I have one playbook with pi:raspberry username/password  
> (as it is configured in hosts) and 2nd playbook with with admin:\<public key authentication\>

I suppose you can set these variables through a set\_fact task, e.g.

set\_fact:  
&nbsp;&nbsp;ansible\_user: admin

Please note that these changes only apply to the current play.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
