Tuesday, April 21, 2015

Ansible + Vagrant: forget your interactive prompts (SOLVED)

If you have a playbook with something like this:

- name: Installing test box
  hosts: all   
  connection: paramiko
  vars_prompt:
     - name: "hosthname"
       hosthname: "Give me a hostname:"
       private: no
  gather_facts: true
  roles:
   - base
   - redisenabled
   - nodebase


 And you are trying to run it with vagrant following this Vagrantfile piece:

  config.vm.define "test" do |test|
     test.vm.box = "chef/centos-6.6"
     test.vm.network "private_network", ip: "10.1.1.13"
     test.vm.provision "ansible" do |ansible|
       ansible.playbook = "ansible/playbooks/test.yml"
       ansible.sudo = true
     end
  end

This var (hosthname) is not interactive in Vagrant, you never will be asked.

What is the trick? I tried this workaround and i liked it:

  • Just in case i would create a default value for the variable.
  • Force the value of the variable in the Vagrantfile

So, the final config files would be:
  • Playbook:
- name: Installing test box
  hosts: all   
  connection: paramiko
  vars_prompt:
     - name: "hosthname"
       hosthname: "Give me a hostname:"
       private: no
       default: "test01-default"
  gather_facts: true
  roles:
   - base
   - redisenabled
   - nodebase

  • Vagrantfile
  config.vm.define "test" do |test|
     test.vm.box = "chef/centos-6.6"
     test.vm.network "private_network", ip: "10.1.1.13"
     test.vm.provision "ansible" do |ansible|
       ansible.playbook = "ansible/playbooks/test.yml"
       ansible.sudo = true
       ansible.extra_vars = {
          hosthname: "test01"
       }
     end
  end



1 comment: