我正在测试使用ansible本地配置并使用vagrant我模拟外部机器。如何将我自己的密钥添加到vagrant和root用户?
答案 0 :(得分:2)
在您的vagrant文件中,您可以使用类似
的内容 ## Ansible Provisioning
cfg.vm.provision :ansible do |ansible|
ansible.playbook = "vagrant-provision.yml"
## Debugging
ansible.verbose = true
ansible.verbose="vvvvv"
end
在与您的vagrant文件相同的目录中创建名为 vagrant-provision.yml 的文件。我假设你使用ubuntu你可能想修改其他系统的组
---
#
# 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
因此,在这种情况下,当流浪者出现时,它将使用上面的代码来部署你的密钥
答案 1 :(得分:0)
可以通过混合“file”和“shell”来完成,例如:
$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