# I see error about can not find role after change playbook to role

**URL:** <https://forum.ansible.com/t/i-see-error-about-can-not-find-role-after-change-playbook-to-role/6247>\
**Category:** Get Help\
**Created:** [June 7, 2024, 5:10pm UTC](https://forum.ansible.com/t/i-see-error-about-can-not-find-role-after-change-playbook-to-role/6247 "2024-06-07T17:10:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mostafa\_Faridi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mostafa_faridi/32/3032_2.png) [@Mostafa\_Faridi](https://forum.ansible.com/u/Mostafa_Faridi)\
**Post date:** [June 7, 2024, 5:10pm UTC](https://forum.ansible.com/t/i-see-error-about-can-not-find-role-after-change-playbook-to-role/6247/1 "2024-06-07T17:10:01Z")

</div>

I use Packer to make qemu image VM In my hcl file I have this

```auto
provisioner "ansible-local" {
    playbook_file = "/ansible/playbooks/mysql.yml"

```

my playbook was complex and with one playbook I install many packages on this VM by ansible.

I break this playbook to roles, so I make role folder and made in that folder folder like common and in that folder I make tasks and in the tasks I made main.yml and put some tasks there

but when I start packer to build image I see error about it can not find role and packer can not make vm.

I do not know where is problem? maybe it made by packer

this tree of my project

```auto
new1
├── ansible
│ ├── ansible.cfg
│ ├── inventory
│ ├── playbooks
│ │ ├── debian.yml
│ │ └── mysql.yml
│ └── roles
│ └── mysql
│ └── common
│ └── tasks
│ └── main.yml
└── mysql
    ├── ansible
    │ ├── playbook.yml
    │ └── playbook.ymlbyroot
    ├── ansible.cfg
    ├── build.txt
    ├── cloud-init
    │ ├── meta-data
    │ ├── network-config
    │ └── user-data
    ├── mysql-image.pkr.hcl
    ├── packerlog.txt
    └── README.md

11 directories, 14 files

```

this is my pakybook

```auto
---
- name: Provision mysql
  hosts: all
  become: true
  roles:
    - role: mysql

```

I see this when run packer build command

```auto
emu.template: ERROR! the role mysql was not found in /tmp/packer-provisioner-ansible-local/666346b6-806f-2b67-4479-0db2ce5e0c2f/roles:/home/ubuntu/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/tmp/packer-provisioner-ansible-local/666346b6-806f-2b67-4479-0db2ce5e0c2f
==> qemu.template: 
==> qemu.template: The error appears to be in '/tmp/packer-provisioner-ansible-local/666346b6-806f-2b67-4479-0db2ce5e0c2f/mysql.yml': line 6, column 7, but may
==> qemu.template: be elsewhere in the file depending on the exact syntax problem.
==> qemu.template: 
==> qemu.template: The offending line appears to be:
==> qemu.template: 
==> qemu.template: roles:
==> qemu.template: - role: mysql
==> qemu.template: ^ here

```

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [June 7, 2024, 6:07pm UTC](https://forum.ansible.com/t/i-see-error-about-can-not-find-role-after-change-playbook-to-role/6247/2 "2024-06-07T18:07:36Z")

</div>

Couple of things.

1. [There’s a few places where Ansible will look for installed roles.](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html#storing-and-finding-roles)  
a. in collections, if you’re using them  
b. in a directory called `roles/`, _relative to the playbook file_  
c. in the configured [roles\_path](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-roles-path). The default search path is `~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles`  
d. in the directory where the playbook file is located
2. You cannot nest roles into other roles. Roles can share other roles as dependencies, but they all must exist in one of the aformented locations

So in your example, `./new1/ansible/roles/mysql/common/` doesn’t make any sense for where the role is installed. Additionally, none of your playbooks live relative to the `roles` directory such that `role: mysql` can discover it.

---

<div class="post-metadata">

**Author:** ![Mostafa\_Faridi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mostafa_faridi/32/3032_2.png) [@Mostafa\_Faridi](https://forum.ansible.com/u/Mostafa_Faridi)\
**Post date:** [June 7, 2024, 7:01pm UTC](https://forum.ansible.com/t/i-see-error-about-can-not-find-role-after-change-playbook-to-role/6247/3 "2024-06-07T19:01:17Z")

</div>

When I change ansible-local to ansible it can find role but can not install mysql and do not give me error  
this work if we give full path of role. for exmple

```auto
role: /home/mn/new1/roles/mysql

```

Why I have to set full path of role?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [June 7, 2024, 9:44pm UTC](https://forum.ansible.com/t/i-see-error-about-can-not-find-role-after-change-playbook-to-role/6247/4 "2024-06-07T21:44:46Z")

</div>

> [@Mostafa\_Faridi](#):
>
> ```auto
> │ └── roles
> │ └── mysql
> │ └── common ← This is the problem.
> │ └── tasks
> │ └── main.yml
> 
> ```

Your problem is the `common` directory. Move your `tasks` directory up a level and get rid of `common`.

---

<div class="post-metadata">

**Author:** ![Mostafa\_Faridi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mostafa_faridi/32/3032_2.png) [@Mostafa\_Faridi](https://forum.ansible.com/u/Mostafa_Faridi)\
**Post date:** [June 8, 2024, 8:14am UTC](https://forum.ansible.com/t/i-see-error-about-can-not-find-role-after-change-playbook-to-role/6247/5 "2024-06-08T08:14:17Z")

</div>

Thank you I move tasks above common , but I get this error

```auto
==> qemu.template: ERROR! the role 'mysql' was not found in /tmp/packer-provisioner-ansible-local/66641215-8439-e46c-6c6a-fa00e8213d2d/roles:/home/ubuntu/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/tmp/packer-provisioner-ansible-local/66641215-8439-e46c-6c6a-fa00e8213d2d
==> qemu.template: 
==> qemu.template: The error appears to be in '/tmp/packer-provisioner-ansible-local/66641215-8439-e46c-6c6a-fa00e8213d2d/mysql.yml': line 6, column 7, but may
==> qemu.template: be elsewhere in the file depending on the exact syntax problem.
==> qemu.template: 
==> qemu.template: The offending line appears to be:
==> qemu.template: 
==> qemu.template: roles:
==> qemu.template: - role: mysql
==> qemu.template: ^ here

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [June 8, 2024, 8:25pm UTC](https://forum.ansible.com/t/i-see-error-about-can-not-find-role-after-change-playbook-to-role/6247/6 "2024-06-08T20:25:03Z")

</div>

As @Denney-tech explained, there are a few places Ansible will look for installed roles. You’ve got a slightly more complicated situation because you’re using `Packer`, and frankly I have no idea what “ansible-local” is, but apparently when it gets around to running `ansible-playbook` on your `mysql.yml` playbook, the path to your `mysql.yml` playbook has become  
`/tmp/packer-provisioner-ansible-local/`_someUUID_`/mysql.yml`. So it looks like whatever this process is is moving things around.

Ansible expects — or I should say I expect Ansible to expect — your `roles` directory to be in the same directory as your playbooks. But I see you have them in their own `playbooks` subdirectory. I would move `playbooks/debian.yml` and `playbooks/mysql.yml` up one level, lose the now-empty `playbooks` directory, and fix the hcl file’s `playbook_file` path accordingly. But that’s all guess work based on the error you’ve shown us. It may be that it’s _only_ copying your `mysql.yml` playbook, and leaving everything else … somewhere, in which case your `ansible.cfg` and `inventory` files also won’t be found either.

In any case, the error lists all the places it looked for your `mysql` role. So the short answer is, it failed the way it did because your `mysql` role isn’t in one of those places. You may tell it to look in other places, with either environment variables or settings in your `ansible.cfg` file, but it isn’t clear whether it’s seeing your `ansible.cfg` file anyway, and I don’t know how to set environment variables through an hcl file.

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [June 11, 2024, 12:03pm UTC](https://forum.ansible.com/t/i-see-error-about-can-not-find-role-after-change-playbook-to-role/6247/7 "2024-06-11T12:03:08Z")

</div>

@Mostafa_Faridi Try to copy the Roles and vars into the Playbooks Folders.  
I have litterally not the slightest idea why, but it should work.  
[Source](https://www.reddit.com/r/awx/comments/1aq8j0n/how_to_use_roles_in_awx/)  
try to use the folder layout like [this](https://spacelift.io/blog/ansible-best-practices):
