Bookmark this page

Lab: Managing Inventory Variables to Use with Automation Content Navigator

  • Manage group and inventory variables, configure automation content navigator settings, and push your changes to a new branch using Git.

Outcomes

  • Use Git to clone a repository, create a branch, ignore files, and push changes to a branch.

  • Configure basic automation content navigator settings.

  • Create Ansible group variables.

  • Create Ansible inventory variables in the YAML format.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command initializes the remote Git repository that you need for this lab. When you are ready to push your changes to the remote repository, use Student@123 as the Git password.

[student@workstation ~]$ lab start review-cr1

All the computer systems have a student user account with student as the password. The password for the root user account is redhat.

Specifications

  • Change an existing project that is stored in a Git repository:

    • The repository is stored at https://git.lab.example.com/student/review-cr1.git.

    • The repository must be cloned into the /home/student/git-repos/review-cr1 directory.

    • All your work must take place within the student/inventory-edits branch.

    • Git must be configured to not track or commit files with the .log file extension, or files in the project's .ssh/ and collections/ansible_collections/ directories.

    • Obtain a token from https://hub.lab.example.com, logging in as the student user with the password redhat123. Set the token in ansible.cfg configuration file provided.

  • Configure Ansible and automation content navigator as follows:

    • Ansible must be configured to use the inventory/ directory by default.

    • Automation content navigator must use the ee-supported-rhel8:latest image as the default automation execution environment and must not generate any playbook artifact files.

  • Configure group variables:

    • The /home/student/git-repos/review-cr1/haproxy.yml playbook contains the lb_service and firewall_services defined variables. To make this playbook more flexible, move these variables out of the playbook, and into the group_vars directory in the review-cr1 directory.

  • Configure inventory variables:

    • The /home/student/git-repos/review-cr1/webapp.yml playbook requires that the web_service and firewall_services variables be defined. Create a group_vars/web/vars.yml file to define these variables for the web group with the following values:

      VariableValue
      web_service httpd
      firewall_services http and https
    • The /home/student/git-repos/review-cr1/db.yml playbook runs plays against the db group. Add the db group to the existing inventory with the single host serverd.lab.example.com as a member. This playbook requires that the db_service and db_port variables be defined. Create a group_vars/db/vars.yml file to define these variables for the db group with the following values:

      VariableValue
      db_service mariadb
      db_port 3306/tcp
  • After all the variables are defined, use automation content navigator to run the following playbooks:

    • /home/student/git-repos/review-cr1/haproxy.yml

    • /home/student/git-repos/review-cr1/webapp.yml

    • /home/student/git-repos/review-cr1/db.yml

  • Navigate to http://servera.lab.example.com to test the web application. The web application displays This is the server server[bc] and the database is up.

  • After you confirm that the web application is running, commit and push your changes to the student/inventory-edits remote branch.

  1. On the workstation machine, clone the https://git.lab.example.com/student/review-cr1.git repository into the /home/student/git-repos/review-cr1/ directory, and create a branch named student/inventory-edits.

    1. Clone the repository to the /home/student/git-repos/review-cr1/ directory:

      [student@workstation ~]$ mkdir -p git-repos/review-cr1
      [student@workstation ~]$ git clone \
      > https://git.lab.example.com/student/review-cr1.git \
      > /home/student/git-repos/review-cr1/
    2. Change to the /home/student/git-repos/review-cr1/ directory. Create a branch named student/inventory-edits and switch to it:

      [student@workstation ~]$ cd /home/student/git-repos/review-cr1/
      [student@workstation review-cr1]$ git checkout -b student/inventory-edits
      [student@workstation review-cr1]$ git status
      On branch student/inventory-edits
      nothing to commit, working tree clean
  2. Configure Git to ignore any files with the .log file extension, and the .ssh/ and collections/ansible_collections/ directories. Git should not track or commit these files or directories.

    1. Create a /home/student/git-repos/review-cr1/.gitignore file with the following content:

      *.log
      .ssh/
      collections/ansible_collections
  3. Retrieve a token from https://hub.lab.example.com, logging in as the student user with the password redhat123, and then use it to update the token option in the ansible.cfg file. Use the ansible-galaxy command to install the collections. The collections must be available to the execution environment, so they must be installed in the /home/student/git-repos/review-cr1/collections/ directory.

    1. Log in to the private automation hub at https://hub.lab.example.com as the student user and using redhat123 as the password.

    2. Navigate to CollectionsAPI token management and then click Load token. Copy the API token.

    3. Using the copied token, update the token line in the ansible.cfg file. Your token is probably different from the one that is displayed in this example.

      ...output omitted...
      
      [galaxy_server.community_repo]
      url=https://hub.lab.example.com/api/galaxy/content/community/
      token=f41f07130d6eb6ef2ded63a574c161b509c647dd
    4. Use the ansible-galaxy command to install the community.mysql content collection into the collections/ directory.

      [student@workstation review-cr1]$ ansible-galaxy collection install \
      > -r collections/requirements.yml
      Starting galaxy collection install process
      ...output omitted...
      Installing 'community.mysql:3.5.1' to '/home/student/git-repos/review-cr1/collections/ansible_collections/community/mysql'
      community.mysql:3.5.1 was installed successfully
    5. Automation content navigator should use the ee-supported-rhel8:latest image as the default automation execution environment and should not generate playbook artifacts. Create the /home/student/git-repos/review-cr1/ansible-navigator.yml file with the following content:

      ---
      ansible-navigator:
        execution-environment:
          image: ee-supported-rhel8:latest
        playbook-artifact:
          enable: false
  4. Remove the variables from the /home/student/git-repos/review-cr1/haproxy.yml playbook to make it more flexible.

    1. Edit the /home/student/git-repos/review-cr1/haproxy.yml playbook and remove the vars: section. The completed playbook should contain the following content:

      ---
      - name: Deploy haproxy
        hosts: haproxy
        become: true
      
        tasks:
      ...output omitted...
    2. Create the /home/student/git-repos/review-cr1/group_vars/{haproxy,web,db} directories:

      [student@workstation review-cr1]$ mkdir -p \
      > /home/student/git-repos/review-cr1/group_vars/{haproxy,web,db}
    3. Create the /home/student/git-repos/review-cr1/group_vars/haproxy/vars.yml file and add the lb_service and firewall_services variables into it:

      ---
      lb_service: haproxy
      firewall_services:
        - http
        - https
    4. Create the /home/student/git-repos/review-cr1/group_vars/web/vars.yml file and add the web_service and firewall_services variables to it:

      ---
      web_service: httpd
      firewall_services:
        - http
        - https
    5. Create the /home/student/git-repos/review-cr1/group_vars/db/vars.yml file and add the db_service and db_port variables to it:

      ---
      db_service: mariadb
      db_port: 3306/tcp
    6. Add the db group to the /home/student/git-repos/review-cr1/inventory/inventory-static.yml inventory file with the single host serverd.lab.example.com:

      web:
        hosts:
          serverb.lab.example.com:
          serverc.lab.example.com:
      
      db:
        hosts:
          serverd.lab.example.com:
  5. Use automation content navigator to run the playbooks.

    1. Verify that automation content navigator is installed, and if not, install it.

      [student@workstation review-cr1]$ rpm -q ansible-navigator
      ansible-navigator-2.1.0-1.el8ap.noarch

      Note

      You might need to install automation content navigator:

      [student@workstation review-cr1]$ sudo yum install ansible-navigator
      [sudo] password for student: student
      ...output omitted...
    2. The /home/student/git-repos/review-cr1/haproxy.yml playbook runs against the haproxy group that is defined in the /home/student/git-repos/review-cr1/inventory/inventory-dynamic.py dynamic inventory file. This inventory file must have execute permissions before it can be used:

      [student@workstation review-cr1]$ chmod 755 inventory/inventory-dynamic.py
    3. Use the podman login command to log in to the private automation hub at hub.lab.example.com.

      [student@workstation review-cr1]$ podman login hub.lab.example.com
      Username: student
      Password: redhat123
      Login Succeeded!
    4. Run the /home/student/git-repos/review-cr1/haproxy.yml playbook:

      [student@workstation review-cr1]$ ansible-navigator run -m stdout haproxy.yml
      ...output omitted...
    5. Run the /home/student/git-repos/review-cr1/webapp.yml playbook:

      [student@workstation review-cr1]$ ansible-navigator run -m stdout webapp.yml
      ...output omitted...
    6. Run the /home/student/git-repos/review-cr1/db.yml playbooks:

      [student@workstation review-cr1]$ ansible-navigator run -m stdout db.yml
      ...output omitted...
  6. Verify that the web application is up:

    [student@workstation review-cr1]$ curl http://servera.lab.example.com
    This is the server serverc and the database is up
    [student@workstation review-cr1]$ curl http://servera.lab.example.com
    This is the server serverb and the database is up
  7. Push your changes to the student/inventory-edits branch.

    1. List all modified and untracked files:

      [student@workstation review-cr1]$ git status
      On branch student/inventory-edits
      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:    ansible.cfg
              modified:    haproxy.yml
              modified:    inventory/inventory-dynamic.py
              modified:    inventory/inventory-static.yml
      
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
              .gitignore
              ansible-navigator.yml
              group_vars/
      
      no changes added to commit (use "git add" and/or "git commit -a")
    2. Add the files you created and edited to the staging area:

      [student@workstation review-cr1]$ git add --all
    3. Commit the staged files to the local repository:

      [student@workstation review-cr1]$ git commit -m 'updating inventory'
      ...output omitted...
    4. Push the changes from the local repository to the student/inventory-edits branch on the remote repository. If prompted, use Student@123 as the Git password.

      [student@workstation review-cr1]$ git push -u origin student/inventory-edits
      Password for 'https://student@git.lab.example.com': Student@123
      ...output omitted...
    5. Verify that your changes were pushed to the student/inventory-edits branch:

      [student@workstation review-cr1]$ git status
      On branch student/inventory-edits
      Your branch is up to date with 'origin/student/inventory-edits'.
      
      nothing to commit, working tree clean

Evaluation

As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures and rerun the command until successful.

[student@workstation ~]$ lab grade review-cr1

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 review-cr1

This concludes the section.

Revision: do374-2.2-82dc0d7