2

I created a file,

./group_vars/all

which has a simple line

---
ansible_user: centos

My inventory filemy /etc/ansible/inventory has

[centos]
10.1.38.15

However, when I use ansible -m ping I see that it's trying to connect with my regular user.

~/cp/ansible$ ansible -vvv centos -m ping
.... stuff
    "msg": "Failed to connect to the host via ssh: ecarroll@10.1.38.15: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).",
    "unreachable": true

You can see it's trying to connect with ecarroll@ and not centos@. Does Ansible -m work with ./group_vars/ if I'm using a global inventory?

2

As per the documentation (my emphasis):

You can also add group_vars/ and host_vars/ directories to your playbook directory. The ansible-playbook command looks for these directories in the current working directory by default. Other Ansible commands (for example, ansible, ansible-console, etc.) will only look for group_vars/ and host_vars/ in the inventory directory. If you want other commands to load group and host variables from a playbook directory, you must provide the --playbook-dir option on the command line. If you load inventory files from both the playbook directory and the inventory directory, variables in the playbook directory will override variables set in the inventory directory.

So you'll have to provide those vars to the command itself, you can do that with

ansible -m setup 10.1.38.15 -e ansible_user=centos

or,

ansible -u centos -m setup 10.1.38.15
  • can you confirm your group_vars/all is not in etc? ie, are you running this from an arbitrary directory? – Evan Carroll Jan 2 at 20:05
  • Yes, it is from the current working directory from where I execute the ansible command. This was just intended as a POC. – maulinglawns Jan 2 at 20:06
  • is your hosts file also local? – Evan Carroll Jan 2 at 20:18
  • Yes. The hosts file is also local, as specified with -i hosts it also resides in the current working directory. – maulinglawns Jan 2 at 20:20
  • 2
    Just did (no need to delete the file though as Ansible defaults there without the -i switch), and that indeed does not work. You must place the group_vars file in the inventory directory. – maulinglawns Jan 2 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.