π Introduction
In this section, we will create an execution environment using ansible-builder.
For this challenge, we are building an execution environment and the collection will be pulled from the Automation Hub that is baked into this lab. Our task for this challenge is to create an execution environment with the ansible.netcommon collection from Automation Hub on top of the minimal execution environment.
The editor we are using is VS Code and it may be unfamiliar for some folks. But we will keep the instructions simple enough and gradually build upon what we are doing in each challenge. If you are confused at any time, there is a folder called solution-definition adjacent to execution-environment.yml which will have the solution for each challenge. You can copy and replace the content for your current definition file if you are unable to solve any YAML errors.
|
Please go through the tasks in this challenge to understand how to interact with ansible-builder v3.
βοΈ Task - Basic definition file from last challenge
On the VS Code tab on the left side you will see that we have a tab called "file." Click File→Open folder and select the directory opened called minimal-downstream-from-hub, and this directory has a file called execution-environment.yml.
This file is the same as the one we created from the last challenge when we pulled the collection from Ansible Galaxy. This will be the starting point for this challenge, and we will slowly build on creating a definition file that can pull the collections from Automation Hub.
For this task, just verify the content of the definition file to ensure that it matches what we learned in the last challenge.
βοΈ Task - ansible.cfg file
We have created an ansible.cfg file in the files directory adjacent to our definition file.
This directory contains an ansible.cfg file which points to the Automation Hub in our lab. You can follow the documentation to understand how to build your own ansible.cfg file.
The idea here is to tell the ansible-galaxy CLI about where it needs to look for collections and also provide a token to download the collections from Automation Hub. We have added the URL of the rh_certified repo (as that repo has the netcommon collection) and the token alongside it.
With the execution environment build process, as ansible-builder would be downloading the collection and adding it to the execution environment, it needs to know about the ansible.cfg file if you want to build with collections from Automation Hub.
For this task, just check the content of the ansible.cfg file, as we will be using it in the execution environment definition in the next tasks.
βοΈ Task - Adding ansible.cfg to the definition file
The addition of ansible.cfg needs to happen in two steps:
-
Copy the contents of the
filesdirectory to the_build/configsdirectory in the build context. -
Copy the
ansible.cfgfile from the_build/configsdirectory to the Ansible config default location on the EE, which is/etc/ansible/ansible.cfg.
The _build directory inside the context directory contains all the additional build files. Itβs important to note the name of this directory as it will be important when you are building custom content into your execution environment, like we are doing in this task.
|
Add the following two sections to the end of the execution-environment.yml file:
additional_build_files:
# Copy arbitrary files next to this EE Definition into the build context
- src: files
dest: configs
additional_build_steps:
prepend_galaxy:
- COPY _build/configs/ansible.cfg /etc/ansible/ansible.cfg
In the above section, you can see that we added a section called additional_build_files. This tells ansible-builder where to copy the additional build files in the context. In this case, the files directory gets copied to _build/configs.
The next section, called additional_build_steps, tells the builder about the additional build steps it needs to take. This section can have multiple subsections, and you can consider each subsection as a build stage. In this case, we are prepending steps to the Galaxy stage (collection download stage) and telling the builder to copy the ansible.cfg in _build/configs to /etc/ansible/ansible.cfg.
The idea here is to copy the Ansible config file to the execution environment so that ansible-galaxy can point to the local Automation Hub.
βοΈ Task - Avoid SSL error while building
To avoid an SSL error, add the following section in the execution-environment.yml file:
build_arg_defaults:
ANSIBLE_GALAXY_CLI_COLLECTION_OPTS: '--ignore-certs'
This option will add the --ignore-certs option to the ansible-galaxy CLI when ansible-builder is building the execution environment. This is just a workaround β we will provide a permanent solution in the next challenge. The idea here is to describe different customizations you can do in the build process.
Please open the terminal window and re-run ansible-builder:
ansible-builder build -v 3
This command will take a minute or two to complete.
βοΈ Task - Let’s check what’s built
Run the following command to check the image that was created:
podman images
You will see that it builds an execution environment called localhost/ansible-execution-env by default. This is our execution environment built with the collection we need.
You can tag the image post-build if you want using the podman CLI:
podman tag localhost/ansible-execution-env localhost/netcommon-hub-ee
βοΈ Task - Let’s push this EE to Automation Hub
Automation Hub also acts as a registry to store your custom execution environments. To push this execution environment to Automation Hub, you need to first tag the EE with a new name and address of the Automation Hub.
Login to Automation Hub with Podman:
podman login control.lab --tls-verify=false
Username |
|
Password |
|
Tag your local image:
podman tag localhost/ansible-execution-env control.lab/netcommon-hub-ee
Push the image to Automation Hub:
podman push control.lab/netcommon-hub-ee --tls-verify=false
Let’s verify if the image is pushed to Automation Hub:
-
Go to the Automation Hub tab
-
Login using the below credentials:
Username |
|
Password |
|
-
Go to the Execution Environments section on the left-hand side, and you should see the recent execution environment pushed to Automation Hub.
π Hurray! You have now created an execution environment with a collection from Automation Hub by ignoring the certificate errors and pushed it to Automation Hub.
In the next challenge, we will try to fix the certificate errors properly.
β Next Challenge
Press the Next button below to go to the next challenge once youβve completed the tasks.