2015-09-09 105 views
6

爲了測試Ansible設置的主機名,我已經設置了,一個流浪VM可與vagrant provision被提供在Vagrantfile給出爲流浪VM在Ansible

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

。這工作時,我設置hostsall

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

或者,文件

.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory 

這是由流浪本身產生的說

default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 

這意味着流浪虛擬機的名稱是default。因此,

- hosts: default 

也做我想要的。但是,我想爲VM提供更具體的名稱(例如,vagrant)。

有沒有辦法將該名稱更改爲其他名稱?

回答

8

訣竅是限定的VM(在這裏與2級的VM productionstaging):

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 

然後流浪漢生成以下清單:

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

參見this answer

1

您可以從流浪漢DOC

閱讀The inventory file如果要更改虛擬機的名稱,你可以有像

Vagrant.configure("2") do |config| 
    config.vm.define "host1" 
    config.vm.provision "ansible" do |ansible| 
    ansible.playbook = "site.yml" 

這將產生清單文件

# Generated by Vagrant 

host1 ansible_ssh_host=... 

請注意,流浪者還提出了一個static inventory選項,這將允許您編寫自己的庫存文件和參考inventory_path選項

+0

這僅只是改變了VM中的'hostname'的'不是內容。vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory',因此不是Ansible提及的名稱。 –

3

要改變僅庫存的主機名.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory,使用:

config.vm.define "myvm" 

這將產生如下:

# Generated by Vagrant 
myvm ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 

因此,ansible​​將成爲myvm

如果你想改變機器的主機名,用途:

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