12

I have this tree:

├── plays
│   ├── ansible.cfg
│   ├── playbook_01.yml
│   ├── playbook_02.yml
│   └── playbook_03.yml
├── README.rst
├── roles
│   ├── role_A
│   │   ├── files
│   │   └── tasks
│   │       └── main.yml
│   └── role_B
│       ├── files
│       └── tasks
│           └── main.yml
├── serverlist
│   ├── client1_serverlist_prod
│   ├── client1_serverlist_test
│   ├── client1_serverlist_train
│   ├── client2_serverlist_prod
│   ├── client2_serverlist_test
│   └── client2_serverlist_train
└── vagrant
    └── Vagrantfile

With ansible.cfg in play folder::

$ cat plays/ansible.cfg
[defaults]
roles_path=../roles/
$

I call from vagrant the ansible.playbook::

$ grep playbook vagrant/Vagrantfile
ansible.playbook = "../plays/playbook_01.yml

on playbook_01.yml::

$ cat plays/playbook_01.yml
- hosts: vagrant
  vars:
    user: fox
    home: /home/fox
  roles:
    - role_B

with role_B::

$ cat roles/role_B/tasks/main.yml
---
- name: Create user group
  group: name={{ user }} state=present

- name: Create home directory for user
  file: state=directory path={{ home }} group=www-data owner={{ user }}
$

But ansible when only went to see roles in play folder, I get this error::

vagrant$ vagrant provision
==> vagrant: Running provisioner: ansible...
PYTHONUNBUFFERED=1 ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_FORCE_COLOR=true
ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o
ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook
--user=vagrant --connection=ssh --timeout=30 --limit='vagrant'
--inventory-file=/home/luis/lab/sandbox/akd-iac/stack/vagrant/.vagrant/provisioners/ansible/inventory
--sudo -vvvv ../plays/playbook_01.yml
ERROR: cannot find role in
~/stack/plays/roles/role_B or
~/stack/plays/role_B or /etc/ansible/roles/role_B
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.
vagrant$

4 Answers 4

7

You can probably use the variable roles_path. It points to the folder where your roles are stored.

7
  • nop even with an abspath to the roles folder I still have the same error Commented Apr 8, 2016 at 9:21
  • Could you post the content of your file: ansible.cfg (if you are using it) and the Ansible version? Commented Apr 8, 2016 at 9:23
  • Ah! You can also try to set the roles_path variable in ansible.cfg and forget about paths in your playbook. Commented Apr 8, 2016 at 9:26
  • I've updated the question with the detail on plays/ansible.cfg file. $ ansible --version say ansible 1.9.2, configured module search path = None Commented Apr 9, 2016 at 15:47
  • 1
    Hi, sorry for being insistent in the same documentation page: docs.ansible.com/ansible/intro_configuration.html. Please, take a look to the first few lines about the location of the ansible.cfg file. You mentioned your file is in plays/ansible.cfg, which doesn´t seem to be an standard path. Please, make sure that this file is in the correct place within your Vagrant Box AND enter the absolute path as the value of roles_path Commented Apr 9, 2016 at 20:02
2
+100

As mentioned your ansible.cfg is not likely being picked up by ansible. You could test if your ansible.cfg is being picked up by setting this:

[defaults]
roles_path = ./isnasiblegettingmycfg

and checking the debug output. The ansible.cfg is looked for in:

  • ANSIBLE_CONFIG (an environment variable)
  • ansible.cfg (in the current directory)
  • .ansible.cfg (in the home directory)
  • /etc/ansible/ansible.cfg

Roles are looked for in the directory of the executed playbook, and in the directory roles/ relative to the executed playbook and roles_path. So another simple solution is:

cd ~/stack/plays/ && ln -s ../roles
1
  • I'd like to know why ansible.cfg it's not picked up, as I am having a similar issue with the roles_path.
    – wedi
    Commented Nov 15, 2016 at 2:49
0

I make it work just moving roles folder inside the plays folder and removing the play/ansible.cfg:

.
├── plays
│   ├── playbook_01.yml
│   ├── playbook_02.yml
│   ├── playbook_03.yml
│   └── roles
│       ├── role_A
│       │   ├── files
│       │   └── tasks
│       │       └── main.yml
│       └── role_B
│           ├── files
│           └── tasks
│               └── main.yml
├── README.rst
├── serverlist
│   ├── client1_serverlist_prod
│   ├── client1_serverlist_test
│   ├── client1_serverlist_train
│   ├── client2_serverlist_prod
│   ├── client2_serverlist_test
│   └── client2_serverlist_train
└── vagrant
    └── Vagrantfile

Details about the ansible.cfg configuration that make it work::

$ ls ~/.ansible.cfg
ls: cannot access ~/.ansible.cfg: No such file or directory

$ grep roles_path /etc/ansible/ansible.cfg
#roles_path    = /etc/ansible/roles

$ echo $ANSIBLE_CONFIG

$

I still call from vagrant the ansible.playbook::

$ grep playbook vagrant/Vagrantfile
ansible.playbook = "../plays/playbook_01.yml

on playbook_01.yml::

$ cat plays/playbook_01.yml
- hosts: vagrant
  vars:
    user: fox
    home: /home/fox
  roles:
    - role_B

with role_B::

$ cat roles/role_B/tasks/main.yml
---
- name: Create user group
  group: name={{ user }} state=present

- name: Check Python version
  command: python --version
  register: pyver

- name: Print version
  debug:
    msg: "Python Version: {{ pyver.stderr }}"

With this modification that work like a charm:

$ vagrant provision
==> vagrant: Running provisioner: ansible...

PLAY [vagrant] ************************************************************
GATHERING FACTS ***********************************************************
  ok: [vagrant]
TASK: [role_B | Create user group] ****************************************
  ok: [vagrant]
TASK: [role_B | Check Python version] *************************************
  changed: [vagrant]
TASK: [role_B | Print version] ********************************************
  ok: [vagrant] => {
    "msg": "Python Version: Python 2.7.9"
}
PLAY RECAP ****************************************************************
vagrant                    : ok=4    changed=1    unreachable=0    failed=0
$

It look like that roles folder need to be inside the folder playbooks lives.

0

the roles path can be provided directly

https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html#storing-and-finding-roles

or if you prefer, one can refer to it with :

roles:
- role: role_in_the_current_folder
- role: ../roles/role_in_an_other_folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.