cpe111
cpe111

Reputation: 85

Using Vagrant to test ansible roles

How do I restrict the scope of a playbook that references a role to the Vagrant image I have provisioned?

I have a playbook:

---
- name: Test Vagrant  
  hosts: all
  become: yes
  roles:
    - vagtest

This points to a role that simply installs the yum package tree.

My Vagrantfile is as follows:

Vagrant.require_version ">= 1.7.0"

Vagrant.configure(2) do |config|

  config.vm.box = "centos/7"

  config.ssh.insert_key = false

  config.vm.provision "ansible" do |ansible|
    ansible.verbose = "v"
    ansible.playbook = "vagtest.yml"
  end
end

Now all works well - vagrant up / vagrant provision. I am concerned about 'hosts: all' in the playbook. Is there any way to restrict the scope of the playbook so that only my vagrant image is affected ? I am concerned that if the playbook is executed outside of vagrant that the test playbook will get executed across the whole environment.

Thanks.

Upvotes: 2

Views: 420

Answers (1)

Kevin C
Kevin C

Reputation: 5740

You need to create a hosts/inventory file. In that host file point towards your vagrant image.

Then, change your hosts: all to hosts: <what you provide in your host file>

run with ansible-playbook -i hosts playbook.yml

Upvotes: 2

Related Questions