I am testing provisioning with ansible locally and using vagrant I am simulate external machine. How to add my own key to vagrant and root user into vagrant box?
1
votes
2 Answers
2
votes
In your vagrant file you can use something like
## Ansible Provisioning
cfg.vm.provision :ansible do |ansible|
ansible.playbook = "vagrant-provision.yml"
## Debugging
ansible.verbose = true
ansible.verbose="vvvvv"
end
Create file called vagrant-provision.yml in the same dir as your vagrant file. I am assuming your using ubuntu you might want to amend the groups for other systems
---
#
# This playbook deploys your keys to the vagrant
#
- name: Provision my keys
hosts: all
sudo: True
vars:
localuser: "{{ lookup('ENV','USER') }}"
tasks:
- name: Create your local user
user:
name="{{localuser}}"
home="/home/{{localuser}}"
shell="/bin/bash"
append="true"
group="admin"
comment="{{localuser}}"
- name: Putting you authorized_key
authorized_key:
key="{{lookup('file', '~/.ssh/id_rsa.pub')}}"
user="{{localuser}}"
manage_dir=yes
So in that case when the vagrant comes up it will use the the above code to deploy your keys
0
votes
It can be done by mixing "file" and "shell" provisining, eg:
$enable_root_passwordless_ssh_access = <<SCRIPT
#vagrant user has sudo passwordless access on precise32.box
[ -d /root ] || sudo mkdir /root
[ -d /root/.ssh ] || sudo mkdir /root/.ssh
[ -f /tmp/id_rsa.pub ] && sudo mv /tmp/id_rsa.pub /root/.ssh/authorized_keys
sudo chmod 0700 /root/.ssh
sudo chmod 0600 /root/.ssh/authorized_keys
sudo chown root:root /root/.ssh/authorized_keys
SCRIPT
machine.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "/tmp/id_rsa.pub"
machine.vm.provision "shell", inline: $enable_root_passwordless_ssh_access