Clone an existing Git repository that contains an Ansible Playbook, make edits to files in that repository, commit the changes to your local repository, and push those changes to the original repository.
Outcomes
Use basic Git commands to manage both new and modified files stored in an existing Git repository.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command creates the Git repository needed for the exercise.
[student@workstation ~]$ lab start develop-git
Procedure 1.2. Instructions
Clone the https://git.lab.example.com/student/develop-git.git repository into the /home/student/git-repos directory and then create a new branch for this exercise.
From a terminal, create the /home/student/git-repos directory if it does not already exist, and then change into it.
[student@workstation ~]$mkdir -p ~/git-repos/[student@workstation ~]$cd ~/git-repos/
Clone the https://git.lab.example.com/student/develop-git.git repository and then change into the cloned repository:
[student@workstation git-repos]$git clone \>https://git.lab.example.com/student/develop-git.gitCloning into 'develop-git'... ...output omitted... [student@workstation git-repos]$cd develop-git
Create the exercise branch.
[student@workstation develop-git]$ git checkout -b exercise
Switched to a new branch 'exercise'Configure the global Git settings for the student user to specify a username, an email address, and a default push method.
Additionally, specify the username to use when connecting to https://git.lab.example.com and allow Git to remember your password for two hours.
Use the git config command to customize Git globally:
[student@workstation develop-git]$git config --global user.name 'Student User'[student@workstation develop-git]$git config --global \>user.email student@lab.example.com[student@workstation develop-git]$git config --global push.default simple[student@workstation develop-git]$git config --global \>credential.https://git.lab.example.com.username student[student@workstation develop-git]$git config --global \>credential.helper cache --timeout=7200
Verify the global configuration settings:
[student@workstation develop-git]$ git config --global -l
user.name=Student User
user.email=student@lab.example.com
push.default=simple
credential.https://git.lab.example.com.username=student
credential.helper=cacheReview the files in the project directory by using the tree command:
[student@workstation develop-git]$ tree
.
├── apache-setup.yml
└── templates
├── httpd.conf.j2
└── index.html.j2
1 directory, 3 filesThe apache-setup.yml file is the playbook.
The httpd.conf.j2 template customizes the Apache server configuration.
The index.html.j2 template generates a basic index page for the server.
Modify the apache-setup.yml playbook to target the web_servers host group.
Create the inventory file in the project directory with the following content:
[web_servers] serverd
Create an Ansible configuration file in the project directory that uses the inventory file and connects to remote hosts as the devops user.
The new ansible.cfg file should consist of the following content:
[defaults] inventory = inventory remote_user = devops
Modify the apache-setup.yml playbook to target the web_servers host group.
The top of the modified playbook contains the following content:
---
- name: Install web servers with WSGI interface
hosts: web_servers
become: true
vars:
httpd_packages:
- httpd
- python3-mod_wsgi
apache_test_message: This is a test message
apache_max_keep_alive_requests: 115
...output omitted...Review the current status of the local Git repository.
[student@workstation develop-git]$git statusOn branch exerciseChanges not staged for commit:(use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory)modified: apache-setup.ymlUntracked files:(use "git add <file>..." to include in what will be committed)ansible.cfginventoryno changes added to commit (use "git add" and/or "git commit -a")
Use the git add command to stage all three files for the next commit.
[student@workstation develop-git]$git add apache-setup.yml ansible.cfg \>inventory
Verify that all three files are staged.
[student@workstation develop-git]$git statusOn branch exerciseChanges to be committed:(use "git restore --staged <file>..." to unstage)new file: ansible.cfgmodified: apache-setup.ymlnew file: inventory
Commit the changes with the message: Target the web_servers group
[student@workstation develop-git]$ git commit -m "Target the web_servers group"
[exercise cc04da0] Target the web_servers group
3 files changed, 6 insertions(+), 1 deletion(-)
create mode 100644 ansible.cfg
create mode 100644 inventoryPush the locally committed changes to the remote server.
If prompted, use Student@123 as the password.
[student@workstation develop-git]$git push -u origin exercisePassword for 'https://student@git.lab.example.com':Student@123Enumerating objects: 7, done. Counting objects: 100% (7/7), done. Delta compression using up to 4 threads Compressing objects: 100% (4/4), done. Writing objects: 100% (5/5), 494 bytes | 494.00 KiB/s, done. Total 5 (delta 1), reused 0 (delta 0), pack-reused 0 remote: remote: To create a merge request for exercise, visit: remote: https://git.lab.example.com/student/develop-git/-/merge_requests/new?merge_request%5Bsource_branch%5D=exercise remote: To https://git.lab.example.com/student/develop-git.git * [new branch] exercise -> exercise Branch 'exercise' set up to track remote branch 'exercise' from 'origin'.
Run the apache-setup.yml playbook using automation content navigator and then verify the content displayed by the web server.
Use the podman login command to log in to the classroom private automation hub at hub.lab.example.com.
When prompted, enter student as the username and redhat123 as the password.
This step simulates a customer logging in to registry.redhat.io and ensures that the specified automation execution environment is pulled down to the local system if it does not already exist.
[student@workstation develop-git]$podman login hub.lab.example.comUsername:studentPassword:redhat123Login Succeeded!
Using the ee-supported-rhel8:latest execution environment, run the apache-setup.yml playbook.
[student@workstation develop-git]$ansible-navigator run apache-setup.yml \>--eei ee-supported-rhel8:latestPlay name Ok Changed ... Failed ... Task count Progress 0│Install web servers with ... 8 7 ... 0 ... 8 Complete ^f/PgUp page up ^b/PgDn page down ↑↓ scroll esc back ...Successful
Press ESC to exit the ansible-navigator command.
Use the curl command to review the content displayed by the web server.
[student@workstation develop-git]$ curl serverd
This is a test message RedHat 8.4 <br>
Current Host: serverd <br>
Server list: <br>
serverd <br>Review the status of the local Git repository.
The ansible-navigator command created the .ssh/ directory, the ansible-navigator.log file, and one artifact file for each time that you used the ansible-navigator command to run a playbook.
[student@workstation develop-git]$git statusOn branch exercise Your branch is up to date with 'origin/exercise'.Untracked files:(use "git add <file>..." to include in what will be committed)ansible-navigator.logapache-setup-artifact-2022-09-12T20:00:56.911737+00:00.jsonnothing added to commit but untracked files present (use "git add" to track)
Create a new .gitignore file to ignore files and directories created by the ansible-navigator command.
The .gitignore file contains the following content:
ansible-navigator.log *-artifact-*
Review the status of the local Git repository. Files and directories that should not be committed are now ignored.
[student@workstation develop-git]$git statusOn branch exercise Your branch is up to date with 'origin/exercise'.Untracked files:(use "git add <file>..." to include in what will be committed).gitignorenothing added to commit but untracked files present (use "git add" to track)
Add and commit the new .gitignore with the message: Ignore generated files
[student@workstation develop-git]$git add .gitignore[student@workstation develop-git]$git commit -m "Ignore generated files"[exercise b780076] Ignore generated files 1 file changed, 2 insertions(+) create mode 100644 .gitignore
Push the latest commits to the remote repository.
If prompted, use Student@123 as the password.
[student@workstation develop-git]$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 323 bytes | 323.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for exercise, visit:
remote: https://git.lab.example.com/student/develop-git/-/merge_requests/new?merge_request%5Bsource_branch%5D=exercise
remote:
To https://git.lab.example.com/student/develop-git.git
6f87080..4ec3448 exercise -> exerciseIn a more realistic scenario, you would also create a merge request or a pull request so that changes could be incorporated into the main branch.