Ubuntu Server 18.04 – Creating a Git repository

How to install Chocolatey on Windows 10

For the examples in this chapter, it’s recommended that you create a Git repository to store your Ansible code. This isn’t required, as you can find other ways of hosting your code, but it’s highly recommended. This is especially true when we get to the pull method of Ansible at the end of this chapter. In this section, I’ll walk you through creating a repository. If you already know how to use GitHub, you can skip this section.

While a full walkthrough of Git is beyond the scope of this tutorial, the basics are more than enough for following along here. When it comes to Git, you can simply install the git package on a server to have it host your code, but GitHub is probably the easiest way to get started. An added bonus is that GitHub is home to a lot of great projects you can benefit from, and browsing the code of these projects is a great way to become more accustomed to syntax rules with different scripting and programming languages. For our purposes, we’re going to use GitHub as a central place to store our Ansible code.

It probably goes without saying, but GitHub is a public resource. Any code you upload to the service will be available for all to see. Therefore, be mindful of the information you commit to your repository. Make sure you don’t include any personally identifiable information, passwords, or anything else you don’t want the public to know about you or your organization.

To get started, create an account at https://www.github.com if you don’t already have one, which is free. Make sure you create a reasonably secure password here. After you create an account, click on New repository, and then give it a name (simply calling it ansible is fine):

Creating an Ansible repository on GitHub

In the example screenshot, I created a repository that is Public, which means exactly that—anyone will be able to view the code. You can create Private repositories as well, but there is a charge for that. Since we’re not going to include confidential information in the repository, we won’t need to worry about that.

Once the repository is created, we’ll need to download it locally. For that, we’ll need to install the git package:

sudo apt install git

Next, we should set up our local git client so that we can fill out our name and email address, otherwise git will most likely complain. To do this, we can issue the following commands, substituting the content in the quotes with your information:

git config user.email "you@example.com"
git config user.name "John Doe"

To download our repository, the following will do the trick (ignore the warning about the repository being empty if you see such a message):

git clone https://github.com/myusername/ansible.git

Now you have the Git repository downloaded locally. Right now, the repository doesn’t include anything useful. To create a file within the repository, you simply change your working directory to be inside the repository folder that was downloaded when you cloned it, and create whatever files you want inside. By default, Git doesn’t care about any files you create inside the repository until you add them. For example, we can create a test file and commit it to the repository with the following commands:

echo "this is a test" > testfile.txt
git add testfile.txt
git commit -m "initial commit"

With these commands, we used echo to create a test file with some text in it. Then, we used the git add command to tell Git that we want the file to be a part of our repository. Finally, we finalized our changes by using git commit, along with the -m flag and a message about the commit.

At this point, the changes only exist locally. To push our changes back to GitHub, we use the following command from inside the repository directory:

git push origin master

By following the on-screen prompts (GitHub username and password), our changes will be placed inside our actual repository.

So, how does this help us when it comes to configuration management? Typically, the code administrators use to provision servers will be placed inside a repository for safe-keeping. If the local copy of the code is lost, it can simply be cloned again from the repository. GitHub is a safe place to put our code, since we can be reasonably sure that our code won’t disappear as the service is very stable (you still may want to create a local backup to be safe). Whether you’re using Ansible, Chef, Puppet, or another utility, it’s common practice to keep the code in a Git repository. In regards to Ansible, this will directly impact us in the last section of this chapter since we’ll be using the ansible-pull command, which actually expects a repository URL.

In practice, when we create Ansible Playtutorials, you should commit those changes back to the repository. I won’t specifically call on you to do that, so go ahead and keep that in mind as we go. When you create a new Playtutorial, add it to the repository, then commit it. If you make changes to existing files, commit those changes. Be sure to use the git push command to push your changes back to the repository. For example, if you created a file inside the repository named myplaytutorial.yml, you would execute commands such as these:

git add myplaytutorial.yml
git commit -m "insert message about the commit here"
git push origin master

Go ahead and practice this a bit before you move on. Even if you don’t use Ansible in production, understanding the basics of Git is invaluable, as you’ll almost definitely need it at some point in your career.

Comments are closed.