Bookmark this page

Guided Exercise: Managing Ansible Project Materials Using Git

  • 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

  1. 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.

    1. 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/
    2. 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.git
      Cloning into 'develop-git'...
      ...output omitted...
      [student@workstation git-repos]$ cd develop-git
    3. Create the exercise branch.

      [student@workstation develop-git]$ git checkout -b exercise
      Switched to a new branch 'exercise'
  2. 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.

    1. 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
    2. 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=cache
  3. Review 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 files
    • The 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.

  4. Modify the apache-setup.yml playbook to target the web_servers host group.

    1. Create the inventory file in the project directory with the following content:

      [web_servers]
      serverd
    2. 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
    3. 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...
    4. Review the current status of the local Git repository.

      [student@workstation develop-git]$ git status
      On branch exercise
      Changes 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.yml
      
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
              ansible.cfg
              inventory
      
      no changes added to commit (use "git add" and/or "git commit -a")
    5. 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
    6. Verify that all three files are staged.

      [student@workstation develop-git]$ git status
      On branch exercise
      Changes to be committed:
        (use "git restore --staged <file>..." to unstage)
              new file:   ansible.cfg
              modified:   apache-setup.yml
              new file:   inventory
    7. 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 inventory
    8. Push the locally committed changes to the remote server. If prompted, use Student@123 as the password.

      [student@workstation develop-git]$ git push -u origin exercise
      Password for 'https://student@git.lab.example.com': Student@123
      Enumerating 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'.
  5. Run the apache-setup.yml playbook using automation content navigator and then verify the content displayed by the web server.

    1. 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.com
      Username: student
      Password: redhat123
      Login Succeeded!
    2. 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:latest
      
        Play 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.

    3. 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>
    4. 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 status
      On 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.log
              apache-setup-artifact-2022-09-12T20:00:56.911737+00:00.json
      
      nothing added to commit but untracked files present (use "git add" to track)
    5. 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-*
    6. Review the status of the local Git repository. Files and directories that should not be committed are now ignored.

      [student@workstation develop-git]$ git status
      On branch exercise
      Your branch is up to date with 'origin/exercise'.
      
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
              .gitignore
      
      nothing added to commit but untracked files present (use "git add" to track)
    7. 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
    8. 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 -> exercise

      Important

      In 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.

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish develop-git

This concludes the section.

Revision: do374-2.2-82dc0d7