How to connect to test instances with dynamic ips?

chronicc
Tue, 07 Apr 2026 06:44:33 +0000
Get Help

I am developing an ansible collection.

I am reading about and working with the Ansible-Native configuration and want to use the shared state feature (Ansible-Native Configuration - Ansible Molecule).

However it seems like molecule can not find instances with dynamic ip addresses. I populated and persistet the instance_config.yml:

- address: 192.168.123.64
  become_method: sudo
  identity_file: /home/chronicc/.ansible/tmp/molecule.kBwO.default/id_ed25519
  instance: instance01
  port: 22
  user: molecule

I’ve tinkered around with the config.yml and are now at a point where the shared state setup does block itself.

I am currently relying on the inventory file that is created by molecule itself (MOLECULE_INVENTORY_FILE ).

❯ cat /home/chronicc/.ansible/tmp/molecule.kBwO.default/inventory/ansible_inventory.yml
# Molecule managed

---
all:
  hosts:
    instance01: &id001
      ansible_become_method: sudo
      ansible_connection: smart
      ansible_host: 192.168.123.64
      ansible_port: 22
      ansible_private_key_file: /home/chronicc/.ansible/tmp/molecule.kBwO.default/id_ed25519
      ansible_ssh_common_args: -o UserKnownHostsFile=/dev/null -o ControlMaster=auto
        -o ControlPersist=60s -o ForwardX11=no -o LogLevel=ERROR -o IdentitiesOnly=yes
        -o StrictHostKeyChecking=no
      ansible_user: molecule
  vars: &id002
    molecule_ephemeral_directory: '{{ lookup(''env'', ''MOLECULE_EPHEMERAL_DIRECTORY'')
      }}'
    molecule_file: '{{ lookup(''env'', ''MOLECULE_FILE'') }}'
    molecule_instance_config: '{{ lookup(''env'', ''MOLECULE_INSTANCE_CONFIG'') }}'
    molecule_no_log: '{{ lookup(''env'', ''MOLECULE_NO_LOG'') or not molecule_yml.provisioner.log|default(False)
      | bool }}'
    molecule_scenario_directory: '{{ lookup(''env'', ''MOLECULE_SCENARIO_DIRECTORY'')
      }}'
    molecule_yml: '{{ lookup(''file'', molecule_file) | from_yaml }}'
kubernetes_control_nodes:
  hosts:
    instance01: *id001
  vars: *id002
ungrouped:
  vars: {}

However when I am running my scenario kubernetes, it want’s to use the ansible_inventory.yml from it’s own ephemeral directory which does not contain the populated inventory file listed above.

Before this, I used the inventory directory extensions/molecule/inventory. This is accessible from all scenarios so the shared state feature does work. However this inventory does not get updated with the dynamic ip addresses for my instances. I am using libvirt currently but this can also change to vSphere or AWS.

I had hoped that molecule somehow combines the static inventory with the test instances hostnames and variables with the dynamic inventory from the ephemeral directory. Or somehow reuses the ip addresses from the instance_config.yml to update the static inventory in memory before running the playbooks.

So how would I need to setup Ansible-Native configuration with shared state and dynamic ip addresses?

10 posts - 2 participants