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.
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)