Where to store Ansible host file on Mac OS X

后端 未结 6 1265
暗喜
暗喜 2021-01-29 23:12

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

According to the documentation the should be stor

6条回答
  •  不思量自难忘°
    2021-01-30 00:10

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

    • 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

    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.

    Let's go for the Vagrant ansible provisionner first :

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

    Vagrantfile:

    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
    

    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.

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

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

    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} You can find an more extensive example mixing vagrant and ansible here {/shameless_plug}

    Good luck !

提交回复
热议问题