在 Mac OS X 上存储 Ansible 主机文件的位置 [英] Where to store Ansible host file on Mac OS X

查看:25
本文介绍了在 Mac OS X 上存储 Ansible 主机文件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开始使用 Ansible 来配置我的 Vagrantbox,但我不知道如何处理主机文件.

I am trying to get started with Ansible to provision my Vagrantbox, but I can’t figure out how to deal with host files.

根据文档,它应该存储在 /etc/ansible/hosts 中,但我在我的系统 (Mac OS X) 上找不到它.我还看到了 host.ini 文件位于文档根目录中与 vagrant 文件相邻的示例.

According to the documentation the should be storred in /etc/ansible/hosts, but I can’t find this on my system (Mac OS X). I also seen examples where the host.ini file situated in the document root adjacent to the vagrant file.

所以我的问题是,您将用于设置单个 vagrant box 的主机文件存储在哪里?

So my question is where would you store your hostfile for setting up a single vagrant box?

推荐答案

虽然 Ansible 默认会尝试 /etc/ansible/hosts,但有几种方法可以告诉 ansible 在哪里寻找替代方案库存文件:

While Ansible will try /etc/ansible/hosts by default, there are several ways to tell ansible where to look for an alternate inventory file :

  • 使用 -i 命令行开关并传递您的库存文件路径
  • ~/.ansible.cfg[defaults] 部分添加 inventory = path_to_hostfile 配置文件
  • 按照 DomaNitro 在他的回答中的建议使用 export ANSIBLE_HOSTS=path_to_hostfile
  • use the -i command line switch and pass your inventory file path
  • add inventory = path_to_hostfile in the [defaults] section of your ~/.ansible.cfg configuration file
  • use export ANSIBLE_HOSTS=path_to_hostfile as suggested by DomaNitro in his answer

现在您没有提及是否要使用 vagrant 中可用的 ansible 供应商,或者如果你想手动配置你的流浪主机.

Now you don't mention if you want to use the ansible provisionner available in vagrant, or if you want to provision your vagrant host manually.

让我们先去 Vagrant ansible provisionner :

Let's go for the Vagrant ansible provisionner first :

创建一个目录(例如test),并在里面创建一个Vagrant文​​件:

Create a directory (e.g. test), and create a Vagrant file inside :

流浪文件:

Vagrant.configure("2") do |config|

  config.vm.box = "precise64-v1.2"
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"

  config.vm.define :webapp do |webapp|
    webapp.vm.hostname = "webapp.local"
    webapp.vm.network :private_network, ip: "192.168.123.2"
    webapp.vm.provider "virtualbox" do |v|
      v.customize ["modifyvm", :id, "--memory", 200, "--name", "vagrant-docs", "--natdnshostresolver1", "on"]
    end
  end

  # 
  # Provisionning
  #
  config.vm.provision :ansible do |ansible|
    ansible.playbook = "provision.yml"
    ansible.inventory_path = "hosts"
    ansible.sudo = true
    #
    # Use anible.tags if you want to restrict what `vagrant provision`
    # Here is a list of possible tags
    # ansible.tags = "foo bar"
    #
    # Use ansible.verbose to see detailled output for ansible runs
    # ansible.verbose = 'vvv'
    #
    # Customize your stuff here
    ansible.extra_vars = { 
      some_var: 42,
      foo: "bar",
    }
  end
end

现在当你运行 vagrant up(或 vagrant provision)时,Vangrant 的 ansible provionner 会在同一个目录中寻找一个文件名 hosts作为 Vagrantfile,并将尝试应用 provision.yml 剧本.

Now when you run vagrant up (or vagrant provision), Vangrant's ansible provionner will look for a file name hosts in the same directory as Vagrantfile, and will try to apply the provision.yml playbook.

您也可以手动运行它,而无需求助于 Vagrant 的 ansible 配置器:

You can also run it manually, without resorting to Vagrant's ansible provisionner :

ansible-playbook -i hosts provision.yml --ask-pass --sudo

请注意,Vagrant+Virtualbox+Ansible 三人组并不总是相处得很好.有一些版本组合有问题.如果遇到问题(尤其是网络问题),请尝试升级到最新版本.

Note that Vagrant+Virtualbox+Ansible trio does not always get along well. There are some versions combinations that are problematic. Try to upgrade to the latests versions if you experience issues (especially regarding network).

{shameless_plug} 你可以在这里找到一个更广泛的混合 vagrant 和 ansible 的例子 {/shameless_plug}

{shameless_plug} You can find an more extensive example mixing vagrant and ansible here {/shameless_plug}

祝你好运!

这篇关于在 Mac OS X 上存储 Ansible 主机文件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆