How to connect to test instances with dynamic ips?

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?

As far as I understand, when using Ansible-Native configuration, you are on your own. Molecule will not do any magic for you. This means:

  1. You will have to generate your inventory and specify Ansible to use it in molecule configuration:
  executor:
    backend: ansible-playbook
    args:
      ansible_playbook:
        - --inventory=/path/to/my_inv.yml
  1. For create phase you will implement a playbook that does all the provisioning of instances and/or containers and generate my_inv.yml.

  2. Other phases will read the generated inventory to run the test against.

In theory, your my_inv.yml can be a static file by using appropriate inventory plugin to dynamically inspect parameters of your newly provisioned instances. This can work for AWS for example:

  1. Your create.yml playbook provisions the instances using ec2_instance module. No additional work needed.
  2. Once your converge.yml playbook runs, Ansible will use inventory that uses aws_ec2 inventory plugin to dynamically retrieve instance information. There is no need to persist, transfer or share any inventory related info between these phases.