Skip to content

Instantly share code, notes, and snippets.

@chrismeyersfsu
Last active December 29, 2021 04:03
Show Gist options
  • Select an option

  • Save chrismeyersfsu/eda91ab3af382d1c86a7039ed0a6c75a to your computer and use it in GitHub Desktop.

Select an option

Save chrismeyersfsu/eda91ab3af382d1c86a7039ed0a6c75a to your computer and use it in GitHub Desktop.

Revisions

  1. chrismeyersfsu revised this gist Jul 30, 2021. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -76,3 +76,8 @@ Using module file /usr/share/ansible/collections/ansible_collections/f5networks/
    ...
    ```

    ### Notes

    `execution_environment_dev.yml` Uses the default community execution environment `quay.io/ansible/ansible-runner:latest`.

    `execution_environment_prod.yml` Will use the official supported images i.e. `registry.redhat.io/ansible-automation-platform-20-early-access/ee-supported-rhel8:2.0.0` node that building execution environments using this spec will require being ran from a registered RHEL system so that repositories are available to resolve system dependencies.
  2. chrismeyersfsu revised this gist Jul 30, 2021. 1 changed file with 78 additions and 0 deletions.
    78 changes: 78 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    ### Develop Your Collection Against Your Execution Environment

    Ansible collections often require python dependencies and other Ansible collections to run. Resolving these dependencies across different environments is tedious work. To solve this problem Ansible provides execution environments.

    A collection developer will want to provide users with an execution environment that can be used to execute their collection. The developer should develop his/her collection against the execution environment that the end-user will use to run the collection. This document provides a development flow to modify collection source code **outside** of the execution environment container while being able to run the modified collection source code **inside** the execution environment container.



    I will show you how to use an execution environment for developing against the `f5networks.f5_modules` collection.



    ````
    git clone https://gist.github.com/eda91ab3af382d1c86a7039ed0a6c75a.git my_dev_env
    cd my_dev_env
    git clone https://github.com/F5Networks/f5-ansible
    ````



    ```
    # tree -L 1
    .
    ├── ansible.cfg
    ├── execution_environment_dev.yml
    ├── execution_environment_prod.yml
    ├── f5-ansible
    ├── requirements.txt
    └── requirements.yml
    ```

    The execution environment needs to have the dependencies for the collection we are developing against.

    `requirements.txt` is copied from `f5-ansible/ansible_collections/f5networks/f5_modules/requirements.txt`

    `requirements.yml` is copied from the `dependencies:` section of `f5-ansible/ansible_collections/f5networks/f5_modules/galaxy.yml`





    ```
    pip install ansible-builder
    ansible-builder -f execution_environment_dev.yml -t my_dev_env
    ```



    ```
    podman run -it -v ${PWD}:/my_dev_env -v ${PWD}/f5-ansible/ansible_collections/f5networks:/usr/share/ansible/collections/ansible_collections/f5networks f5 /bin/bash
    ```

    The above starts your development execution environment container and drops you into a `/bin/bash` session. From here, you will be able to run `ansible-playbook main.yml` that contains calls to the module you are developing, `f5networks.f5_modules` in our case. Below is an example of the `f5networks.f5_modules.bigip_pool` module being called.

    ```
    cd /my_dev_env
    ansible-playbook main.yml -vvv
    ```

    ```
    # main.yml
    ---
    - name: Using Collections
    hosts: localhost
    tasks:
    - f5networks.f5_modules.bigip_pool:
    name: my-pool
    ```

    The above playbook will fail. However, the Ansible output tells the file path of the module we called and this path is the collection that we mapped into the container. Therefore, we can now modify our collection source code outside of the container and test that code by running it inside the execution environment. An execution environment that is very similar to the one that the users will run.
    ```
    ...
    Using module file /usr/share/ansible/collections/ansible_collections/f5networks/f5_modules/plugins/modules/bigip_pool.py
    ...
    ```

  3. chrismeyersfsu revised this gist Jul 30, 2021. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions main.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    ---
    - name: Using Collections
    hosts: localhost

    tasks:
    - f5networks.f5_modules.bigip_pool:
    name: my-pool
  4. chrismeyersfsu revised this gist Jul 30, 2021. 2 changed files with 7 additions and 4 deletions.
    7 changes: 7 additions & 0 deletions ansible.cfg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    [galaxy]
    server_list = automation_hub

    [galaxy_server.automation_hub]
    url=https://cloud.redhat.com/api/automation-hub/
    auth_url=https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token
    token=<put your hub token here>
    4 changes: 0 additions & 4 deletions execution_environment_dev.yml
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,5 @@
    ---
    version: 1
    ansible_config: ansible.cfg
    #build_arg_defaults:
    # EE_BASE_IMAGE: 'registry.redhat.io/ansible-automation-platform-20-early-access/ee-supported-rhel8:2.0.0'
    #EE_BUILDER_IMAGE: 'registry.redhat.io/ansible-automation-platform-20-early-access/ansible-builder-rhel8:2.0.0-10'
    dependencies:
    galaxy: requirements.yml
    python: requirements.txt
  5. chrismeyersfsu created this gist Jul 30, 2021.
    9 changes: 9 additions & 0 deletions execution_environment_dev.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    ---
    version: 1
    ansible_config: ansible.cfg
    #build_arg_defaults:
    # EE_BASE_IMAGE: 'registry.redhat.io/ansible-automation-platform-20-early-access/ee-supported-rhel8:2.0.0'
    #EE_BUILDER_IMAGE: 'registry.redhat.io/ansible-automation-platform-20-early-access/ansible-builder-rhel8:2.0.0-10'
    dependencies:
    galaxy: requirements.yml
    python: requirements.txt
    9 changes: 9 additions & 0 deletions execution_environment_prod.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    ---
    version: 1
    ansible_config: ansible.cfg
    build_arg_defaults:
    EE_BASE_IMAGE: 'registry.redhat.io/ansible-automation-platform-20-early-access/ee-supported-rhel8:2.0.0'
    EE_BUILDER_IMAGE: 'registry.redhat.io/ansible-automation-platform-20-early-access/ansible-builder-rhel8:2.0.0-10'
    dependencies:
    galaxy: requirements.yml
    python: requirements.txt
    5 changes: 5 additions & 0 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    ipaddress; python_version < '3.5'
    cryptography
    objectpath
    ordereddict
    simplejson
    4 changes: 4 additions & 0 deletions requirements.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    ---
    collections:
    - name: ansible.netcommon
    version: ">=2.0.0"