In this tutorial we will be creating a ansible role, publish our ansible role to github, then we will install the role locally and create a ansible playbook to use the ansible role.
The source code for this blog post will be available on my github repository.
Ansible Installation
Create a virtual environment with Python:
1 2 |
|
Install ansible with pip:
1
|
|
Now that we have ansible installed, we can create our role.
Initialize Ansible Role
A Ansible Role consists of a couple of files, and using ansible-galaxy
makes it easy initializing a boilerplate structure to begin with::
1 2 |
|
The role that we created is named ssh_config
and will be placed under the directory roles
under our current working directory.
Define Role Tasks
Create the dummy task under roles/ssh_config/tasks/main.yml
:
Then define the defaults environment values in the file roles/ssh_config/defaults/main.yml
:
1 2 3 |
|
The value of ssh_port
will default to 22
if we don’t define it in our variables.
Commit to Git
The assumption is made here that you already created a git repository and that your access is sorted. Add the files and commit it to git:
1 2 3 |
|
Now your ansible role should be commited and visible in git.
SSH Config Client Side
I will be referencing the git source url via SSH, and since I am using my default ssh key, the ssh config isn’t really needed, but if you are using a different version control system, with different ports or different ssh keys, the following ssh config snippet may be useful:
1 2 3 4 5 |
|
If you won’t be using SSH as the source url in your ansible setup for your role, you can skip the SSH setup.
Installing the Ansible Role from Git
When installing roles, ansible installs them by default under: ~/.ansible/roles
, /usr/share/ansible/roles
or /etc/ansible/roles
.
From our previous steps, we still have the ansible role content locally (not under the default installed directory), so by saying installing the role kinda sounds like we are doing double the work. But the intention is that you have your ansible role centralized and versioned on git, and on new servers or workstations where you want to consume the role from, that specific role, won’t be present on that source.
To install the role from Git, we need to populate the requirements.yml
file:
1 2 |
|
The requirements file is used to define where our role is located, which version and the type of version control, the requirements.yml
:
1 2 3 4 5 6 |
|
For other variations of using the requirements file, you can have a look at their documentation
Then install the ansible role from our requirements file (I have used --force
to overwrite my current one while testing):
1 2 3 4 5 |
|
Ansible Playbook
Define the ansible playbook to use the role that we installed from git, in a file called playbook.yml
:
1 2 3 4 5 6 |
|
Run the ansible playbook:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Thank You
Thanks for reading, if you like my content, check out my website, read my newsletter or follow me at @ruanbekker on Twitter.