Prepare an Ansible Project with Git

AnsibleBeginner
Practice Now

Introduction

Ansible work is easier to maintain when each automation task lives in a clear project directory. RHCE-style Ansible work also expects source control habits because playbooks, inventories, variables, and roles are all text files that change over time.

In this lab, you will create a small Ansible project under /home/labex/project/rhce-ansible, initialize it as a Git repository, add a first inventory, and commit the baseline. The project will be intentionally small, but the layout matches the structure used throughout this prep course.

Create the Project Workspace

In this step, you will create the project directory and the first set of folders used by Ansible projects. The option -p tells mkdir to create missing parent directories and to avoid failing if a directory already exists.

Move into the standard LabEx workspace.

cd /home/labex/project

Create a project directory with common subdirectories for inventories, playbooks, variables, files, templates, and roles.

mkdir -p rhce-ansible/{inventory,playbooks,group_vars,host_vars,roles,files,templates}

The directory names are conventional rather than mandatory. Using them early helps you recognize where later labs place inventory files, variables, reusable role code, and source files that playbooks copy or template.

List the new project tree. The output should show the directories you just created.

find rhce-ansible -maxdepth 2 -type d | sort

find prints the directories in the project tree, and sort makes the order stable. At this point the directories are empty, but their presence is the baseline structure you will commit later.

Initialize Git and Set Local Identity

In this step, you will turn the project into a Git repository. Git stores change history in a hidden .git directory. Setting identity locally keeps this lab self-contained and avoids changing global user settings.

Enter the project directory.

cd /home/labex/project/rhce-ansible

Initialize the repository.

git init

Git creates a hidden .git directory inside the project. That directory is the repository database; the visible Ansible files stay in the normal project tree.

Set a local author name and email for commits in this repository.

git config user.name "LabEx RHCE Learner"
git config user.email "learner@example.com"

Check the repository status. Git should report that you are on a branch with no tracked files yet.

git status --short

The short status format is intentionally compact. Empty directories are not tracked by Git, so it is normal if this command prints little or nothing until you add real files.

Add Project Notes and Ignore Rules

In this step, you will add two simple project files. A README.md explains the purpose of the project, and .gitignore keeps temporary files out of Git history.

Before running commands in this step, return to the project directory so relative paths refer to the Ansible project.

cd /home/labex/project/rhce-ansible

Use a here-document to create README.md. The text between EOF markers is written into the file exactly as shown.

cat > README.md <<'EOF'
## RHCE Ansible Practice Project

This repository contains guided Ansible practice files for RHCE-style automation.
EOF

Create a small .gitignore file for common Ansible temporary artifacts.

cat > .gitignore <<'EOF'
*.retry
.artifacts/
__pycache__/
EOF

The *.retry pattern covers retry files from older Ansible workflows, .artifacts/ is a common place for generated output, and __pycache__/ keeps Python bytecode out of your project history.

Display both files so you can confirm their contents before tracking them.

sed -n '1,20p' README.md .gitignore

Create the First Inventory

In this step, you will create a small static inventory. An inventory names the systems Ansible can manage. For the first lab, the host named control uses ansible_connection=local, which means Ansible runs tasks on this same VM without SSH.

Before running commands in this step, return to the project directory so relative paths refer to the Ansible project.

cd /home/labex/project/rhce-ansible

Create inventory/hosts.

cat > inventory/hosts <<'EOF'
[control]
control ansible_connection=local
EOF

The bracketed [control] line defines a group. The next line defines a host alias named control, and ansible_connection=local tells Ansible not to use SSH for this first project.

Use ansible-inventory to parse the file. This command checks inventory syntax before you use it in a playbook.

ansible-inventory -i inventory/hosts --graph

The graph should include the group control and the host control.

Commit the Baseline Project

In this step, you will add the baseline files to Git and create the first commit. A commit is a named checkpoint that you can inspect later.

Before running commands in this step, return to the project directory so relative paths refer to the Ansible project.

cd /home/labex/project/rhce-ansible

Stage the files. Staging tells Git which changes should be included in the next commit.

git add README.md .gitignore inventory/hosts

Create the baseline commit.

git commit -m "Create Ansible project baseline"

The commit output should mention the files added to the repository. If Git says there is nothing to commit, rerun git status --short and make sure README.md, .gitignore, and inventory/hosts were staged.

Inspect the final status and most recent commit. A clean status means all tracked changes are committed.

git status --short
git log --oneline -1

git status --short should now be empty, and git log --oneline -1 should show the new commit message. That gives you a known-good checkpoint before later labs add more Ansible content.

Summary

You created a clean Ansible project workspace, initialized Git, added a basic inventory, and committed the first baseline. These project habits will make later playbooks, variables, templates, roles, and validation files easier to manage.