Inventory variables not being read

Hi All,

I seem to be having an issue defining a variables in my /etc/ansible/hosts. When I run a playbook against a host in my inventory, it seems the variable are not read based on the error messages. For example, here it seems to not see the network os variable:

ansible-playbook -i csr1, first_playbook_ext_ios2.yml

PLAY [Network Getting Started First Playbook - Change Hostname] ****************************************************************************************************************

TASK [Get old config for ios devices] ******************************************************************************************************************************************
fatal: [csr1]: FAILED! => {“msg”: “Unable to automatically determine host network os. Please manually configure ansible_network_os value for this host”}

PLAY RECAP *********************************************************************************************************************************************************************
csr1 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

Even though the variable is defined and shows up in ansible-inventory:

ansible-inventory --host csr1
{
“ansible_become”: true,
“ansible_become_method”: “enable”,
“ansible_become_password”: “cisco”,
“ansible_host”: “192.168.1.73”,
“ansible_network_os”: “ios”,
“ansible_user”: “admin”
}

Here is the hosts file:

cat /etc/ansible/hosts

Additional information after more testing. All seems to work if I use --limit instead of -i.

I.e.:

This works:
ansible-playbook -k first_playbook_ext_ios2.yml --limit csr1

This does not work:
ansible-playbook -i csr1, first_playbook_ext_ios2.yml -k

Is this expected behavior? Any pointers or doc links on what I’m missing in terms of differences between the two options above?

Thank you!

Hey EE1,

As you rightly pointed out --limit works and -i don’t, as you reported you are getting desired output for ansible-inventory --host csr1 which indicates the inventory is already being picked up correctly.
To limit the hosts you target on a particular run we use –limit flag, -i on the other hand points to an inventory as a whole, not particular hosts.
you might get more clarity here.

Regards,
Sagar Paul
E - sagpaul@redhat.com
G - KB-perByte

Thank you Sagar. That gives me some clarity and puts me on the right path.

The link you shared is informative and certainly clarified some tips and flexibility for patterns with --limit. It does mention both methods of selecting a single host towards the bottom, but doesn’t provide as clear and concise statement of the difference as you did :slight_smile: Namely that -i “points to an inventory as a whole” and will not reference existing inventory file or variables. To that end, I tried to be helpful and submitted a documentation PR that I think will be helpful to other new users: https://github.com/ansible/ansible/pull/77300

Hey EE1,

Happy to help!
well, you may find this doc helpful regarding the specific usability of -i.
the synopsis here covers most of the attribute options.

Regards,
Sagar Paul
E - sag…@redhat.com
G - KB-perByte