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
chocolatey
and add a description.
- Click Create repository.
- After the repository is created, click the Code button.
- 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.
- Use
ansible-galaxy init
to generate the role directory structure.
ansible-galaxy init chocolatey --offline
- Use the
tree
command to view the directory structure of the new Ansible galaxy role.
tree
- Change into the
chocolatey
directory.
cd chocolatey
- Connect the Ansible Galaxy role to the GitHub repository.
git initgit add .git commit -m "init commit"# Replace <remoteRepositoryURL> with the# HTTPS URL of GitHub chocolatey repository.git remote add origin <remoteRepositoryURL>
Replace <remoteRepositoryURL>
with the HTTPS URL of GitHub Chocolatey repository.
- View the Git remotes to verify it’s connected to GitHub.
git remote -v
- Push the changes to GitHub.
git push -u origin master
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
ansible
directory.
cd ../ansible
- Create a new directory to store the non-Galaxy Chocolatey role.
mkdir -p nonGalaxy/chocolatey
- Move the non-Galaxy Chocolatey role.
mv roles/chocolatey/* nonGalaxy/chocolatey
- Remove the
roles/chocolatey
directory.
rmdir roles/chocolatey
- Copy the
tasks/main.yml
to the Galaxy role.
cp nonGalaxy/chocolatey/tasks/main.yml ../chocolatey/tasks/main.yml
- Copy the
files
contents to the Galaxy role.
cp nonGalaxy/chocolatey/files/* ../chocolatey/files/
- Push the changes to GitHub.
#change to the galaxy role directorycd ../chocolateygit add .# Configure user.email# Replace <Your email> with the actual emailgit config --global user.email "<YourEmail>"# Configure user.name# Replace <Your name> with the actual namegit config --global user.name "<YourName>"git commit -m 'added role logic and files'git push
- Run the
site.yml
playbook.
ansible-playbook site.yml -i hosts --ask-vault-pass
Missing role
The rolechocolatey
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.
- src: https://github.com/Duffney/chocolatey.gitscm: gitversion: "master"name: chocolatey
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 togit
because that’s the type of source control used. -
version
: defines which code to pull down. Set tomaster
to pull down the code from themaster
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.
ansible-galaxy install -r roles/requirements.yml
Run the site.yml
playbook.
ansible-playbook site.yml -i hosts --ask-vault-pass# skip chocolateyExtension tasksansible-playbook site.yml -i hosts --skip-tags chocolateyExtension --ask-vault-pass
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.yml
to use a tag.- Change the version to
v1.0
to reference the release tag instead of themaster
branch.
- Change the version to
- src: https://github.com/Duffney/chocolatey.gitscm: gitversion: "v1.0"name: chocolatey
- Re-install the Galaxy role.
ansible-galaxy install -r roles/requirements.yml -f
- Run the
site.yml
playbook. Update the<Password>
with the password created using theansible-vault
command in thegroup_vars/linux.yml
andgroup_vars/windows.yml
files using thenano
editor.
ansible-playbook site.yml -i hosts --ask-vault-pass# skip chocolateyExtension tasksansible-playbook site.yml -i hosts --skip-tags chocolateyExtension --ask-vault-pass
Practice all the commands in the terminal. We have provided a summarized view of the commands below:
# initialize the role directoryansible-galaxy init chocolatey --offline# View the directory structuretree# Change into chocolatey directorycd chocolatey# Connect Ansible Galaxy Role repositorygit init# Configure user.email# Replace <Your email> with the actual emailgit config --global user.email "<YourEmail>"# Configure user.name# Replace <Your name> with the actual namegit config --global user.name "<YourName>"git add .git commit -m "init commit"# Replace <remoteRepositoryURL> with the# HTTPS URL of GitHub chocolatey repository.git remote add origin <remoteRepositoryURL># Verify Connectivitygit remote -v# Push the changesgit push -u origin master# End - Connect Ansible Galaxy Role repository# Change to the ansible directorycd ../ansible# Create nonGalaxy/chocolatey directorymkdir -p nonGalaxy/chocolatey# Move the contents of roles/chocolatey to nonGalaxy/chocolateymv roles/chocolatey/* nonGalaxy/chocolatey# Remove the roles/chocolatey directoryrm -rf roles/chocolatey# Copy the taskscp nonGalaxy/chocolatey/tasks/main.yml ../chocolatey/tasks/main.yml# Copy the filescp nonGalaxy/chocolatey/files/* ../chocolatey/files/#change to the galaxy role directorycd ../chocolateygit add .git commit -m 'added role logic and files'git push# Change directorycd ../ansible# Execute the playbookansible-playbook site.yml -i hosts --ask-vault-pass# Create requirements.ymltouch roles/requirements.yml# Add Contentnano roles/requirements.yml# Contents# - src: https://github.com/Duffney/chocolatey.git# scm: git# version: "master"# name: chocolatey# Save ChangesPress Ctrl + oPress enter# Install Roleansible-galaxy install -r roles/requirements.yml# Execute the playbookansible-playbook site.yml -i hosts --ask-vault-pass# skip chocolateyExtension tasksansible-playbook site.yml -i hosts --skip-tags chocolateyExtension --ask-vault-pass# Update Contentnano roles/requirements.yml# Updated Contents# - src: https://github.com/Duffney/chocolatey.git# scm: git# version: "v1.0"# name: chocolatey# Save ChangesPress Ctrl + oPress enter# Re-Install Roleansible-galaxy install -r roles/requirements.yml -f# Execute the playbookansible-playbook site.yml -i hosts --ask-vault-pass# skip chocolateyExtension tasksansible-playbook site.yml -i hosts --skip-tags chocolateyExtension --ask-vault-pass
Troubleshooting tips
Missing role
If you receive the errorchocolatey
was 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.
Get hands-on with 1300+ tech skills courses.