ansible/molecule

When including more than 1 entry in platforms molecule_inventory.yml doesn't contain all entries

brandor5 opened this issue · 2 comments

Prerequisites

  • This was not already reported in the past (duplicate check)
  • It does reproduce it with code from main branch (latest unreleased version)
  • I include a minimal example for reproducing the bug
  • The bug is not trivial, as for those a direct pull-request is preferred
  • Running pip check does not report any conflicts
  • I was able to reproduce the issue on a different machine
  • The issue is not specific to any driver other than 'default' one

Environment

$ molecule --version
molecule 6.0.2 using python 3.9
ansible:2.15.4
default:6.0.2 from molecule

$ rpm -q python3
python3-3.9.16-1.el9_2.1.x86_64

$ rpm -q ansible-core
ansible-core-2.15.4-1.el9ap.noarch

$ rpm -q molecule
molecule-6.0.2-1.el9ap.noarch

$ cat /etc/redhat-release
Red Hat Enterprise Linux release 9.2 (Plow)

What happened

I followed the Red Hat blog [0] example and then followed the docs [1] to enable podman containers. Everything appeared to work, however when I ran molecule test not all platform entries are destroyed.

$ podman ps -a
CONTAINER ID  IMAGE       COMMAND     CREATED     STATUS      PORTS       NAMES
[brandor5@dev extensions]$ cat molecule/default/molecule.yml 
---
dependency:
  name: galaxy
  options:
    requirements-file: requirements.yml

platforms:
  - name: ubi8
    image: registry.access.redhat.com/ubi8/ubi-init:latest
  - name: ubi9
    image: registry.access.redhat.com/ubi9/ubi-init:latest

provisioner:
  name: ansible
  config_options:
    defaults:
      collections_path: ${ANSIBLE_COLLECTIONS_PATH}

driver:
  options:
    managed: False
    login_cmd_template: 'podman exec -ti {instance} bash'
    ansible_connection_options:
      ansible_connection: podman
[brandor5@dev extensions]$ molecule test
INFO     default scenario test matrix: dependency, cleanup, destroy, syntax, create, prepare, converge, idempotence, side_effect, verify, cleanup, destroy
INFO     Performing prerun with role_name_check=0...
INFO     Running default > dependency
Starting galaxy collection install process
Nothing to do. All requested collections are already installed. If you want to reinstall them, consider using `--force`.
INFO     Dependency completed successfully.
WARNING  Skipping, missing the requirements file.
INFO     Running default > cleanup
WARNING  Skipping, cleanup playbook not configured.
INFO     Running default > destroy
[WARNING]: Could not match supplied host pattern, ignoring: molecule

PLAY [Destroy molecule containers] *********************************************
skipping: no hosts matched

PLAY [Remove dynamic molecule inventory] ***************************************

TASK [Remove dynamic inventory file] *******************************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

INFO     Running default > syntax
[WARNING]: Could not match supplied host pattern, ignoring: molecule

playbook: /home/brandor5/work/ansible-collections/ansible_collections/foo/bar/extensions/molecule/default/converge.yml
INFO     Running default > create

PLAY [Create] ******************************************************************

TASK [Create a container] ******************************************************
changed: [localhost] => (item={'image': 'registry.access.redhat.com/ubi8/ubi-init:latest', 'name': 'ubi8'})
changed: [localhost] => (item={'image': 'registry.access.redhat.com/ubi9/ubi-init:latest', 'name': 'ubi9'})

TASK [Fail if container is not running] ****************************************
skipping: [localhost] => (item=ubi8) 
skipping: [localhost] => (item=ubi9) 
skipping: [localhost]

TASK [Add container to molecule_inventory] *************************************
ok: [localhost] => (item=ubi8)
ok: [localhost] => (item=ubi9)

TASK [Dump molecule_inventory] *************************************************
changed: [localhost]

TASK [Force inventory refresh] *************************************************

TASK [Fail if molecule group is missing] ***************************************
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

PLAY [Validate that inventory was refreshed] ***********************************

TASK [Check uname] *************************************************************
ok: [ubi9]

TASK [Display uname info] ******************************************************
ok: [ubi9] => {
    "msg": "Linux c2c968db5538 5.14.0-284.30.1.el9_2.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Aug 25 09:13:12 EDT 2023 x86_64 x86_64 x86_64 GNU/Linux\n"
}

PLAY RECAP *********************************************************************
localhost                  : ok=4    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
ubi9                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

INFO     Running default > prepare
WARNING  Skipping, prepare playbook not configured.
INFO     Running default > converge

PLAY [Fail if molecule group is missing] ***************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Print some info] *********************************************************
ok: [localhost] => {
    "msg": {
        "all": [
            "ubi8",
            "ubi9"
        ],
        "molecule": [
            "ubi9"
        ],
        "ungrouped": [
            "ubi8"
        ]
    }
}

TASK [Assert group existence] **************************************************
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

PLAY [Converge] ****************************************************************

TASK [Check uname] *************************************************************
ok: [ubi9]

TASK [Print some info] *********************************************************
ok: [ubi9] => {
    "changed": false,
    "msg": "All assertions passed"
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubi9                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

INFO     Running default > idempotence

PLAY [Fail if molecule group is missing] ***************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Print some info] *********************************************************
ok: [localhost] => {
    "msg": {
        "all": [
            "ubi8",
            "ubi9"
        ],
        "molecule": [
            "ubi9"
        ],
        "ungrouped": [
            "ubi8"
        ]
    }
}

TASK [Assert group existence] **************************************************
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

PLAY [Converge] ****************************************************************

TASK [Check uname] *************************************************************
ok: [ubi9]

TASK [Print some info] *********************************************************
ok: [ubi9] => {
    "changed": false,
    "msg": "All assertions passed"
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubi9                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

INFO     Idempotence completed successfully.
INFO     Running default > side_effect
WARNING  Skipping, side effect playbook not configured.
INFO     Running default > verify
INFO     Running Ansible Verifier
WARNING  Skipping, verify action has no playbook.
INFO     Verifier completed successfully.
INFO     Running default > cleanup
WARNING  Skipping, cleanup playbook not configured.
INFO     Running default > destroy

PLAY [Destroy molecule containers] *********************************************

TASK [Stop and remove container] ***********************************************
changed: [ubi9 -> localhost]

TASK [Remove potentially stopped container] ************************************
ok: [ubi9 -> localhost]

PLAY [Remove dynamic molecule inventory] ***************************************

TASK [Remove dynamic inventory file] *******************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubi9                       : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

INFO     Pruning extra files from scenario ephemeral directory
[brandor5@dev extensions]$ podman ps -a
CONTAINER ID  IMAGE                                            COMMAND     CREATED         STATUS         PORTS       NAMES
4a62ce804639  registry.access.redhat.com/ubi8/ubi-init:latest  sleep 1d    34 seconds ago  Up 34 seconds              ubi8

[0] https://developers.redhat.com/articles/2023/09/13/introducing-ansible-molecule-ansible-automation-platform
[1] https://ansible.readthedocs.io/projects/molecule/podman/

Reproducing example

---
dependency:
  name: galaxy
  options:
    requirements-file: requirements.yml

platforms:
  - name: ubi8
    image: registry.access.redhat.com/ubi8/ubi-init:latest
  - name: ubi9
    image: registry.access.redhat.com/ubi9/ubi-init:latest

provisioner:
  name: ansible
  config_options:
    defaults:
      collections_path: ${ANSIBLE_COLLECTIONS_PATH}

driver:
  options:
    managed: False
    login_cmd_template: 'podman exec -ti {instance} bash'
    ansible_connection_options:
      ansible_connection: podman

Sorry, should have included this:

$ molecule create
INFO     default scenario test matrix: dependency, create, prepare
INFO     Performing prerun with role_name_check=0...
INFO     Running default > dependency
Starting galaxy collection install process
Nothing to do. All requested collections are already installed. If you want to reinstall them, consider using `--force`.
INFO     Dependency completed successfully.
WARNING  Skipping, missing the requirements file.
INFO     Running default > create

PLAY [Create] ******************************************************************

TASK [Create a container] ******************************************************
changed: [localhost] => (item={'image': 'registry.access.redhat.com/ubi8/ubi-init:latest', 'name': 'ubi8'})
changed: [localhost] => (item={'image': 'registry.access.redhat.com/ubi9/ubi-init:latest', 'name': 'ubi9'})

TASK [Fail if container is not running] ****************************************
skipping: [localhost] => (item=ubi8) 
skipping: [localhost] => (item=ubi9) 
skipping: [localhost]

TASK [Add container to molecule_inventory] *************************************
ok: [localhost] => (item=ubi8)
ok: [localhost] => (item=ubi9)

TASK [Dump molecule_inventory] *************************************************
changed: [localhost]

TASK [Force inventory refresh] *************************************************

TASK [Fail if molecule group is missing] ***************************************
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

PLAY [Validate that inventory was refreshed] ***********************************

TASK [Check uname] *************************************************************
ok: [ubi9]

TASK [Display uname info] ******************************************************
ok: [ubi9] => {
    "msg": "Linux 581abc58dcf3 5.14.0-284.30.1.el9_2.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Aug 25 09:13:12 EDT 2023 x86_64 x86_64 x86_64 GNU/Linux\n"
}

PLAY RECAP *********************************************************************
localhost                  : ok=4    changed=2    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
ubi9                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

INFO     Running default > prepare
WARNING  Skipping, prepare playbook not configured.

[brandor5@dev extensions]$ cat ~/.cache/molecule/extensions/default/inventory/molecule_inventory.yml 
all:
  children:
    molecule:
      hosts:
        ubi9: {ansible_connection: containers.podman.podman}

I just ran into the same thing - {{ molecule_inventory | combine(inventory_partial_yaml | from_yaml, recursive=true) }} will fix that