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
---
- 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
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
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.
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
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.
@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: