# Error when using group\_vars

**URL:** <https://forum.ansible.com/t/error-when-using-group-vars/3857>\
**Category:** Get Help\
**Created:** [February 13, 2024, 8:58pm UTC](https://forum.ansible.com/t/error-when-using-group-vars/3857 "2024-02-13T20:58:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cslayton65](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cslayton65/32/2309_2.png) [@cslayton65](https://forum.ansible.com/u/cslayton65)\
**Post date:** [February 13, 2024, 8:58pm UTC](https://forum.ansible.com/t/error-when-using-group-vars/3857/1 "2024-02-13T20:58:43Z")

</div>

I am just getting started with Ansible and intend to mainly use Ansible for networking. When I include my vars directly in the hosts.yml file, my playbook works. When I move the vars to group\_vars, the playbook fails with the error: FAILED! =\> {“msg”: "The conditional check ‘ansible\_network\_os == ‘cisco.nxos.nxos’’ failed. The error was: error while evaluating conditional (ansible\_network\_os == ‘cisco.nxos.nxos’): ‘ansible\_network\_os’ is undefined.

```auto
HOSTS.YML with VARS
nxos:
  hosts:
    switch1:
      ansible_host: switch1.example.com
    switch2:
      ansible_host: switch2.example.com
  vars:
    ansible_become: yes
    ansible_become_method: enable
    ansible_network_os: cisco.nxos.nxos
    ansible_user: <username>
    ansible_password: !vault |
          $ANSIBLE_VAULT;1.1;AES256
		  <encrypted password>
		  
GROUP_VARS/nxos
vars:
  ansible_become: yes
  ansible_become_method: enable
  ansible_network_os: cisco.nxos.nxos
  ansible_user: <username>
  ansible_password: !vault |
        $ANSIBLE_VAULT;1.1;AES256
		<encrypted password>
		
PLAYBOOK
---
- name: "Demonstrate connecting to NEXUS switches"
  hosts: nxos
  gather_facts: no
  connection: ansible.netcommon.network_cli

  tasks:
    ###
    # Collect data
    #
    - name: Gather facts (nxos)
      cisco.nxos.nxos_facts:
      when: ansible_network_os == 'cisco.nxos.nxos'

 ###
    # Demonstrate variables
    #
    - name: Display some facts
      debug:
        msg: "The hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}"

```

- Set at group\_vars one tag (the experts follow the tags, so the right people will find you if you tag)

---

<div class="post-metadata">

**Author:** ![cslayton65](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cslayton65/32/2309_2.png) [@cslayton65](https://forum.ansible.com/u/cslayton65)\
**Post date:** [February 13, 2024, 9:03pm UTC](https://forum.ansible.com/t/error-when-using-group-vars/3857/2 "2024-02-13T21:03:22Z")

</div>

I believe I have solved this myself. I had to remove “vars:” from the group\_vars/nxos file.

---

<div class="post-metadata">

**Author:** ![Dustin](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dustin/32/2573_2.png) [@Dustin](https://forum.ansible.com/u/Dustin)\
**Post date:** [February 13, 2024, 9:13pm UTC](https://forum.ansible.com/t/error-when-using-group-vars/3857/3 "2024-02-13T21:13:29Z")

</div>

host\_var and group\_var files are read directly as variables. You don’t need to preface the list of variables with the “vars:” line.

You are correct in your assumption of the solution.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 14, 2024, 9:13pm UTC](https://forum.ansible.com/t/error-when-using-group-vars/3857/4 "2024-03-14T21:13:50Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
