I have the following directory structure in my playbook:
deploy-thing
├── README.md
├── files
│ ├── thing.service
│ └── ld.local.conf
├── main.yml
├── roles
│ ├── ansible-role-add-swap
│ │ ├── README.md
│ │ ├── defaults
│ │ │ └── main.yml
│ │ └── tasks
│ │ └── main.yml
│ ├── ansible-role-build-pkgs
│ │ └── tasks
│ │ └── main.yml
│ ├── ansible-role-deploy-other-thing
│ │ ├── README.md
│ │ ├── main.yml
│ │ ├── roles
│ │ │ ├── ansible-role-build-pkgs
│ │ │ │ └── tasks
│ │ │ │ └── main.yml
│ │ │ └── ansible-role-build-redis
│ │ │ ├── README.md
│ │ │ └── tasks
│ │ │ └── main.yml
│ │ ├── tasks
│ │ │ └── main.yml
└── vars.yml
Within main.yml, the following is called:
roles:
- {role: ./roles/ansible-role-deploy-other-thing}
Within roles/ansible-role-deploy-other-thing/tasks/main.yml, the following is called:
- include_role:
name: ansible-role-build-redis
When run, this include_role
causes the following error:
ERROR! the role ‘ansible-role-build-redis’ was not found in
/Users/myuser/playbooks/deploy-thing/roles:/Users/myuser/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/Users/myuser/playbooks/deploy-thing
It appears that the parent is only searching its own roles path when running, not the role path of the role we’re using (ansible-role-deploy-other-thing). How can I cause it to search its own roles path, allowing me to deploy roles within roles?
2
Answers
A: Put the path to the roles into the configuration. See DEFAULT_ROLES_PATH. The default is
As you can see from the error message Ansible searches for roles in the configured paths plus the current directory of the playbook
You’ll have to add each path of the nested roles into the configuration. This makes the idea of nested roles not very practical. Best practice is to keep the roles flat. See more details in Search paths in Ansible.
i don’t think it’s a good practice to include Ansible roles on another roles. if you want any role depends on other, use the meta/main.yml file under your role to create dependency between two or more roles.
So you puts all the roles at the same level (deploy-thing/roles)
and you add meta like this on the ansible-role-deploy-other-thing/meta/main.yml file. (it’s a best practice to create a role using ansible-galaxy
ansible-galaxy init ansible-role-deploy-other-thing
)Read the Meta Ansible Documentation for roles dependency:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html#role-dependencies