<p>Since goss can use variables from a yaml or json file, I just modified the <code>verify.yml</code> example playbook to export this variable and use it in the goss command.</p>

<pre><code>- name: Verify
  hosts: all
  become: true
  vars:
    [...]
    goss_vars: "{{ goss_test_directory }}/vars"
  tasks:
    [...]
    - name: Register hostvars
      copy:
        content: "{'hostvars': {{ hostvars }}}"
        dest: "{{ goss_vars }}"

    - name: Execute Goss tests
      command: "{{ goss_bin }} --vars {{ goss_vars }} -g {{ item }} validate --format {{ goss_format }}"
      register: test_results
      with_items: "{{ test_files.stdout_lines }}"
      failed_when: false

    [...]
</code></pre>

<p>Then the variable is available in goss test files as <code>{{ .Vars.hostvars }}</code>.</p>

CC BY-SA 4.0
2019-10-03T14:23:18.360Z
2019-10-03T14:23:18.360Z
Christophe Drevet
<p>I have a molecule scenario that creates 3 docker instances, each having a different configuration from the same role. The three communicates with each other as I put the instances IP addresses in their respective configuration files.</p>

<p>I need to test that the correct IP address is set the config files of each instance.</p>

<p>How can I access ansible group and host variables from goss?</p>

CC BY-SA 4.0
2019-10-07T11:58:14.010Z
What's wrong with my question?
Christophe Drevet
<molecule>
How to access hostvars from goss test with molecule
Christophe Drevet
# How to access hostvars from goss test with molecule

(Asked by: username_0 on 2019-10-03)

I have a molecule scenario that creates 3 docker instances, each having a different configuration from the same role. The three communicates with each other as I put the instances IP addresses in their respective configuration files.

I need to test that the correct IP address is set the config files of each instance.

How can I access ansible group and host variables from goss?

**Tags:** molecule

> **Comments:**
> **username_0**: What's wrong with my question?  

---

## 1 answers

### Answer by username_0 on 2019-10-03. Score: 0 | Accepted answer

Since goss can use variables from a yaml or json file, I just modified the verify.yml example playbook to export this variable and use it in the goss command.
- name: Verify
  hosts: all
  become: true
  vars:
    [...]
    goss_vars: "{{ goss_test_directory }}/vars"
  tasks:
    [...]
    - name: Register hostvars
      copy:
        content: "{'hostvars': {{ hostvars }}}"
        dest: "{{ goss_vars }}"

    - name: Execute Goss tests
      command: "{{ goss_bin }} --vars {{ goss_vars }} -g {{ item }} validate --format {{ goss_format }}"
      register: test_results
      with_items: "{{ test_files.stdout_lines }}"
      failed_when: false

    [...]

Then the variable is available in goss test files as {{ .Vars.hostvars }}.


---

