在Ansible中设置Vagrant VM的主机名

Nic*_*mer 7 vagrant ansible vagrantfile

为了测试Ansible,我已经建立了一个流浪VM可与被提供vagrant provision,给予

config.vm.provision "ansible" do |ansible|
  ansible.playbook = "site.yml"
end
Run Code Online (Sandbox Code Playgroud)

在Vagrantfile.当我设置hosts为all,这工作,

- hosts: all
  sudo: true
  roles:
    - common
    - ssl
    - webserver
Run Code Online (Sandbox Code Playgroud)

或者,文件

.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
Run Code Online (Sandbox Code Playgroud)

这是由Vagrant自己说的

default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222
Run Code Online (Sandbox Code Playgroud)

意思是Vagrant VM的名称是 default.因此,

- hosts: default
Run Code Online (Sandbox Code Playgroud)

也做我想要的.但是,我想为VM设置一个更具体的名称(例如vagrant,像).

有没有办法将该名称更改为其他名称?

and*_*chy 9

关键是要定义(2个虚拟机这里的VM production和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
Run Code Online (Sandbox Code Playgroud)

然后vagrant生成以下库存:

production ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222
staging ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200
Run Code Online (Sandbox Code Playgroud)

另见这个答案.


use*_*062 5

要改变只有库存的主机名中.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory,使用:

config.vm.define "myvm"
Run Code Online (Sandbox Code Playgroud)

这将生成以下内容:

# Generated by Vagrant
myvm ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222
Run Code Online (Sandbox Code Playgroud)

因此, ansibleinventory_hostname将成为myvm.

如果你想也改变机器的主机名,使用方法:

config.vm.define "myvm" do |myvm|
  myvm.vm.hostname = "myvm.example.com"
end
Run Code Online (Sandbox Code Playgroud)