Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set host name for Vagrant VM in Ansible

For testing Ansible, I've set up a Vagrant VM which can be provisioned with vagrant provision, given

config.vm.provision "ansible" do |ansible|
  ansible.playbook = "site.yml"
end

in the Vagrantfile. This works when I set hosts to all,

- hosts: all
  sudo: true
  roles:
    - common
    - ssl
    - webserver

Alternatively, the file

.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory

which is generated by Vagrant itself says

default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222

meaning that the name of the Vagrant VM is default. Hence,

- hosts: default

also does what I want. However, I'd like to have a more specific name for the VM (like vagrant, for example).

Is there a way to change that name to something else?

like image 265
Nico Schlömer Avatar asked Sep 09 '15 15:09

Nico Schlömer


1 Answers

The trick is to define the VM (here with 2 VMs production and staging):

config.vm.define "production" do |production|
  production.vm.hostname = "production.example.com"
end

config.vm.define "staging" do |staging|
  staging.vm.hostname = "staging.example.com"
end

Then vagrant generates the following inventory:

production ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222
staging ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200

See also this answer.

like image 159
andrenarchy Avatar answered Sep 17 '22 07:09

andrenarchy