HPE Developer Community Portal

Accessing iLO Redfish APIs and HPE OneView APIs on Ansible AWX

Nathan Lin

There has been a growing demand in infrastructure management automation and an adaption toward infrastructure-as-code (IAC) with Ansible and AWX. Backed by Red Hat, Ansible has become one of the most popular IAC toolings for its simple and easy to understand coding style, masterless and agentless design, and the ability to create custom playbooks and roles for providing extra support to process automation. AWX, on the other hand, is the open-sourced version of Ansible Tower, which along with a set of tools, provides a web-based graphical user interface hub for consuming Ansible playbooks.

Both the HPE Python iLO REST Library and the HPE OneView SDK for Ansible do not come bundled with AWX. The AWX project does provide instructions on managing custom Python dependencies on AWX. This blog post is to share the process that we took to set up a custom Python environment for the Python iLO REST Library and the HPE OneView SDK in order to access the iLO Redfish APIs and the HPE OneView APIs from an AWX job.

Ansible and AWX setup on localhost

Ansible and AWX setup instructions can be found here on GitHub. If running behind proxies, make sure the proxy parameters, such as http_proxy, https_proxy, and no_proxy are configured accordingly in the installation inventory file. Once installation completes, the Ansible command becomes available on the localhost, and AWX runs as a containerized application, as shown here:

bash-4.4# ansible --help
usage: ansible [-h] [--version] [-v] [-b] [--become-method BECOME_METHOD]
               [--become-user BECOME_USER] [-K] [-i INVENTORY] [--list-hosts]
               [-l SUBSET] [-P POLL_INTERVAL] [-B SECONDS] [-o] [-t TREE] [-k]
               [--private-key PRIVATE_KEY_FILE] [-u REMOTE_USER]
               [-c CONNECTION] [-T TIMEOUT]
               [--ssh-common-args SSH_COMMON_ARGS]
               [--sftp-extra-args SFTP_EXTRA_ARGS]
               [--scp-extra-args SCP_EXTRA_ARGS]
               [--ssh-extra-args SSH_EXTRA_ARGS] [-C] [--syntax-check] [-D]
               [-e EXTRA_VARS] [--vault-id VAULT_IDS]
               [--ask-vault-pass | --vault-password-file VAULT_PASSWORD_FILES]
               [-f FORKS] [-M MODULE_PATH] [--playbook-dir BASEDIR]
               [-a MODULE_ARGS] [-m MODULE_NAME]
               pattern
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                COMMAND                  CREATED      STATUS      PORTS                  NAMES
79248db66699   ansible/awx:17.0.1   "/usr/bin/tini -- /u…"   5 days ago   Up 5 days   8052/tcp               awx_task
11e9c78d53cf   ansible/awx:17.0.1   "/usr/bin/tini -- /b…"   5 days ago   Up 5 days   0.0.0.0:80->8052/tcp   awx_web
46c529f34016   postgres:12          "docker-entrypoint.s…"   5 days ago   Up 5 days   5432/tcp               awx_postgres
75c8e09ad2de   redis                "docker-entrypoint.s…"   5 days ago   Up 5 days   6379/tcp               awx_redis

Installing the HPE libraries on AWX

The installation of the HPE Python iLO REST library and the HPE OneView Ansible library can be achieved by the following three steps:

  1. Log in to the awx_task container. Create a custom Python virtual environment and install the HPE Python iLO REST library and the HPE OneView Ansible library using the Python package manager.

  2. Log in to the awx_web containers. Create a new virtual environment with the same name as the one newly created in the awx_task container, and then install these two HPE Python libraries again using the Python package manager.

  3. Lastly, add the new Python environment to the custom_virtualenvs in AWX through its REST APIs.

Keep in mind to create the virtual environments first in awx_web and awx_task before adding it to the AWX custom virtual environment. The following sections talk more about each of the steps.

Create new custom virtual environments on awx_task and awx_web

First, access the container BASH shell with the command Docker exec -it . For example, accessing the awx task shell :

[root@localhost ~]# docker exec -it awx_task /bin/bash
bash-4.4#

Within the container, create a new Python virtual environment. For this example, the virtual environment is created at /opt/hpeAutomation/venv:

bash-4.4# mkdir -p /opt/hpeAutomation/
bash-4.4# chmod 0755 /opt/hpeAutomation/
bash-4.4# python3 -m venv /opt/hpeAutomation/venv

Install the HPE libraries on awx_task and awx_web

On each of the AWX containers, proceed as follows:

  1. YUM install the pre-requisites gcc:
bash-4.4# yum install gcc -y
  1. Install the psutil Python module and the HPE libraries:
bash-4.4# /opt/hpeAutomation/venv/bin/pip3 install psutil
bash-4.4# /opt/hpeAutomation/venv/bin/pip3 install ansible hpOneView hpICsp python-ilorest-library
bash-4.4# git clone https://github.com/HewlettPackard/oneview-ansible.git
bash-4.4# cd oneview-ansible
bash-4.4# cp library/*.py  /opt/hpeAutomation/venv/lib/python3.6/site-packages/ansible/modules/remote_management/oneview/
bash-4.4# cp library/module_utils/oneview.py  /opt/hpeAutomation/venv/lib/python3.6/site-packages/ansible/module_utils 

Configure the custom_virtualenvs in AWX

Once finished installing the Python modules to awx_task and awx_web , the last step is to add the newly created virtual environment to custom_virtualenvs in AWX. This can be done with a HTTP PATCH to the AWX:

[root@localhost ~]# curl -X PATCH http://AWX_admin_username:AWX_adminpassword@AWX_ip_address/api/v2/settings/system/ \
-d '{"CUSTOM_VENV_PATHS": ["/var/lib/awx/venv/ansible", "/opt/hpeAutomation/"]}' \
-H 'Content-Type:application/json'

You can verify the custom_virtualenvs in AWX with a HTTP GET request to /api/v2/config/ , as shown below:

[root@localhost ~]# curl -u AWX_admin_username:AWX_admin_password http://AWX_IP_address/api/v2/config/
{
  "time_zone": "UTC",
  "license_info": {
    "license_type": "open",
    "valid_key": true,
    "subscription_name": "OPEN",
    "product_name": "AWX"
  },
  ...
  ..
  .
  "custom_virtualenvs": [
    "/var/lib/awx/venv/ansible/",
    "/opt/hpeAutomation/venv/"
  ]
}

Once completed, the custom virtual environment becomes available as an Ansible Environment in the AWX Projects and the Python libraries become accessible by the Job Templates in the project. project template

There you have it. The AWX is now ready to run jobs for HPE OneView and iLO.