Vagrant with Ansible

69 views
Skip to first unread message

Ivan Adji-Krstev

unread,
Oct 10, 2017, 11:12:31 AM10/10/17
to Vagrant
Hi all, 

I want to use Ansible during the creation of Vagrant Box or Vagrant UP. So i have the following configuration about it

config.vm.provision "ansible_local" do |ansible|
  ansible.extra_vars = { HOSTS: "vagrantbox" }
  ansible.playbook = "/vagrant/httpd.yaml"    
end
And in the httpd.yaml file i have this
---
- hosts: localhost
  vars:
    http_port: 80
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum: name=httpd state=latest
  - name: ensure apache is running (and enable it at boot)
    service: name=httpd state=started enabled=yes
 
So when i run the Vagrant up from the directory where i have Vagrant file and httpd.yaml file i got this error from Ansible
    default: Running ansible-playbook...
PLAY [localhost] ***************************************************************
skipping: no hosts matched
PLAY RECAP *********************************************************************

Now the interesting part is that when i do vagrant ssh and try to execute the same playbook, it works. It does not give me a problem. 
Yes this is ansible executing the playbook locally for testing.
 Also i want to use ansible-pull but still have the same problem.  

Its not the problem installation of the httpd i know i can do it with vagrant, i want to do something with Ansible and i start this simple test. 

Can someone tell me how can i force ansible to work with localhost. 

Jamie Jackson

unread,
Oct 10, 2017, 4:34:21 PM10/10/17
to vagra...@googlegroups.com
Maybe you could take a step back and explain why you want to use (or test) against localhost? You mentioned that you wanted to use ansible during vagrant up, for instance, so I'm having trouble understanding why you'd want to provision your host instead of your guest.

I'm provisioning a guest so I went with the simple `hosts: all` in the playbook.yml

Also, I don't know if you'll need it or not, but in my Vagrantfile, I have:


    # Disable the new default behavior introduced in Vagrant 1.7, to
    # ensure that all Vagrant machines will use the same SSH key pair.
    config.ssh.insert_key = false






--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/44fac18b-4c28-407b-9b7e-c1a82fdf825a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ivan Adji-Krstev

unread,
Oct 10, 2017, 6:12:41 PM10/10/17
to Vagrant
Hi Jamie, 

What i want to do is, to have is when the VagrantBox is up or bring up, from the inside of the box, i want Ansibe to make pull or execute the playbook.yml file on the VM (VagrantBox/Guest). I'm using Vagrant to build the VM, and i want Ansible to play role as Pull or to execute locally the playbook. I'll take the ssh.insert_key parameter, but i'm having problem to execute this playbook.yml file locally which is working if i execute after the VM is up and running but not when i build the VM. 

I think the problem is that when you build the Vagrant Box, the Ansible take the default host file and search for the localhost information, instead of the playbook.yml or something. Its executing with parameters or something similar. 

I try to use host all but i got this error:

    default: Running ansible-playbook...
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [default]
TASK [ensure apache is at the latest version] **********************************
fatal: [default]: FAILED! => {"changed": true, "failed": true, "msg": "You need to be root to perform this command.\n", "rc": 1, "results": ["Loaded plugins: fastestmirror\n"]}
        to retry, use: --limit @/vagrant/httpd.retry
PLAY RECAP *********************************************************************
default                    : ok=1    changed=0    unreachable=0    failed=1
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

Also the main part here is to use Ansible-Pull which pull the yaml file from Git Repo and then execute locally. But if i can work around with this ill work around with pull to. 
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.

Jamie Jackson

unread,
Oct 10, 2017, 10:42:06 PM10/10/17
to vagra...@googlegroups.com
Sorry, I just realized you're using the "ansible_local" provisioner; I forgot that existed. I've only used the "ansible" provisioner, so that's why I was confused. Hopefully, someone with actual "ansible_local" experience can help.

To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/0b65e7f3-5c6c-4d0a-a104-4e69bf904113%40googlegroups.com.

Alvaro Miranda Aguilera

unread,
Oct 11, 2017, 4:11:23 AM10/11/17
to vagra...@googlegroups.com
what happens if you use:

  ansible.playbook = "httpd.yaml" 

instead 

  ansible.playbook = "/vagrant/httpd.yaml" 

Ivan Adji-Krstev

unread,
Oct 11, 2017, 12:54:23 PM10/11/17
to Vagrant
Hi Alvaro, 
This seems to work. I have problem with ansible yaml file too but i have fixed. 
Now my question is, how can i use ansible-pull ( https://docs.ansible.com/ansible/2.4/ansible-pull.html ) with Vagrant file. 
Instead of Ansible playbook to use ansible-pull which pull the yaml file from git and execute it locall.

Alvaro Miranda Aguilera

unread,
Oct 12, 2017, 11:43:32 AM10/12/17
to vagra...@googlegroups.com
I don't think that is supported yet.

so the best would do some shell scripts at the moment.

Seems very useful, you way want to open some issue on the tracker and maybe someone can help integrate it.

Alvaro

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Reply all
Reply to author
Forward
0 new messages