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

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

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

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

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

I see this when run packer build command

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

Couple of things.

  1. There’s a few places where Ansible will look for installed 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. 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.

3 Likes

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

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

Why I have to set full path of role?

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

3 Likes

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

==> 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

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.

4 Likes

@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
try to use the folder layout like this: