ansibleansible-inventorymolecule

How to pass an additional inventory file (dynamic inventory script) in molecule?


In my playbook I use var group[inventory_group]. And I need to use my dynamic inventory script. In the documentation I found:

Like above, it is possible to pass an additional inventory file (or even dynamic inventory script), using the hosts key. Ansible will automatically merge this inventory with the one generated by molecule. This can be useful if you want to define extra hosts that are not managed by Molecule.

But I didn't find any examples.

So, how do I "pass an additional inventory file (or even dynamic inventory script), using the hosts key" ?


Solution

  • Perhaps you could execute the dynamic inventory script in create or prepare steps in the molecule scenario, and then use the add_host module to add the hosts to the in memory inventory?

    So something like:

    - command:
        cmd: /path/to/your_dynamic_inventory_scri.pt
      delegate_to: localhost
      register: dynamic_inventory
    
    
    - add_host:
        name: "{{ item }}"
      loop: "{{ dynamic_inventory.stdout_lines }}"