Prathisrihas Reddy
Prathisrihas Reddy

Reputation: 434

running plays on host besides running on vagrant guests when playbook is provisioned via vagrant

I am new to ansible actually. I believe that I am using an ansible i.e., /usr/bin/ansible of the Ubuntu host machine to provision vagrant guests but, Where can I do a change to my playbook so that I can control some things on my host machine besides vagrant guests?

Upvotes: 0

Views: 305

Answers (2)

gile
gile

Reputation: 5996

In your Vagrantfile you can define provisioning as:

config.vm.provision "ansible" do |ansible|
ansible.playbook = "myPlaybook.yml"

in this case Vagrant automatically creates an Ansible inventory file in .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory that looks like:

# Generated by Vagrant
default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222

so the playbook runs to localhost connecting via ssh to port 2222 and not to localhost as

---
- hosts: localhost
  connection: local

If I well understood your problem, you need to run the same playbooks both on Vagrant provided hosts and on your Vagrant host (localhost).

So I think you can assign an ip to Vagrant provided hosts that you are provisioning with Ansible and use a static inventory file you'll supply to Vagrant, so your Vagrantfile will contain something like

config.vm.network "private_network", ip: "192.168.33.10"
...

config.vm.provision "ansible" do |ansible|
ansible.playbook = "myPlaybook.yml"
ansible.inventory_path = "./myInventory"

and in ./myInventory

myHost1 ansible_ssh_host=192.168.33.10 ansible_ssh_port=22

then you should be able to distinguish vagrant provided host (as it was a remote host) from your localhost. Then you'll arrange your playbook having different targets (myHost1 and localhost) and it should handle correctly local_action.

In your inventory you can define groups as in any ansible inventory file and you could tell Vagrantfile to take care of that supplying ansible.groups

config.vm.provision "ansible" do |ansible|
ansible.playbook = "myPlaybook.yml"
ansible.inventory_path = "./myInventory"
ansible.groups = { 
"group1" => ["myGroup"]

Upvotes: 0

zabeltech
zabeltech

Reputation: 971

To run commands on your local host basically you have two options:

  1. for quick stuff you can use local_action like so:

- name: take out of load balancer pool local_action: command /usr/bin/some_command

  1. you can hav a collection of tasks in you playbook like this:

- name: Execute some local commands hosts: localhost tasks: - local_action: command /usr/bin/some_command - local_action: command /usr/bin/some_command

For more information refer to the very extensive docmentation

Upvotes: 1

Related Questions