# Ansible Ping from command line not working

**URL:** <https://forum.ansible.com/t/ansible-ping-from-command-line-not-working/6595>\
**Category:** Get Help\
**Tags:** variables, ping, inventory\
**Created:** [June 17, 2024, 12:17pm UTC](https://forum.ansible.com/t/ansible-ping-from-command-line-not-working/6595 "2024-06-17T12:17:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evan\_Goulden](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/evan_goulden/32/3864_2.png) [@Evan\_Goulden](https://forum.ansible.com/u/Evan_Goulden)\
**Post date:** [June 17, 2024, 12:17pm UTC](https://forum.ansible.com/t/ansible-ping-from-command-line-not-working/6595/1 "2024-06-17T12:17:20Z")

</div>

Hi There, I am new to Ansible so just learning at the moment and have a question regarding variables and how to access them under specific scenarios.

A bit of background, I’ve setup a couple of inventory files and splitting them out for various different functions and I have a couple of basic playbooks, mostly for discovering information about my network. This all works great.

What I’m struggling to understand, if I want to use the ping module from the command line, how can i get ansible to use my stored variable for username and password. When i do this using a playbook I have an entry in the YAML file pointing at the var\_files, like shown below:

```auto
- name: Gather Facts from Cisco IOS Devices
  hosts: corp_edge_cisco
  gather_facts: true
  vars_files: ../groupvars/default.yaml
  tasks:
  - name: Gather facts

```

But when I run the following command:  
ansible corp\_edge\_cisco -i ansible/inventory/corp\_edge\_network.ansible.yaml -m ping

This does not have a reference to where the variables are stored so throws the following error:

```auto
cisco | FAILED! => {
    "msg": "The field 'remote_user' has an invalid value, which includes an undefined variable. The error was: 'netadmin_username' is undefined. 'netadmin_username' is undefined"
}

```

I’m aware I could create a playbook which defines a task to ping and passes the varible file, but I’m wondering if the structure I have in place is wrong and maybe the inventory file should be linking directly to the variable files?

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [June 17, 2024, 2:58pm UTC](https://forum.ansible.com/t/ansible-ping-from-command-line-not-working/6595/2 "2024-06-17T14:58:13Z")

</div>

Ansible inventory will dynamically pick up vars files in the `host_vars` and `group_vars` subdirectories in the same directory of the inventory file, if the vars filename matches a respective host/group in the inventory. Your playbook is directly including a `groupvars/default.yaml` vars file (note the missing underscore `_` in the directory), but running against the `corp_edge_cisco` group.

What you will want instead is to rename `ansible/inventory/groupvars/default.yaml` to either `ansible/inventory/group_vars/corp_edge_cisco.yaml` or if _all_ of your inventory will use the same defaults, rename to `ansible/inventory/group_vars/all.yaml`

Then your ad-hoc ping should work, and your playbook no longer needs to specify the `vars_files` for basic inventory information.

For reference:  
[How to build your inventory — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#organizing-host-and-group-variables)

---

<div class="post-metadata">

**Author:** ![Evan\_Goulden](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/evan_goulden/32/3864_2.png) [@Evan\_Goulden](https://forum.ansible.com/u/Evan_Goulden)\
**Post date:** [June 18, 2024, 9:34am UTC](https://forum.ansible.com/t/ansible-ping-from-command-line-not-working/6595/3 "2024-06-18T09:34:26Z")

</div>

That’s worked a treat. We figured out that I can pass --extra-vars to the command line and pass the path to the vars file but what you suggested is exactly what I needed.

Had a feeling my files and folder structure wasn’t right. Thanks!

---

<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:** [July 18, 2024, 9:34am UTC](https://forum.ansible.com/t/ansible-ping-from-command-line-not-working/6595/4 "2024-07-18T09:34:48Z")

</div>

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