Molecule changing inventory_hostname with idempotence test

I’ve an Icinga Ansible role with a template that contains:

/* Our local instance name. By default this is the server's hostname as returned by `hostname --fqdn`.
 * This should be the common name from the API certificate.
 */
const NodeName = "{{ inventory_hostname }}"
/* Our local zone name. */
const ZoneName = "{{ inventory_hostname }}"

On commits I have a GitLab CI configuration which runs Molecule to test the role, this is currently generating a idempotence error:

CRITICAL Idempotence test failed because of the following tasks:
*  => icinga : Icinga constants configuration file in place

The template task that generates this is:

    - name: Icinga constants configuration file in place
      ansible.builtin.template:
        src: "constants.conf.j2"
        dest: "/etc/icinga2/constants.conf"
        backup: true
        owner: nagios
        group: nagios
        mode: "0640"

And the error is:

TASK [icinga : Icinga constants configuration file in place] *******************
--- before: /etc/icinga2/constants.conf
+++ after: /root/.ansible/tmp/ansible-local-16599q5y5noqw/tmp0p1mg328/constants.conf.j2
@@ -22,10 +22,10 @@
 /* Our local instance name. By default this is the server's hostname as returned by `hostname --fqdn`.
  * This should be the common name from the API certificate.
  */
-const NodeName = "runner-tusiyw1--project-879-concurrent-0"
+const NodeName = "localhost"
 /* Our local zone name. */
-const ZoneName = "runner-tusiyw1--project-879-concurrent-0"
+const ZoneName = "localhost"
 /* Secret key for remote node tickets */
 const TicketSalt = "eef5mujau3Tae3niesohK6uicheQu8oh"
changed: [localhost]

Does anyone know the cause and or solution for this? I guess I could add tags to ignore any task that uses inventory_hostname in all roles but I’d rather have a more elegant answer!

I’ve posted about this issue on the Molecule GitHub forum but there hasn’t been a response there either:

Should I raise this as a Molecule issue or is this expected behaviour that I need to make changes to the role to accommodate?

Something else, apart from Molecule is likely modifying that value and putting it on the default hostname --fqdn. Likely the Icinga process itself or another installation state of Icinga that resets this file.

1 Like

You are probably right but I can’t track it down, I do have a role to set /etc/hostname but this isn’t used by Molecule for the Icinga role, however I also haven’t had this issue with other roles, for now I’ve added a molecule-idempotence-notest tag to the task.