Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reuse ansible roles I have written?

Tags:

ansible

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?

like image 806
user2490003 Avatar asked Sep 05 '25 22:09

user2490003


1 Answers

Prerequisite

Have each role in its own git repository.

Typical project structure

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

roles/.gitignore

# Ignore everything in dir...
/*

# ... but current file...
!.gitignore

# ... external role requirement file
!requirements.yml

# ... and configured custom/local roles
!localy_versionned_role*/

roles/requirements.yml

# 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.

Ansible.cfg

We make sure that roles are searched and downloaded in our local folder

roles_path = roles

Workflow to deploy from your project:

  • Clone your project repository
  • Download the external roles:
ansible-galaxy install -r roles/requirements
  • Launch your playbook:
ansible-playbook -i inventories/dev some_playbook.yml

Going further

Downloading the roles as git workspaces

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.

Updating roles after first install

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
like image 125
Zeitounator Avatar answered Sep 09 '25 19:09

Zeitounator