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$
The easiest way to run only one task in Ansible Playbook is using the tags statement parameter of the “ansible-playbook” command. The default behavior is to execute all the tags in your Playbook with --tags all .
You can re-use tightly focused playbooks, but you can only re-use them statically, not dynamically. A role contains a set of related tasks, variables, defaults, handlers, and even modules or other plugins in a defined file-tree.
Ansible role is a part of ansible-galaxy. So you need to use the ansible-galaxy command to list down all the roles in your Ansible controller node.
Installing roles By default, Ansible downloads roles to the first writable directory in the default list of paths ~/. ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles . This installs roles in the home directory of the user running ansible-galaxy .
You can probably use the variable roles_path
. It points to the folder where your roles are stored.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With