Bookmark this page

Guided Exercise: Building a Custom Automation Execution Environment

  • Create a custom automation execution environment that provides a specific Ansible Content Collection and its supporting software.

Outcomes

  • Create the configuration files needed for a custom automation execution environment.

  • Build a new automation execution environment.

  • Verify that the new automation execution environment contains the specified Ansible Content Collection.

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

This command creates an Ansible project in the /home/student/builder-custom/ directory.

[student@workstation ~]$ lab start builder-custom

Procedure 9.2. Instructions

  1. Review some of the files in the exercise.motd custom Ansible Content Collection. This collection contains a role that updates the Message of the Day file that is displayed when users log in.

    1. Change to the ~/builder-custom/exercise.motd/ directory and extract the ~/builder-custom/exercise.motd.tar.gz archive.

      [student@workstation ~]$ cd ~/builder-custom/exercise.motd/
      [student@workstation exercise.motd]$ tar -xzf ../exercise.motd.tar.gz
    2. Use the tree command to list the files in the working directory.

      [student@workstation exercise.motd]$ tree
      .
      ├── FILES.json
      ├── galaxy.yml
      ├── MANIFEST.json
      ├── README.md
      └── roles
          └── banner
              ├── defaults
              │   └── main.yml
              ├── meta
              │   └── main.yml
              ├── README.md
              ├── tasks
              │   └── main.yml
              └── templates
                  └── motd.j2
      
      6 directories, 9 files
    3. Display the content of the motd.j2 template file.

      [student@workstation exercise.motd]$ cat roles/banner/templates/motd.j2
      ================================================================================
      ================================================================================
      ==                                                                            ==
      {{ formatted_msg_1 }}
      {{ formatted_msg_2 }}
      ==                                                                            ==
      ================================================================================
      ================================================================================

      The banner role creates the /etc/motd.d/banner file using this template.

    4. Display the content of the main task file in the banner role.

      [student@workstation exercise.motd]$ cat roles/banner/tasks/main.yml
      ---
      # tasks file for banner
      - name: Ensure /etc/motd.d/ exists
        ansible.builtin.file:
          path: /etc/motd.d
          state: directory
          owner: root
          group: root
          mode: '0755'
      
      - name: Create custom motd banner
        vars:
          left_pad_1: "{{ (width - (message_1 | length)) // 2 }}"
          left_pad_2: "{{ (width - (message_2 | length)) // 2 }}"
          right_pad_1: "{{ left_pad_1|int + ((width - (message_1 | length)) % 2) }}"
          right_pad_2: "{{ left_pad_2|int + ((width - (message_2 | length)) % 2) }}"
          formatted_msg_1: "=={% for x in range((left_pad_1|int) - 1) %} {% endfor %}{{ message_1 }}{% for y in range((right_pad_1|int) - 1) %} {% endfor %}=="
          formatted_msg_2: "=={% for x in range((left_pad_2|int) - 1) %} {% endfor %}{{ message_2 }}{% for y in range((right_pad_2|int) - 1) %} {% endfor %}=="
        ansible.builtin.template:
          src: motd.j2
          dest: /etc/motd.d/banner

      The banner role uses the width, message_1, and message_2 variables to align two custom messages in the center of the /etc/motd.d/banner file. When using the role, users might override the default value of the message_1 and message_2 variables.

  2. Create the configuration files for the new automation execution environment.

    1. Create and change into the ~/builder-custom/ee-motd/ directory.

      [student@workstation exercise.motd]$ mkdir ~/builder-custom/ee-motd/
      [student@workstation exercise.motd]$ cd ~/builder-custom/ee-motd/
    2. Create the execution-environment.yml file with the following content:

      ---
      version: 1
      build_arg_defaults:
        EE_BASE_IMAGE: 'hub.lab.example.com/ee-minimal-rhel8:latest'
        EE_BUILDER_IMAGE: 'hub.lab.example.com/ansible-builder-rhel8:latest'
      dependencies:
        galaxy: requirements.yml
        python: requirements.txt
        system: bindep.txt

      The lab command downloaded the ee-minimal-rhel8 container image to the workstation machine. This container image can be used as a base image for additional customizations.

      Note

      You can use the ~/builder-custom/solutions/execution-environment.yml file for comparison.

    3. Create the requirements.yml file with the following content:

      ---
      collections:
        - name: /build/exercise.motd.tar.gz
          type: file

      Note

      The path to the exercise.motd.tar.gz archive file is the absolute path within a container used during the build process.

      You can use the ~/builder-custom/solutions/requirements.yml file for comparison.

    4. Create the requirements.txt file for the Python package requirements with the following content:

      # Python dependencies
      funmotd

      Note

      You can specify the version number for the Python package. For example:

      funmotd==0.3

      Use the ~/builder-custom/solutions/requirements.txt file for comparison.

    5. Create the bindep.txt file with the following content:

      # System-level dependencies
      hostname

      Use bindep.txt file to specify the system packages to install. Add one package per line.

      Note

      As with the Python packages, you can specify the version to install.

      Use the ~/builder-custom/solutions/bindep.txt file for comparison.

  3. Verify that ansible-builder is installed.

    [student@workstation ee-motd]$ rpm -qa | grep ansible
    python39-ansible-runner-2.2.1-1.el8ap.noarch
    ansible-runner-2.2.1-1.el8ap.noarch
    ansible-navigator-2.1.0-1.el8ap.noarch
    ansible-builder-1.1.0-3.el8ap.noarch
    ansible-core-2.13.4-2.el8ap.x86_64

    Note

    If ansible-builder is not installed, install the ansible-builder package. The password for the student user is student.

    [student@workstation ee-motd]$ sudo yum install ansible-builder
  4. Create the automation execution environment.

    1. Use the ansible-builder create command to create additional files used during the build process.

      [student@workstation ee-motd]$ ansible-builder create
      Complete! The build context can be found at: /home/student/builder-custom/ee-motd/context

      Note

      Usually, running the ansible-builder create command is optional. Running the ansible-builder build command creates the context directory and associated files.

      In this exercise, you add an additional file to the context/_build/ directory.

    2. Use the tree command to list the files in the current directory. Notice how the ansible-builder command created a context directory with a Containerfile file and an _build subdirectory.

      [student@workstation ee-motd]$ tree
      .
      ├── bindep.txt
      ├── context
      │   ├── _build
      │   │   └── bindep.txt
      │   │   └── requirements.txt
      │   │   └── requirements.yml
      │   └── Containerfile
      ├── execution-environment.yml
      └── requirements.txt
      └── requirements.yml
      
      2 directories, 8 files
    3. Copy the ~/builder-custom/exercise.motd.tar.gz archive to the context/_build/ directory.

      [student@workstation ee-motd]$ cp ../exercise.motd.tar.gz context/_build/

      The build process mounts the contents of the context/_build/ directory into the container at the /build/ mount point. Consequently, the context/_build/exercise.motd.tar.gz archive is accessible to the container at the location specified in the requirements.yml file: /build/exercise.motd.tar.gz

    4. Use the ansible-builder build command to build the custom automation execution environment. Label the resulting container image with the ee-motd-minimal:1.0 tag. The ansible-builder build command might take a few minutes to complete.

      [student@workstation ee-motd]$ ansible-builder build \
      > -t ee-motd-minimal:1.0
      Running command:
        podman build -f context/Containerfile -t ee-motd-minimal:1.0 context
      Complete! The build context can be found at: /home/student/builder-custom/ee-motd/context

      Note

      If you have not authenticated to hub.lab.example.com, then you receive an authorization error. Use the podman login hub.lab.example.com command to authenticate the session. When prompted, enter student for the username and redhat123 for the password.

      Important

      The ansible-builder build command displays an associated podman build command. Use the podman build command if you make customizations to the context/Containerfile file that cannot be made by modifying the execution-environment.yml file.

    5. Run the podman images command to list local container images. The image ID for your container might be different from the one displayed.

      [student@workstation ee-motd]$ podman images localhost/ee-motd-minimal
      REPOSITORY                   TAG  IMAGE ID      CREATED         SIZE
      localhost/ee-motd-minimal    1.0  122452abb43b  18 seconds ago  409 MB
  5. Use the ansible-navigator images command to examine the ee-motd-minimal container image.

    1. Verify that ansible-navigator is installed.

      [student@workstation ee-motd]$ rpm -qa | grep ansible
      python39-ansible-runner-2.2.1-1.el8ap.noarch
      ansible-runner-2.2.1-1.el8ap.noarch
      ansible-navigator-2.1.0-1.el8ap.noarch
      ansible-builder-1.1.0-3.el8ap.noarch
      ansible-core-2.13.4-2.el8ap.x86_64

      Note

      If the ansible-navigator package is not installed, install it.

      [student@workstation ee-motd]$ sudo yum install ansible-navigator
    2. Use ansible-navigator command to verify that the custom automation execution environment exists locally. Specify the name of the automation execution environment image as localhost/ee-motd-minimal with the tag 1.0, and use never as the image pull policy.

      [student@workstation ee-motd]$ ansible-navigator images --pp never \
      > --eei localhost/ee-motd-minimal:1.0
      ...output omitted...
    3. Locate the ee-motd-minimal image and type the number associated with that name. If the number of the container image is greater than nine, then type :NUMBER and press Enter. This is similar to navigating to a specific line number using the Vi and Vim editors.

        Image                          Tag       Execution environment   ...
      0│ansible-builder-rhel8          latest    False                   ...
      1│ee-minimal-rhel8               latest    True                    ...
      2│ee-motd-minimal                1.0       True                    ...
    4. Type the number for the Ansible version and collections line.

        Image: ee-motd-minimal:1.0         Description
      0│Image information                  Information collected from image inspection
      1│General information                OS and python version information
      2│Ansible version and collections    Information about ansible and ansible ...
      3│Python packages                    Information about python and python packages
      4│Operating system packages          Information about operating system packages
      5│Everything                         All image information
    5. The resulting output indicates that the automation execution environment contains the exercise.motd content collection.

      Image: ee-motd-minimal:1.0 (primary) (Information about ansible and  ...
       0│---
       1│ansible:
       2│  collections:
       3│    details:
       4│      exercise.motd: 1.0.0
       5│  version:
       6│    details: ansible [core 2.13.4]
    6. Use the Esc key to go back one level, then type the number for the Python packages line.

        Image: ee-motd-minimal:1.0 (pr...  Description
      0│Image information                  Information collected from image inspection
      1│General information                OS and python version information
      2│Ansible version and collections    Information about ansible and ansible ...
      3│Python packages                    Information about python and python packages
      4│Operating system packages          Information about operating system packages
      5│Everything                         All image information
    7. Verify that the funmotd package is in the list of installed Python packages.

         Name             Version  Summary
       0│ansible-compat   2.0.2        Ansible compatibility goodies
       1│ansible-core     2.13.4       Radically simple IT automation
       2│ansible-lint     6.0.2        Checks playbooks for practices and ...
       3│ansible-runner   2.2.1        "Consistent Ansible Python API and
       4│bcrypt           3.2.0        Modern password hashing for your s....
       5│bracex           2.2.1        Bash style brace expander.
       6│cffi             1.14.3       Foreign Function Interface for Pyt ...
       7│chardet          3.0.4        Universal encoding detector for Pyt...
       8│commonmark       0.9.1        Python parser for the CommonMark Ma...
       9│cryptography     3.3.1        cryptography is a package which pro...
      10│decorator        5.0.7        Decorators for Humans
      11│docutils         0.16         Docutils -- Python Documentation Ut...
      12│enrich           1.2.7        enrich
      13│funmotd          1.1          TV Show and Movie Quotes MOTD for...
       ...output omitted...
    8. Use the Esc key to go back one level, and then type the number for the Operating system packages line.

        Image: ee-motd-minimal:1.0 (pr...  Description
      0│Image information                  Information collected from image inspection
      1│General information                OS and python version information
      2│Ansible version and collections    Information about ansible and ansible ...
      3│Python packages                    Information about python and python packages
      4│Operating system packages          Information about operating system packages
      5│Everything                         All image information
    9. Verify that the hostname RPM package you specified as a dependency in bindep.txt is included in the container image.

       ...output omitted...
       37│grep           3.1                Pattern matching utilities
       38│gzip           1.9                The GNU data compression program
       39│hostname       3.20               Utility to set/show the host n...
       40│info           6.5                A stand-alone TTY-based reader...
       41│json-c         0.13.1             JSON implementation in C
       ...output omitted...

      Type :q and press Enter to exit the ansible-navigator command when finished.

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 builder-custom

This concludes the section.

Revision: do374-2.2-82dc0d7