Steps

Normally root user has password based login when we create a new machine, and to work with ansible or in general development work we want to use ssh key instead of password. To do that we can configure ssh key based login using following ways:

Setup Ansible to use ssh password

Setup root user with ssh key based login. To do that we will use this role:

ansible-galaxy install GROG.authorized-key

and install sshpass so that we can run ansible command with password

brew install hudochenkov/sshpass/sshpass

Ref: how-to-install-sshpass-on-mac

and then use this playbook to setup root ssh key in server

Setup ssh key based root login : using Ansible --ask-pass

ansible-playbook -i hosts setup-ssh-root.yml -l server1 -u root --ask-pass

Setup ssh key based root login : using ssh-copy-id

For me sshpass installation didn't work so wasn't able to use --ask-pass param in ansible command so used the ssh-copy-id command as manual step

ssh-copy-id -i  ~/.ssh/id_ed25519.pub root@rack

Now I can ssh using ssh key

ssh -i ~/.ssh/id_ed25519  root@rack

Initialize the server : Using Ansible playbook

Once the root ssh key setup is done, we can run the initial server setup plybook

ansible-playbook -i hosts playbook.yml -l server1 -u root

References