Ansible - Simulating hostname based configuration

21 views
Skip to first unread message

Joaquin Menchaca

unread,
Aug 5, 2015, 10:23:05 PM8/5/15
to Vagrant
I would have to do something like this:

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "chef/centos-6.6" # Get CentOS6 From Somewhere
  config.vm.hostname = "centos6box" # Give System a Hostname

  # Retreive and Configure Default SSH host port
  ssh_port = open('../../provisioning/vagrant.ini').grep(/#{config.vm.hostname}/)[0].split(/[=\s]/)[4]
  config.vm.network :forwarded_port, guest: 22, host: ssh_port, id: "ssh"

  # Provision System
  config.vm.provision :ansible do |ansible|
     ansible.limit = config.vm.hostname
     ansible.playbook = "../../provisioning/playbook.yml"
     ansible.inventory_path = "../../provisioning/vagrant.ini"
     ansible.sudo = true
  end
end


My vagrant.ini looks like this.

[scriptbox]
centos6box ansible_ssh_host=127.0.0.1 ansible_ssh_port=3222 ansible_ssh_private_key_file=vagrants/centos6/.vagrant/machines/default/virtualbox/private_key
wheezybox  ansible_ssh_host=127.0.0.1 ansible_ssh_port=3223 ansible_ssh_private_key_file=vagrants/wheezy/.vagrant/machines/default/virtualbox/private_key
precisebox ansible_ssh_host=127.0.0.1 ansible_ssh_port=3224 ansible_ssh_private_key_file=vagrants/precise/.vagrant/machines/default/virtualbox/private_key


And my directory structure.

.
├── README.md
├── provisioning
│   ├── inventory
│   ├── playbook.yml
│   ├── tasks
│   │   ├── Debian.yml
│   │   └── RedHat.yml
│   ├── vagrant.ini
│   └── vars
│       └── main.yml
└── vagrants
    ├── centos6
    │   └── Vagrantfile
    ├── precise
    │   └── Vagrantfile
    └── wheezy
        └── Vagrantfile

Reply all
Reply to author
Forward
0 new messages