Decoupling Ansible 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.

  1. Log into GitHub.
  2. In the upper-right corner of any page, click +, and then click New repository.
  3. Name the repository chocolatey and add a description.
Create a new repository
  1. Click Create repository.
  2. After the repository is created, click the Code button.
  3. Take note of the HTTPS URL for 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.

  1. Use ansible-galaxy init to generate the role directory structure.
Generate role directory structure
  1. Use the tree command to view the directory structure of the new Ansible galaxy role.
View directory structure
  1. Change into the chocolatey directory.
Change directory
  1. Connect the Ansible Galaxy role to the GitHub repository.
Connect to repository

Replace <remoteRepositoryURL> with the HTTPS URL of GitHub Chocolatey repository.

  1. View the Git remotes to verify it’s connected to GitHub.
Verify connection
  1. Push the changes to GitHub.
Push the changes

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.

  1. Change into the ansible directory.
Change into the ansible repository
  1. Create a new directory to store the non-Galaxy Chocolatey role.
Create the nonGalaxy/chocolatey directory
  1. Move the non-Galaxy Chocolatey role.
Move the roles
  1. Remove the roles/chocolatey directory.
Remove directory
  1. Copy the tasks/main.yml to the Galaxy role.
Copy tasks
  1. Copy the files contents to the Galaxy role.
Copy files
  1. Push the changes to GitHub.
Push changes
  1. Run the site.yml playbook.
Execute the playbook

Missing role
The role chocolatey was 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.

requirements.yml

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 to git because that’s the type of source control used.

  • version: defines which code to pull down. Set to master to pull down the code from the master branch.

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

Install Ansible Galaxy role

Run the site.yml playbook.

Execute the 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#

  1. Log into GitHub.
  2. Open the Chocolatey repository.
  3. On the right-hand side under Release, click Create a new release.
  4. Input a tag, release title, and description.
Create a new release
  1. Update the requirements.yml to use a tag.
    • Change the version to v1.0 to reference the release tag instead of the master branch.
requirements.yml
  1. Re-install the Galaxy role.
Re-Install role
  1. Run the site.yml playbook. Update the <Password> with the password created using the ansible-vault command in the group_vars/linux.yml and group_vars/windows.yml files using the nano editor.
Execute the playbook

Practice all the commands in the terminal. We have provided a summarized view of the commands below:

Commands
This code requires the following environment variables to execute:
AWS_Linux_EC2_Instance_DNS_Name
Not Specified...
AWS_Windows_EC2_Instance_DNS_Name
Not Specified...
Azure_Linux_VM_Instance_DNS_Name
<Linux>
Azure_Windows_VM_Instance_DNS_Name
<Windows>
Terminal 1
Terminal

Click to Connect...

Troubleshooting tips#

Missing role
If you receive the error chocolatey was NOT installed successfully: could not find/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.

Make it Reusable with Ansible Roles
Summary
Mark as Completed
Report an Issue