Sergey Nivens - Fotolia

Adopt the Git version control system for configuration as code

Configuration files define infrastructure as code, which enables IT admins to control updates and enforce standardization. Start using a version control system to codify configurations with just a handful of commands.

When IT teams manage environments at a large scale, even the most minor change can have major consequences. Version control plays a critical role in IT infrastructure configuration stability and accountability.

Version control, while common in developer toolkits, is a new addition for many IT admins. The Git version control system, which is open source and instituted across many types of IT organizations, enables users to manage not only source code, but all manner of configuration files. Users save files and changes into Git with commits. Therefore, IT admins can write configuration as code, which means they control IT infrastructure via a template, such as YAML, with scripted provisioning steps. Configuration as code enforces standardization in a large environment and helps prevent issues across complex setups.

Git also enables developers to easily track changes across their source code, wherein they can roll back, revert and create new versions -- or branches -- in parallel. Basic Git commands and concepts are easy to learn, and using Git is the same across different systems.

Initialize Git

Download and install the appropriate version from Git's website; the specifics of an installation might depend on the particular OS.

This demo of the Git version control system is based on the Windows root directory. Create a folder called source in the root directory for the source code, and enter it.

Git initialization command
Figure 1. Initialize Git in a command prompt.

Open a command prompt, and initialize Git by typing git init in the source folder (Figure 1).

Use a basic configuration string to specify repository ownership. Ownership information is used for commits. Configure Git using:

     git config –global "[email protected]"

     git config –global "My Name"

Basic Git commands

You can now add files into the system and subject them to version control via the git add command. If configuration files are not added to Git, they are considered uncontrolled and, therefore, should be ignored. To add a single file -- that has already been created -- use:

     git add myfile.txt

Source file for Git
Figure 2. This simple text file is the source code for this example of using Git.

Alternatively, wildcards -- for example, git add * -- enable the user to add all the files that are in the source folder. However, if you don't add the files after changes are made, Git will not recognize the new version and will thus only display the old version. Step one is to save the file, but the vital second step is to add that updated file to Git.

Unique code commit identifier
Figure 3. This code commit uses a unique identifier for easy rollbacks.

The files are now in the system, but to commit the files -- i.e., create a point-in-time copy -- use the git commit -m function. The commit requires options such as comments. Note in Figure 3 the string 8e93636, which is the commit ID. This unique identifier enables Git users to roll back to a previous version of a file at any point in time.

The Git version control system now recognizes and manages the file. To add a second file, use the following commands:

     git add secondfile.txt

     git commit -m "Second source code file added"

Return to the first file, and edit it to add a second line, creating a new version. This simple example illustrates how an IT administrator would update, for example, network configuration settings for a deployed VM or other configuration-as-code files.

Edit the first file
Figure 4. The file has been altered, requiring a new commit with comment.

Re-add the file to Git with git add *, and commit it with a comment -- git commit -m "Added second line" -- to provide information about what changed.

Update file with comment
Figure 5. Specify file changes in a comment.

Git displays information on the number of changes made to a given file. This feature enables a user to compare, contrast and revert code versions as necessary. Use the command git log to view the history of file changes and commits.

The git show command (Figure 6) displays the differences between file versions.

File changes are displayed by git show
Figure 6. The command git show highlights changes between file versions. Red lines prefixed by a minus are lines that have been removed; green lines prefixed with a plus are additions.

To revert a file, check out the file with the Git checkout [commitid] filename command.

The contents of the file will revert any changes after that file version, as if it never existed. Use the command with caution, and maintain a careful system of backups.

For a single user, the version control system is simple enough, once you master basic Git commands and concepts. However, for a team of users, a centralized, multiuser platform coordinates and manages that system more efficiently. Many version control tools are marketed to enterprises, including GitHub, Microsoft Team Foundation Version Control, Atlassian Bitbucket and Perforce. Some vendor products are based on the Git open source technology, while others use a different approach.

IT admins interested in using Git for configuration as code but deterred by the command-line interface should investigate GUI-based tools, but only once they have a solid understanding of the underlying concepts of version control.

Unsurprisingly, Git itself is version-controlled and offers a comprehensive guidebook.

Next Steps

New to Git and distributed version control? Here are some Git examples and Jenkins-Git integration tutorials designed to help you master the popular source code versioning tool.

Dig Deeper on Systems automation and orchestration

Software Quality
App Architecture
Cloud Computing
Data Center