SSH into a Vagrant machine with Ansible

后端 未结 2 779
南笙
南笙 2021-02-01 21:02

Normally, you can ssh into a Vagrant-managed VM with vagrant ssh. There are two options:

  1. You can use an insecure_private_key generated by
2条回答
  •  -上瘾入骨i
    2021-02-01 21:15

    I think that you should try using the inventory generated by vagrant. This will save you from having to maintain an Ansible inventory in addition to your Vagrantfile.

    For example, you should find an inventory like this used for vagrant ssh:

    cat .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
    >>>
    # Generated by Vagrant
    
    default ansible_host=127.0.0.1 ansible_port=2222 ansible_user='vagrant' ansible_ssh_private_key_file='/home/someone/coding-in-a-project/.vagrant/machines/default/virtualbox/private_key'
    

    You will be able to run ansible ad-hoc commands and ansible-playbook commands. (specify this maybe for your needs : --private-key=~/.ssh/your_private_key)

    ansible default -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory -m ansible.builtin.shell -a 'echo foobar'
    
    ansible-playbook -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory playbook.yml
    

    source : https://docs.ansible.com/ansible/latest/scenario_guides/guide_vagrant.html

提交回复
热议问题