Lets say I have numerous ansible playbooks for various personal projects. They all do wildly different things, but they have a few repeated roles in common.
For example one of the roles clones my personal dot-files repository so I can have my personal .bash_profile
, .vimrc
, etc... on every server.
All my playbooks are in separate git repositories. Right now each of them duplicates the "dotfiles" role separately.
Is there a good way I can move that re-usable role to a separate repository and have each of the invidiual playbooks import that as needed?
Have each role in its own git repository.
This can be challenged. that's the one I like to use on different of my projects:
├── inventories
│ ├── dev
│ │ ├── group_vars/
│ │ └── hosts.ini
│ └── prod
│ ├── group_vars/
│ └── hosts.ini
├── group_vars/
├── host_vars/
├── files/
├── templates/
├── roles
│ ├── localy_versionned_role1/
│ ├── localy_versionned_role2/
│ ├── requirements.yml
│ ├── .gitignore
├── ansible.cfg
├── README.md
├── some_playbook.yml
├── other_playbook.yml
# Ignore everything in dir...
/*
# ... but current file...
!.gitignore
# ... external role requirement file
!requirements.yml
# ... and configured custom/local roles
!localy_versionned_role*/
# Classic galaxy role
- src: galaxy_user.role_name
# Git available role
- src: [email protected]:path/to/repo.git
scm: git
version: master
name: local_role_name
You only need to list "top" roles, dependencies (listed in meta/main.yml
of the role) will be downloaded also.
We make sure that roles are searched and downloaded in our local folder
roles_path = roles
ansible-galaxy install -r roles/requirements
ansible-playbook -i inventories/dev some_playbook.yml
By default, ansible-galaxy downloads from git and removes the local repository structure (i.e. the .git
directory). If you want to download the roles and keep working on them (change, commit, push...), you can keep the git structure with:
ansible-galaxy install -g -r roles/requirements
Note that this will write a meta/.galaxy_install_info
file inside your role that git will see as new if you did not ignore already.
Roles will not be updated with a new version (especially from galaxy) if they are already installed. To force updating use:
ansible-galaxy -f -r roles/requirements.yml
of course you do this also to switch to git workspace version (or reset to version given in requirements file)
ansible-galaxy -f -g -r roles/requirements.yml
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