Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

packer + ansible, how to specify the inventory file

When I use vagrant, I can specify the inventory file in the Vagrantfile.

Example:

config.vm.provision "ansible" do |ansible|
    ansible.playbook = "my_folder/playbook.yml"
    ansible.inventory_path = "my_folder/inventory_file"
end

How can I configure the inventory_file when I use packer?

I can't see any related info in the official documentation of packer:

http://www.packer.io/docs/provisioners/ansible-local.html

I need specify the inventory file, because, when I run my packer.json, this is the output:

digitalocean: Executing Ansible: ansible-playbook /tmp/my_folder/playbook.yml -c local -i "127.0.0.1,"
digitalocean:
digitalocean: PLAY [foo] ******************************************************************
digitalocean: skipping: no hosts matched

Packer seems using a inventory file named "127.0.0.1,", and I don't know why.

How can I specify my inventory file?

Thanks!

like image 564
fj123x Avatar asked Feb 23 '14 09:02

fj123x


2 Answers

Well, my guess is that since ansible with packer uses a local connection (-c local), there is not need to provide an inventory file.

You just have to make sure that in your playbook, your -hosts line(s) contains all or 127.0.0.1 and it should run ok.

like image 177
leucos Avatar answered Sep 19 '22 22:09

leucos


Setting hosts: all is fine if the playbook is used exclusively for your Packer build.

If you're re-using a playbook that also needs to run against hosts specified in an inventory file, for example:

[web_servers]
web1.example.com
web2.example.com

[db_servers]
db.example.com

...then you can add the inventory_groups attribute to your ansible-local provisioner, and Packer will assign localhost to those group(s):

"provisioners": [ 
  { "type": "file", "source": "my_folder", "destination": "/tmp/" }, 
  { "type": "shell", "script": "provisioner.sh" }, 
  { 
    "type": "ansible-local", 
    "staging_directory": "/tmp/my_folder",
    "playbook_file": "my_folder/playbook.yml",
    "inventory_groups": "web_servers"
  }
]

For more details, see the Optional sub-section of the Packer doc for ansible-local provisioner Configuration Reference

like image 32
thinkmassive Avatar answered Sep 19 '22 22:09

thinkmassive