It would be helpful for writing Ansible playbooks (as one example) if Vagrant set a magic variable to help communicate to Ansible that the host is currently running in a Vagrant environment. This would allow you to have Ansible tasks which are only run against Vagrant hosts.
Eg
- name: Do the thing if Vagrant
apt:
state: latest
name: vim
when: vagrant is true
Current work-arounds include:
1) Use the shell provisioner in Vagrantfile to drop a /.vagrant file before the Ansible provisioner is run
2) Set the variable by hand in the Ansible playbook
I've checked if this is a duplicate issue but it doesn't seem to be.
If there's a better way to accomplish this pls just let me know :)
Vagrant 1.8.1
Ubuntu 16.04.4 LTS
CentOS Linux 7 (Core)
Vagrant.configure(2) do |config|
config.vm.define "consul1" do |consul1|
consul1.vm.box = "centos/7"
end
end
Thanks!
Thank you @jeffWelling for asking, and filling the issue template so precisely 👍
From your Vagrantfile, I suppose that you're not using any of the Vagrant Ansible provisioners. These two provisioners are the only components where Vagrant (without additional plugins) supports some Ansible integration capabilities. By using them, you can easily get your question resolved, as illustrated below. The simplest way is to rely on the extra_vars option (but you also can consider group or host variables).
Example:
Vagrant.configure(2) do |config|
config.vm.define "consul1" do |consul1|
consul1.vm.box = "centos/7"
end
config.vm.provision :ansible do |ansible|
ansible.playbook = "ansible/consul.yml"
ansible.extra_vars = { vagrant: true }
end
end
Then you simply have to run vagrant up --provision or vagrant provision (when the vm is already up).
I am closing this issue. If you have more question, please let's move the discussion to the vagrant mailing list. Thank you 😃
Thanks @gildegoma! I am indeed using the Vagrant Ansible provisioner, I just forgot to include that part in my Vagrantfile sample, my bad! This works perfectly though.
@gildegoma Sorry, for posting into a closed issue, but it describes precisely the feature I'd like to have in Vagrant.
Some background for better understanding. In our team we use Ansible either directly or combined with Vagrant and Packer. Packer Ansible Provisioner provides passes by default some extra_vars, e.g., packer_builder_type, whereas Vagrant Ansible provisioner requires adding such a variable explicitly.
While this is an absolutely viable solution, it requires customizing every Vagrantfile accordingly. Why not let Vagrant Ansible Provisioner supply a default extra var, just like Packer Ansible Provisioner does this?
I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
Thanks @gildegoma! I am indeed using the Vagrant Ansible provisioner, I just forgot to include that part in my Vagrantfile sample, my bad! This works perfectly though.