# Quick way for testing new Ansible roles

**URL:** <https://forum.ansible.com/t/quick-way-for-testing-new-ansible-roles/27587>\
**Category:** Ansible Project\
**Tags:** galaxy-ng\
**Created:** [May 22, 2018, 2:01am UTC](https://forum.ansible.com/t/quick-way-for-testing-new-ansible-roles/27587 "2018-05-22T02:01:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Behrang](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Behrang](https://forum.ansible.com/u/Behrang)\
**Post date:** [May 22, 2018, 2:01am UTC](https://forum.ansible.com/t/quick-way-for-testing-new-ansible-roles/27587/1 "2018-05-22T02:01:23Z")

</div>

Hi,

In the [docs](https://galaxy.ansible.com/intro#role-test), it is written that:

#### Test It

The actual work to be performed gets added as playbook tasks to _tasks/main.yml_. Once you’ve added some tasks, create a playbook to test the role and insure it works. Here’s a simple playbook to execute your new role:

```auto
# test.yml
# test playbook
- hosts: example
  roles:
  - { role: acme }
 

```

```auto
$ ansible-playbook test.yml

```

However, this won’t work as the test playbook directory doesn’t have access to the role.

```auto

$ ansible-galaxy init sample-role
- sample-role was created successfully

$ cd sample-role/tests/

$ ansible-playbook test.yml
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

ERROR! the role 'sample-role' was not found in /Users/grey.behrangs/Documents/Projects/ansible-roles/sample-role/tests/roles:/Users/grey.behrangs/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/Users/grey.behrangs/Documents/Projects/ansible-roles/sample-role/tests

The error appears to have been in '/Users/grey.behrangs/Documents/Projects/ansible-roles/sample-role/tests/test.yml': line 5, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  roles:
    - sample-role
      ^ here

Is there a quick way to test newly created roles?

Cheers,
Behrang

```

---

<div class="post-metadata">

**Author:** ![BIJAY\_PARIDA](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@BIJAY\_PARIDA](https://forum.ansible.com/u/BIJAY_PARIDA)\
**Post date:** [May 22, 2018, 3:22am UTC](https://forum.ansible.com/t/quick-way-for-testing-new-ansible-roles/27587/2 "2018-05-22T03:22:28Z")

</div>

Host example is not a valid host name.

- hosts: localhost

---

<div class="post-metadata">

**Author:** ![Behrang](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Behrang](https://forum.ansible.com/u/Behrang)\
**Post date:** [May 22, 2018, 8:01am UTC](https://forum.ansible.com/t/quick-way-for-testing-new-ansible-roles/27587/3 "2018-05-22T08:01:26Z")

</div>

ansible-galaxy generates the correct host name (localhost), so the docs on the website seem to be out of date.

But the problem was that the role being developed is not under tests/roles. I added a symlink from tests/roles to the root directory of the role and that fixed the problem.

---

<div class="post-metadata">

**Author:** ![Mischa\_ter\_Smitten](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Mischa\_ter\_Smitten](https://forum.ansible.com/u/Mischa_ter_Smitten)\
**Post date:** [May 23, 2018, 7:15am UTC](https://forum.ansible.com/t/quick-way-for-testing-new-ansible-roles/27587/4 "2018-05-23T07:15:14Z")

</div>

Have a look at some of our roles [https://github.com/Oefenweb?tab=repositories](https://github.com/Oefenweb?tab=repositories). They are all tested on Travis CI and contain a Vagrantfile for manual testing.

---

<div class="post-metadata">

**Author:** ![Behrang](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Behrang](https://forum.ansible.com/u/Behrang)\
**Post date:** [May 23, 2018, 7:28am UTC](https://forum.ansible.com/t/quick-way-for-testing-new-ansible-roles/27587/5 "2018-05-23T07:28:26Z")

</div>

Thanks Mischa.

I ended up doing something similar with Vagrant.

- Created a symlink from the tests/roles dir to the root dir of the role I am writing

- Installed the _vagrant-hostupdater_ to add a custom host name to the Vagrant box I use for testing

- Updated the inventory and added the hostname of the Vagrant box

- Created a minimal Vagrantfile to spin up the test env:

> Vagrant.configure(“2”) do |config|
> 
> config.vm.box = “centos/6”
> 
> config.vm.network “private\_network”, ip: “192.168.33.10”
> 
> config.vm.hostname = “myrole.vagrant.test.local”
> 
> config.vm.provider “virtualbox” do |vb|
> 
> vb.memory = “2048”
> 
> end
> 
> config.vm.provision “shell”, inline: \<\<-SHELL
> 
> printf ‘[sensu]\nname=sensu\nbaseurl=[https://sensu.global.ssl.fastly.net/yum/$releasever/$basearch/\ngpgcheck=0\nenabled=1](https://sensu.global.ssl.fastly.net/yum/$releasever/$basearch/%5Cngpgcheck=0%5Cnenabled=1)’ \> /etc/yum.repos.d/sensu.repo
> 
> yum update -y
> 
> yum install libselinux-python sensu -y
> 
> SHELL
> 
> end

There’s still room for improvement, but this gets the job done for the time being.
