So I figured I should start using Ansible Galaxy when possible, instead of writing my own roles. I just installed my first role and it was installed to /etc/local/ansible/roles
(I am on OSX).
Now I wonder how you install this roles where I actually need it? Do I just copy the role to where I need it or is there an Ansible way of doing it?
Setting where to install 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 .
Galaxy can use git to add other role sources, such as GitHub. You can initialize a new galaxy role using ansible-galaxy init , or you can install a role directly from the Ansible Galaxy role store by executing the command ansible-galaxy install <name of role> .
Storing and finding roles in the configured roles_path. The default search path is ~/. ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles. in the directory where the playbook file is located.
Yes, you would copy them according to a sample project structure:
site.yml webservers.yml fooservers.yml kubernetes.yaml roles/ common/ files/ templates/ tasks/ handlers/ vars/ meta/ webservers/ files/ templates/ tasks/ handlers/ vars/ meta/ kubernetes/ files/ templates/ tasks/ handlers/ vars/ meta/
or you can just run ansible-galaxy
with the -p ROLES_PATH
or --roles-path=ROLES_PATH
option to install it under /your/project/root
You can also use the /etc/local/ansible
directory as your project root if you'd like to.
Additionally, you can get help by running the command ansible-galaxy install --help
In general, I stick all my shared roles in a single master folder that gets shared across all my projects. This avoids the tediousness of manual copy/pasting and updating multiple copies of the same role.
Than I modify each project's ansible.cfg
to tell ansible
to look for roles in that master folder in addition to the local project folder.
Sample ansible.cfg
:
[defaults] roles_path = ~/Code/ansible_roles
Ansible first searches the local project for a role, then searches the roles_path
. You can specify multiple paths by separating them with colons.
By default, ansible-galaxy install username.rolename
will install the role to the roles_path
configured in ansible.cfg
, so that's pretty much all you need to do.
Occasionally I want to install the role into the specific project and not the master folder. For example, to avoid version conflicts when two roles have role dependencies that require different versions of the same role. In that case, you can use the -p ROLES_PATH
or --roles-path=ROLES_PATH
option:
ansible-galaxy install username.rolename -p ~/Code/project_deploy/ansible/roles/
In Ansible 1.9, you can manually specify where you want a role to be installed in your project's requirements.yml
. Unfortunately, this path
param was removed in Ansible 2:
# from galaxy - src: jeffwidman.elasticsearch # from private github repo, installing to a relative path - src: https://github.com/jeffwidman/private_ansible_role path: vagrant/roles/
If you want to customize things further, there's an open issue to add support for multiple ansible.cfg
files which would let you easily set roles_path
at varying levels of specificity. Ansible will read ANSIBLE_CONFIG
, ansible.cfg
in the current working directory, .ansible.cfg
in the home directory or /etc/ansible/ansible.cfg
, whichever it finds first.
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