# Molecule changing inventory\_hostname with idempotence test

**URL:** <https://forum.ansible.com/t/molecule-changing-inventory-hostname-with-idempotence-test/41514>\
**Category:** Get Help\
**Tags:** gitlab, molecule\
**Created:** [April 4, 2025, 12:50pm UTC](https://forum.ansible.com/t/molecule-changing-inventory-hostname-with-idempotence-test/41514 "2025-04-04T12:50:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [April 4, 2025, 12:50pm UTC](https://forum.ansible.com/t/molecule-changing-inventory-hostname-with-idempotence-test/41514/1 "2025-04-04T12:50:55Z")

</div>

I’ve [an Icinga Ansible role](https://git.coop/webarch/icinga) with a [template](https://git.coop/webarch/icinga/-/blob/4d03fd3231c0c8c3c9fab5a797c098782a1ba678/templates/constants.conf.j2) that contains:

```auto
/* 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](https://git.coop/webarch/icinga/-/blob/4d03fd3231c0c8c3c9fab5a797c098782a1ba678/.gitlab-ci.yml) which runs Molecule to test the role, this is currently generating [a idempotence error](https://git.coop/webarch/icinga/-/jobs/65357):

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

```

The [template task that generates this](https://git.coop/webarch/icinga/-/blob/4d03fd3231c0c8c3c9fab5a797c098782a1ba678/tasks/config.yml#L49) is:

```auto
    - 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](https://git.coop/webarch/icinga/-/jobs/65357#L203) is:

```auto
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!

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [April 7, 2025, 11:50am UTC](https://forum.ansible.com/t/molecule-changing-inventory-hostname-with-idempotence-test/41514/2 "2025-04-07T11:50:03Z")

</div>

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

> **[Molecule changing inventory\_hostname with idempotence test · ansible molecule...](https://github.com/ansible/molecule/discussions/4423)**
>
> I've found that inventory\_hostname is changed with the with idempotence test when running Molecule using GitLab CI and have posted a question about this on the Ansible forum, I'm posting this here ...

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

---

<div class="post-metadata">

**Author:** ![rvdg](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rvdg/32/8143_2.png) [@rvdg](https://forum.ansible.com/u/rvdg)\
**Post date:** [April 15, 2025, 3:42pm UTC](https://forum.ansible.com/t/molecule-changing-inventory-hostname-with-idempotence-test/41514/3 "2025-04-15T15:42:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [April 15, 2025, 4:16pm UTC](https://forum.ansible.com/t/molecule-changing-inventory-hostname-with-idempotence-test/41514/4 "2025-04-15T16:16:20Z")

</div>

You are probably right but I can’t track it down, I do have a [role to set `/etc/hostname`](https://git.coop/webarch/etchosts) 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.
