Decoupling Ansible Roles
Decouple the roles from the core Ansible repository using Ansible Galaxy roles.
Using roles that live in the Ansible code repository increases reusability. However, reusability is limited to that single repository. When you need to share a role across multiple repositories, use Ansible Galaxy roles.
Ansible Galaxy roles allow you to decouple the role from the core Ansible repository. Galaxy roles are stored in their own git repository, making it possible to develop and version the role independently.
Using the Chocolatey role as an example, you’ll now convert the role to a Galaxy role within its own git repository.
Create a Git repository#
Ansible Galaxy roles are very similar to local roles. The major difference is that they are stored outside of the core Ansible repository, in their own repository.
Using GitHub create a new repository for the chocolatey Ansible Galaxy role.
- Log into GitHub.
- In the upper-right corner of any page, click
+, and then click New repository. - Name the repository
chocolateyand add a description.
- Click Create repository.
- After the repository is created, click the Code button.
- Take note of the
HTTPS URLfor the repository. It will be used to install the Galaxy role.
Generate Ansible Galaxy role#
Ansible Galaxy roles have the same directory structure as the role when done by hand, but there is an easier way. The command ansible-galaxy has an init option that will create the role’s skeleton.
Git Repository
Create the galaxy role outside of your Ansible repository. Each Galaxy role is stored in its git repository.
- Use
ansible-galaxy initto generate the role directory structure.
- Use the
treecommand to view the directory structure of the new Ansible galaxy role.
- Change into the
chocolateydirectory.
- Connect the Ansible Galaxy role to the GitHub repository.
Replace <remoteRepositoryURL> with the HTTPS URL of GitHub Chocolatey repository.
- View the Git remotes to verify it’s connected to GitHub.
- Push the changes to GitHub.
Replace a role with a Galaxy role#
Next, you will copy the role files and tasks; then move the non-galaxy role out of the roles directory.
- Change into the
ansibledirectory.
- Create a new directory to store the non-Galaxy Chocolatey role.
- Move the non-Galaxy Chocolatey role.
- Remove the
roles/chocolateydirectory.
- Copy the
tasks/main.ymlto the Galaxy role.
- Copy the
filescontents to the Galaxy role.
- Push the changes to GitHub.
- Run the
site.ymlplaybook.
Missing role
The rolechocolateywas not found.
Using Ansible Galaxy roles#
The roles directory is empty. Without the non-galaxy role living in that directory, Ansible won’t load the role. You use a requirements.yml file to deal with that.
A requirements.yml is a file that lists the role dependencies our Ansible codebase has. Using this file, you can download the required roles before running the playbooks that need them.
Let’s create a requirements.yml file in the roles directory.
Let’s look at the parameters in the file above:
-
src: set to the HTTPS URL of the GitHub repository as the Galaxy role is stored in a Git repository. -
scm: set togitbecause that’s the type of source control used. -
version: defines which code to pull down. Set tomasterto pull down the code from themasterbranch. -
name: specifies the name of the role,chocolatey.
With the requirements defined, install the Galaxy role. To do so, you will use the ansible-galaxy command.
Run the site.yml playbook.
Version with Git tags#
At some point, you’ll want more control over the use of the role’s version to avoid pulling untested changes. You can gain that level of control by leveraging Git tags.
Within GitHub, you can create releases for your code and tag them. You can use those tags within the requirements.yml to version-lock the role to that tag.
Create a release#
- Log into GitHub.
- Open the Chocolatey repository.
- On the right-hand side under Release, click Create a new release.
- Input a tag, release title, and description.
- Update the
requirements.ymlto use a tag.- Change the version to
v1.0to reference the release tag instead of themasterbranch.
- Change the version to
- Re-install the Galaxy role.
- Run the
site.ymlplaybook. Update the<Password>with the password created using theansible-vaultcommand in thegroup_vars/linux.ymlandgroup_vars/windows.ymlfiles using thenanoeditor.
Practice all the commands in the terminal. We have provided a summarized view of the commands below:
Troubleshooting tips#
Missing role
If you receive the errorchocolateywas NOT installed successfully: could notfind/use git, you need to install git.apt-get install git
In this lesson, you decoupled your codebase from the Ansible Galaxy roles in a different repository.
