ansiblemolecule

How to "mock" groups in molecule unit test for Ansible?


I have this task definition for a Role I have written a molecule unit test for.

---
- name: Debug JDBC lines
  ansible.builtin.debug:
          msg: "jdbc_connection_string_np{{ my_idx }}=jdbc:mysql://{{ item }}:3306/np"
  with_items: "{{ groups['db'] }}"
  loop_control:
    index_var: my_idx
  when:
    - asp_srv is defined
    - anp_pkg_srv is defined
    - anp_pkg_srv in asp_srv

When I run molecule test I get this error:

TASK [bern.numberprovisioning : Debug JDBC lines] *********************
fatal: [centos7]: FAILED! => {"msg": "'dict object' has no attribute 'db'"}

I was hoping for output close to this:

jdbc_connection_string_np0=jdbc:mysql://mockdb-host1:3306/np
jdbc_connection_string_np1=jdbc:mysql://mockdb-host2:3306/np

Here is the provisioner section. I think I need to put something there, but whatever I do I cause errors.

provisioner:
  name: ansible
  inventory:
    host_vars:
      centos7:
        asp_srv: "all,figapiv2,nisioning,ocessor"

Solution

  • It turns out that I did not need to change the provisioner section of my molecule/default/molecule.yml file. I needed to set my platforms section to:

    platforms:
      - name: centos7
        image: ghcr.io/easybuilders/centos-7.9:latest
        tmpfs:
          - /run
          - /tmp
        volumes:
          - /sys/fs/cgroup:/sys/fs/cgroup:ro
        command: "/usr/sbin/init"
        privileged: true
        pre_build_image: true
      - name: mockdb1
        image: ghcr.io/easybuilders/centos-7.9:latest
        groups:
          - db
      - name: mockdb2
        image: ghcr.io/easybuilders/centos-7.9:latest
        groups:
          - db
    

    The above does what I need, but it is kind of annoying that I have to specify the image for mockdb1 & mockdb2.