A few years ago I found myself writing a whole lot of Ansible Galaxy roles for both company and personal use. After the fourth or fifth time of going through the process of setting up the GitHub repository, creating the skeleton, and checking it all in, I decided to automate it. I’ve broken down what it does in this blog post and you can find it as the file createrole in the bin directory of my dotfiles repo.
Setting Up the Variables
The scripts starts with two variables that need to be set. The first is your GitHub API token (you can find instructions on how to set one up here). I use a function I created with 1Password to set the GIT_TOKEN environmental variable (you can see it in this post). The second variable is the name of your personal GitHub account. The need for this will become apparent later on in the script.
#****************
# Variables
#****************
authtoken=$GIT_TOKEN
my_gitaccount="austincloudguru"
Usage()
The usage function helps me to remember what I named the the options for the name of the Ansible Role (-n) and the GitHub organization/user (-g) to create it in.
usage()
{
echo -n "usage: $0 [OPTION]...
Script that easily creates an Ansible role in github
Options:
-n Name of the Ansible Role (required)
-g Name of the Github organization to create it in (required)
"
}
Getting Options
I use the built-in function getopts()
to parse my options and set variables to the inputs. Since both are required, I send an error and exit if an invalid option is used or an argument is not passed. I finish it up by ensuring both the variables were set properly and if they are not, to print the usage.
#****************
# getopts
#****************
while getopts ":pg:n:" opt; do
case $opt in
g)
gitaccount=$OPTARG
;;
n)
rolename=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Make sure rolname is set
if [ -z "$rolename" ]
then
usage
exit
fi
# Make sure gitaccount is set
if [ -z "$gitaccount" ]
then
usage
exit
fi
Check to See if the Repo Exists
Before creating a new GitHub repo, I want to check and make sure that it doesn’t already exist.
# Check to see if the Repo already exists
gitrepo=`curl -s -G https://api.github.com/search/repositories?q=$rolename+user:$gitaccount \
-H "Accept: application/vnd.github.preview" \
-H "Authorization: token $authtoken" \
|jq -r .items[].name`
# Exit if the repo already exists
if [ -n "$gitrepo" ]
then
echo "Repo Already Exists in Org!"
exit
fi
Create the new Repo
If the repo does not exist, then the next bit of code creates the repository. This is where the variable $my_gitaccount
is important. There are different endpoints depending on whether it is a user or an organization. If the $gitaccount
that is passed via the command line matches $my_gitaccount
set in the script, it uses the user endpoint, otherwise it uses the orgs endpoint. I then run the command and set the output of the SSH URL to the variable $giturl
.
# Create the Git Repository
if [ $gitaccount == "austincloudguru" ]
then
git_endpoint=user/repos
else
git_endpoint=orgs/$gitaccount/repos
fi
giturl=$(curl -s -X POST https://api.github.com/$git_endpoint \
-H "Authorization: token $authtoken" \
-d @<(cat <<EOF
{
"name": "$rolename",
"private": "$privaterepo"
}
EOF
)|jq -r .ssh_url
)
Create the Galaxy Role
With the GitHub repo created successfully, we move on to the creating the ansible galaxy role. Initially, I used the ansible-galaxy
command to initialize a role. Once I started using Molecule to test my Ansible roles, I updated the script to use the molecule init role
command. Once the role is created, we cd
into it so that we can initialize the repository.
# Create the Galaxy Role - Old Way
# ansible-galaxy init $rolename
# Create the Galaxy Role
molecule init role -r $rolename -d docker
cd $rolename
Initialize the Git Repository and Push it to GitHub
With the repository created in GitHub and role created locally, it is time to initialize the role as a git repository and then push it up to GitHub. We will use the $giturl
variable that we set when we created the repo to set the origin on the newly created repository and then push it up
# Initialize the Git Repo
git init
git add .
git commit -m "Commit Skeleton Role"
# Add the Remote and Push
git remote add origin $giturl
git push -u origin master
Report the Role Is Completed
We finish the script off by returning to the parent directory and printing a completed message.
cd ..
echo -e "\n\n\nCompleted setting up Ansible Galaxy Role"
That’s it. Now I’m able to start working on my new role.
Comments