Ansible: playbook calling Role in a directory that is in the roles directory

断了今生、忘了曾经 提交于 2019-12-05 10:56:28

I think the problem is that you need to set the relative path properly. Ansible first applies the given path relative to the called playbooks directory, then looks in the current working path (from which you are executing the ansible-playbook command) and finally checks in /etc/ansible/roles, so instead of { role: java/java_role1 } in your dir structure you could use { role: ../../roles/java/java_role1 } or { role: roles/java/java_role1 }. Yet another option would be to configure the paths in which ansible is looking for roles. For that you could set the roles_path inside your projects ansible.cfg as described in the Ansible docs.

Based on your example:

Dir tree:

ansible/
├── hosts
│   └── dev
├── plays
│   └── java_plays
│       └── java.yml
└── roles
    ├── java
    │   └── java_role1
    │       └── tasks
    │           └── main.yml
    └── role1
        └── tasks
            └── main.yml

To test it, the play would include java_role1 and role1.

plays/java_plays/java.yml:

---
 - name: deploy java stuff
   hosts: java
   roles:
    - { role: roles/role1 }
    - { role: roles/java/java_role1 }

For testing purposes these roles simply print a debug msg.

role1/tasks/main.yml:

---
- debug: msg="Inside role1"

The dev hosts file simply sets localhost to the java group. Now I can use the playbook:

fishi@zeus:~/workspace/ansible$ ansible-playbook -i hosts/dev plays/java_plays/java.yml

PLAY [deploy java stuff] *******************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [role1 : debug] ***********************************************
ok: [localhost] => {
    "msg": "Inside role1"
}

TASK [java_role1 : debug] *************************************
ok: [localhost] => {
    "msg": "Inside java_role1"
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0

Now doing the same when you use { role: ../../roles/java/java_role1 } and { role: ../../roles/role1 } your log output inside the TASK brackets would show the whole relative path instead of just the role name:

fishi@zeus:~/workspace/ansible$ ansible-playbook -i hosts/dev plays/java_plays/java.yml

PLAY [deploy java stuff] *******************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [../../roles/role1 : debug] ***********************************************
ok: [localhost] => {
    "msg": "Inside role1"
}

TASK [../../roles/java/java_role1 : debug] *************************************
ok: [localhost] => {
    "msg": "Inside java_role1"
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0

Another option and one that I use is to create an ansible.cfg file in your playbook directory and place the following in it:

[defaults]

roles_path = /etc/ansible/roles: :

or in your case:

[defaults]

roles_path = /etc/ansible/roles:/etc/ansible/roles/java

Then don't use any relative paths.

A more elegant solution (imo) is symlinking your roles directory to inside the playbooks directory.

My directory structure is as follows:

inventory/
playbooks/
  |-> roles -> ../roles
  |-> group_vars -> ../group_vars
  |-> host_vars -> ../host_vars
roles/
group_vars/
host_vars/

In my case, I created the symlink by running ln -s ../roles playbooks/roles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!